2

I am new to R. I am running Jupyter Lab on a Windows 11 machine, and have created a virtual environment where I installed some packages and irkernel. I get the following message when I execute %load_ext rpy2.ipython:

Unable to determine R library path: Command '('C:\\Users\\ephra\\miniconda3\\envs\\cde\\Lib\\R\\bin\\Rscript', '-e', 'cat(Sys.getenv("LD_LIBRARY_PATH"))')' returned non-zero exit status 1.

Here is my complete code:

import os
os.environ['R_HOME'] = 'C:\\Users\\ephra\\miniconda3\\envs\\cde\\Lib\\R'
os.environ['R_USER'] = 'C:\\Users\\ephra\\miniconda3\\envs\\cde\\Lib\\site-packages\\rpy2' 

from src.setup import *

%load_ext rpy2.ipython

%%R 
library(tidyverse)

Apart from the environment variables, the above code comes from David Mertz book "Cleaning Data for Effective Data Science". I need your help.

ezyman
  • 155
  • 8
  • Sorry I can't help with this specific issue but if your looking to combine R and Python I would highly recommend using RMarkdown notebooks in RStudio. It takes a lot of the pain out of trying to use the two languages together. – JaredS Jun 10 '22 at 13:30
  • rpy2 does not seem to be supported for windows. LD_LIBRARY_PATH is something you have in unix and unix-like OS's, but not windows. From the rpy2 documentation: *Running rpy2 on Windows is currently not supported although relative success was recently reported with the lastest in the 3.3.x series.* Referene: https://rpy2.github.io/doc/v3.3.x/html/overview.html#installation – topsail Jun 10 '22 at 14:27

1 Answers1

3

Note: This is a speculative answer as I don't use Jupyter

That os.environ trick works for me in plain Python but Jupyter's magic routine %load_ext rpy2.ipython utilizes LD_LIBRARY_PATH to set itself up. The error comes from the fact that LD_LIBRARY_PATH is Linux/Unix thing hence the trouble.

Failed Attempt #1 What you may be able to do is to add the following line to your os.environ calls:

os.environ['LD_LIBRARY_PATH'] = 'C:\\Users\\ephra\\miniconda3\\envs\\cde\\Lib\\R\\bin\\x64'

Assuming 'C:\Users\ephra\miniconda3\envs\cde\Lib\R\bin' exists. If not, look for a R subfolder with bunch of exe and dll files.

Attempt #2

Surveying further, I noticed that LD_LIBRARY_PATH itself is never used by rpy2, and it appears to be a Linux workaround. So, what happens if you comment out Lines 26-28 of Lib\site-packages\rpy2\rinterface_lib\openrlib.py?

The lines should read

LD_LIBRARY_PATH = (rpy2.situation.r_ld_library_path_from_subprocess(R_HOME)
                   if R_HOME is not None
                   else '')

If this hack succeeds, let me know and I can file a PR.

kesh
  • 4,515
  • 2
  • 12
  • 20
  • I added the line but still get the message "Unable to determine R library path: Command '('C:\\Users\\ephra\\miniconda3\\envs\\cde\\Lib\\R\\bin\\Rscript', '-e', 'cat(Sys.getenv("LD_LIBRARY_PATH"))')' returned non-zero exit status 1." – ezyman Jun 10 '22 at 17:05
  • OK. That's too bad, but I see why my idea doesn't work... If you are gun ho about getting this to work, you can try another hack idea in my updated answer above – kesh Jun 10 '22 at 17:33
  • The hack has succeeded: the "os.environ['LD_LIBRARY_PATH'] = ..." in the os.environ has been removed and I did commented out Lines 26-28 of Lib\site-packages\rpy2\rinterface_lib\openrlib.py as you suggested. I really appreciate! – ezyman Jun 10 '22 at 18:36
  • Good to hear. I've added this workaround to my pending PR (which fixes the auto detection issue) so hopefully it'll be fixed in the future releases. – kesh Jun 10 '22 at 19:00
  • good research. appears to be a related open issue here: https://github.com/rpy2/rpy2/issues/875 – topsail Jun 11 '22 at 15:21