Unable to determine R library path

Issue

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.

Solution

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.

Answered By – kesh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published