+1 vote
in Programming Languages by (39.4k points)

I am getting the following error when I try to use the rpy2 package in a conda environment.

ValueError: r_home is None. Try python -m rpy2.situation

The location of R in my conda environment is: "~/.conda/envs/my_env/bin/R"

How can I set the r_home environment variable to the above R home directory? 

1 Answer

+2 votes
by (66.8k points)
selected by
Best answer

You can set the environment variable even in your Python code using the os library of Python. 

Add the following two lines to your Python code, and it should solve the error.

import os

os.environ['R_HOME'] = "~/.conda/envs/my_env/bin/R"