import rpy2.robjects as ro
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
It raises an error:
ValueError: r_home is None. Try python -m rpy2.situation
I tried googleing but I don't find an answer. I use Linux and python3
Thank you for your help
Search for the path of your R installation (e.g. C:\Program Files\R\R-3.6.1)
Use the following code before importing rpy2:
import os
os.environ['R_HOME'] = "C:\Program Files\R\R-3.6.1" #or whereever your R is installed
This works for me (but only temporarily)
Related
I can import the rpy2 library without ant issue
import rpy2
However, when I want to import other objects from this package, I get errors
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
from rpy2.robjects.vectors import ListVector
For these imports, I get the following errors:
Unable to determine R home: [WinError 2] The system cannot find the file specified
R[write to console]: Error: cons memory exhausted (limit reached?)
R[write to console]: Error: no more error handlers available (recursive errors?); invoking 'abort' restart
I have installed Rstudio but aparrently, there is no connection between Rstudio and Microsoft Visual Code Studio, from which I run the Python code.
I have installed r2py via pip on a Mac OS.
python3 -m pip install rpy2
If I try to call it I get an error
import os
os.environ['R_HOME'] = '/Library/Frameworks/R.framework/Resources/bin/R'
from rpy2.robjects.packages import importr
OSError: cannot load library '/Library/Frameworks/R.framework/Resources/bin/R/lib/libR.dylib': dlopen(/Library/Frameworks/R.framework/Resources/bin/R/lib/libR.dylib, 2): no suitable image found. Did find:
/Library/Frameworks/R.framework/Resources/bin/R/lib/libR.dylib: stat() failed with errno=20
I'm a bit troubled with a simple thing. I was trying to install a package called hunspell, but I discovered it is originally an R package. I installed this version: https://anaconda.org/conda-forge/r-hunspell, but I'm not being able to import it. Is this package supposed to work with Python? Should I use rpy2 to import it? First time using cross-platform packages so I'm a bit confused.
Just to be clear, import hunspell brings ModuleNotFoundError: No module named 'hunspell' and import r-hunspell brings SyntaxError: invalid syntax.
I also noticed that this package, also installed an r-base package, but I'm also not sure how to import that.
After running in the command line:
pip install rpy2
or with the "!" if you're in a Jupyter Notebook. The following procedure will answer your issue, based on the official documentation:
# Using R inside python
import rpy2
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
from rpy2.robjects.packages import importr
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)
# Install packages
packnames = ('hunspell', 'some other desired packages')
utils.install_packages(StrVector(packnames))
# Load packages
hunspell = importr('hunspell')
If you want to access specific functions in this module you could check out these answer or that answer too.
According to documentation http://rpy2.readthedocs.io/en/version_2.8.x/robjects_oop.html it shows how to import R packages that is not standard in python. To my luck they do the example I need which is lme4
import rpy2.robjects as ro
from rpy2.robjects import FloatVector
from rpy2.robjects.packages import importr
import rpy2.rinterface as rinterface
stats = importr('stats')
base = importr('base')
lme4 = importr('lme4')
getmethod = ro.baseenv.get("getMethod")
StrVector = ro.StrVector
No matter what I did I got the error
RRuntimeError: Error in loadNamespace(name) : there is no package called 'lme4'
I'm in windows environment and I know that this package is installed under
"C:/Users/me/Documents/R/win-library/3.4" not the standard "C:/Program Files/R/R-3.4.3/library"
Please any help is greatly appreciated.
Note that the error message is coming from the R kernel (RRuntimeError). This suggests that the R kernel can't find the package lme4. I think you have two options here:
Launch the R kernel and install lme4 there (install.packages('lme4'))
IF you used pip/conda to install rpy2, it should have also installed R in your environment (you can confirm via pip freeze or conda list). In this case, you can use pip/conda to install lme4 via the package r-lme (conda install r-lme)
I've run into a rather strange error when doing unit testing with the numpy.testing module. I'm running an iPython notebook in a VM. In my code, I have one test where I compare my output to that in R. This requires me to load the rpy2 modules like so:
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
fastclime = importr('fastclime')
grdevices = importr('grDevices')
However when I run ! py.test, I get the following error:
==================================== ERRORS ====================================
_____________________ ERROR collecting test_fastclime_R.py _____________________
test_fastclime_R.py:6: in <module>
import rpy2.robjects as robjects
../../anaconda/lib/python2.7/site-packages/rpy2/robjects/__init__.py:15: in <module>
import rpy2.rinterface as rinterface
../../anaconda/lib/python2.7/site-packages/rpy2/rinterface/__init__.py:101: in <module>
from rpy2.rinterface._rinterface import *
E ImportError: /home/bitnami/anaconda/bin/../lib/libreadline.so.6: undefined symbol: PC
====================== 10 passed, 1 error in 0.19 seconds ======================
I suspect that this has something to do with some environment variable not being linked to my working directory, but I have no idea how to fix it. Any suggestions are greatly appreciated! Thank you!
The solution was to
1) delete __pycache__ directory if a previous version was created
2) Install readline in anaconda in the command line:
conda install -c asmeurer readline
3) Inside the .py file include import readline
another solution is to remove conda's readline from the environment and to use pip's one:
conda remove --force readline
pip install readline