Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using rpy.robjects a message comes up "Unable to determine R home: [WinError 2] The system could not find the specified file"

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

like image 595
João Avatar asked Dec 05 '25 17:12

João


1 Answers

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.

like image 62
kesh Avatar answered Dec 08 '25 09:12

kesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!