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)