0

I tried installing rpy2 but it couldn't load any packages.

My Linux is Mint 20,

Python 3.8.2,

R 3.6.3,
rpy2 3.3.5 (installed with pip, by the way)

I remember I followed someone saying how to install R packages from within python and this is what I did:

import rpy2.robjects.packages as rpackages
utils.chooseCRANmirror(ind=1)

packnames= ("ggplot2", "hexbin")
from rpy2.robjects.vectors import StrVector
# R vector of strings (idk what it does)

names_to_install= [x for x in packnames if not rpackages.isinstalled(x)] 
if len(names_to_install) > 0:
    utils.install_packages(StrVector(names_to_install))
quit()

# It will create a WARNING as such: 
# R[write to console]:  'lib = "/usr/local/lib/R/site-library"' is not writable

# Would you like to use a personal library instead? (yes/No/cancel) yes
# Would you like to create a personal library
# ‘~/R/x86_64-pc-linux-gnu-library/3.6’
# to install packages into? (yes/No/cancel) yes

# SO JUST SAY YES TWICE AND IT WILL CREATE A NEW FOLDER FOR YOUR INSTALLED PACKAGES!!

Well ok so my packages were installed in /R/x86_64-pc-linux-gnu-library/3.6

But I've changed that later, when I tried some test code from the internet, I got a warning that I have no packages.

#!/usr/bin/python3.8

import rpy2

print("rpy2 version is:", rpy2.__version__)
# >>rpy2 version is: 3.3.5

import rpy2.robjects as robjects

                    # R PACKAGES
# rpy2 is providing a function rpy2.robjects.packages.importr()
# that makes that step very similar to importing Python packages.

from rpy2.robjects.packages import importr  

base = importr('base')
utils = importr('utils')


from rpy2.robjects import Formula, Environment
from rpy2.robjects.vectors import IntVector, FloatVector
from rpy2.robjects.lib import grid
from rpy2.robjects.packages import data
from rpy2.rinterface_lib.embedded import RRuntimeError
import warnings

# The R 'print' function
rprint = robjects.globalenv.find("print")
stats = importr('stats')
grdevices = importr('grDevices')
datasets = importr('datasets')

grid.activate() 

The warning was:

R[write to console]: Warning messages:

R[write to console]: 1: 
R[write to console]: In (function (package, help, pos = 2, lib.loc = NULL, character.only = FALSE,  :
R[write to console]: 
 
R[write to console]:  library ‘/usr/lib/R/site-library’ contains no packages

R[write to console]: 2: 
R[write to console]: In (function (package, help, pos = 2, lib.loc = NULL, character.only = FALSE,  :
R[write to console]: 
 
R[write to console]:  library ‘/usr/lib/R/site-library’ contains no packages

then I tried to install the packages from R, so I sudo opened R and typed install.packages() for some packages like base, utils, grDevices..., which count as base packages, it said it doesn't update it for that reason. But I found out their path is "/usr/lib/R/library"

then for other packages I wanted to install, like ggplot2, it installed it into "/usr/local/lib/R/site-library", which is what was wanted in the first installing step.

neither of those are what rpy2 wanted. It wanted /usr/lib/R/site-library

But I even found out I can add a path when importing a package like this:

base = importr('base', lib_loc="/usr/lib/R/library")

it worked kind of. I've tested my code from above (second box) line by line and I've added it for all packages imported with importr. And most of the from x import y worked aswell. Just not grid. from rpy2.robjects.lib import grid still gave me the same "library ‘/usr/lib/R/site-library’ contains no packages" message. But I don't know how to add a path there!!

My Prof and I tried to find a solution for 2 days now. I am really desperate. Why are the paths even such an issue? I saw noone talking about that the default paths it chooses are not correct, and everything I found was just for windows! If the defaults are not correct, shouldn't that be in the documentation? And if it just works for everyone else, what have I done wrong?

R library contains no packages was a similar question, but his rpy2 wanted to use usr/local/lib, which would be correct in my case!! (and there was not really an answer, at least nothing that I understood)

MOL
  • 1
  • 2
  • Perhaps this helps setting up rpy2 https://stackoverflow.com/questions/61622624/how-to-correctly-set-up-rpy2/62986815#62986815 – Miguel Trejo Aug 29 '20 at 01:07

1 Answers1

2

R can be installed in quite a few ways, and the default directory that will receive the packages can be specified at configure (and may be install) time. If you R installed for the full system, and assuming that this is the only R installed, we will call it the "system R" and the directory in which R is installed, the default ("recommended") packages are installed, and additional packages will be installed are under /usr. If you you are using a precompiled and packaged R (e.g., RPM or deb) the exact location will depend on that binary package. Writing in that directory will often require elevated privileges, which is why you need sudo.

In addition to that directory, it is possible to specify additional directories in which R packages should be installed. This is was is happening when R prompts you with an offer to use a user-local directory. (note: your packages are then installed in ~/R/x86_64-pc-linux-gnu-library/3.6, not /R/x86_64-pc-linux-gnu-library/3.6). When in R, the command .libPaths() will tell you where R is looking for its packages, and the R doc for that function will list how R get that information.

In the end you are listing a few directories with packages, or no packages, or where packages might have been installed in your question:

  • /usr/local/lib/R/site-library
  • ~/R/x86_64-pc-linux-gnu-library/3.6
  • /usr/lib/R/site-library
  • /usr/lib/R/library

There is almost certainly confusion about where your installed packages, and where you thought they were installed. Since you sudo, at some point I suggest that you uninstall and reinstall R, install packages in a personal library as R suggests it, and set the environment variable R_LIBS_USER to point out to that directory (see automatically create personal library in R).

lgautier
  • 11,363
  • 29
  • 42