+1 vote
ago in Programming Languages by (26.1k 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
ago by (33.0k points)
selected ago 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"