1

I want to import libraries in my python code, so I can interface my Rcode using Python. I've installed rpy2 using pip pip rpy2 install However, I'm getting the error "ValueError: r_home is None. Try python -m rpy2.situation" in my python session.

System:

MacOS: Mojave 10.14.5

Python: 3.7.4 (using eclipse & pydev to build scripts)

R: 3.5.3

I ran this code in terminal export R_HOME=$(R RHOME) to set my R_HOME variable. Then to check I ran python -m ryp2.situation to check my output and see if the R_HOME variable was correctly added.

Python version: 3.7.4 (default, Jul 9 2019, 18:13:23) [Clang 10.0.1 (clang-1001.0.46.4)] Looking for R's HOME:

Environment variable R_HOME: /Library/Frameworks/R.framework/Resources
Calling `R RHOME`: /Library/Frameworks/R.framework/Resources
InstallPath in the registry: *** Only available on Windows *** R version:
In the PATH: R version 3.5.3 (2019-03-11) -- "Great Truth"
Loading R library from rpy2: OK Additional directories to load R packages from: None

So now it looks like I have R_HOME as a variable, but I still get the same error in executing my python code. I have tried to uninstall rpy2 in the terminal and reinstall using terminal following some of the logic in this post. I'm stuck and not sure what I should try next, so appreciate the help.

Here is the python code I want to run:

import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector

    
packageNames = ('pdftools', 'dplyr', 'stringi', 'knitr', 'markdown', 'rmarkdown')
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)
    
packnames_to_install = [x for x in packageNames if not rpackages.isinstalled(x)]
    
if len(packnames_to_install) > 0:
    utils.install_packages(StrVector(packnames_to_install))
hlzl
  • 479
  • 2
  • 5
  • 17
  • It looks like the default OSX location of the R executable is `/Library/Frameworks/R.framework/Versions/Current/Resources`. So I needed to set an environment variable (in `.bashrc` or `.bash_profile` or `.zprofile`) with the line `export R_HOME="/Library/Frameworks/R.framework/Versions/Current/Resources"`. – PeterK Mar 16 '21 at 16:06

1 Answers1

1

Try this:

pip uninstall rpy2
pip list --outdated
pip install --upgrade wheel
pip install --upgrade setuptools
pip install rpy2