Search code examples
pythonrpy2

When I call packages from rpy2 it gives me an error


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


Solution

    1. Search for the path of your R installation (e.g. C:\Program Files\R\R-3.6.1)
    2. 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)