3

I'm implementing a simple function in python with the rpy2 API.

In VScode I manage to print a confidence interval, calculating Z-statistic calling the function created with the robjects as seen in the pic. But before the printed value I get the message "Unable to determine R home: [WinError 2] The system could not find the specified file". The same message appears in Jupyter but doesn't allow me to print anything at all, when calling a rpy2 function it get the error "Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>'".

Also just importing the package itself "import rpy2.robjects" retrives the same message. Import rpy2 alone doesn't show up anything.

I tried to add Rscript.exe and R.exe path to system variables but didn't work, neither of them (not sure if I did it properly), tried to reinstall R, didn't work.

Any clue on how to solve this?

Image of the function being used and it's output in VSCode The output for the same function as before but in jupyter

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
João
  • 51
  • 1
  • 3

1 Answers1

5

The cause of the error is that Windows R has changed its registry structure and it made the current rpy2's R install path detection obsolete. I've pushed a PR to address this issue, but for the meantime your solution is to set R_HOME environmental variable.

Here is what you could do:

import os
os.environ["R_HOME"] = r"C:\Program Files\R\R-4.2.0" # change as needed
import rpy2

As long as you run the os.environ... line before importing any rpy2 module, you should be good to go.

On a side note, you can also set os.environ["R_LIBS_USER"] to use per-user library path.

kesh
  • 4,515
  • 2
  • 12
  • 20
  • I too am facing the same issue and tried to implement what you had mentioned but it doesn't seem to still work for me. – kev Jun 29 '22 at 14:53
  • That's curious. Is it with Python or Jupyter? In any case, post a new question and I can try to help you. – kesh Jun 29 '22 at 14:57
  • If you are using Jupyter, see if [this workaround](https://stackoverflow.com/questions/72575015) helps. – kesh Jun 29 '22 at 14:59