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.
Related
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
Even after installing AppKit, I am not able to import NSWorkspace from it.
This is the error I am receiving:
ImportError: cannot import name 'NSWorkspace' from 'AppKit'
I have tried installing pyobjc, but still it's not solving the problem
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)
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)