Oserror: Cannot Load Library 'C:\Program Files\R\R-4.0.2\Bin\X64\R.Dll': Error 0X7E

OSError: cannot load library 'C:\Program Files\R\R-4.0.2\bin\x64\R.dll': error 0x7e

1 - Windows + IDE

For those not using Anaconda, add the following in Windows' environment variables PATH:

C:\Program Files\R\R-4.0.3\bin\x64

Your R version may differ from "R-4.0.3"

2 - Anaconda

Otherwise, check Grayson Felt's reply:

I found a solution here.

Adding the PATH

C:\Users\username\Anaconda2;C:\Users\username\Anaconda2\Scripts;C:\Users\username\Anaconda2\Library\bin;C:\Users\username\Anaconda2\Library\mingw-w64\lib;C:\Users\username\Anaconda2\Library\mingw-w64\bin

and subsequently restarting Anaconda fixed the issue.

3 - Code header Windows basic

Alternatively, following Bruno's suggestion (and being more sohpisticated):

try:
import rpy2.robjects as robjects
except OSError as e:
try:
import os
import platform
if ('Windows', 'Microsoft') in platform.system():
os.environ["R_HOME"] = 'C:/Program Files/R/R-4.0.3/bin/x64' # Your R version here 'R-4.0.3'
os.environ["PATH"] = "C:/Program Files/R/R-4.0.3/bin/x64" + ";" + os.environ["PATH"]
import rpy2.robjects as robjects
except OSError:
raise(e)

This code won't be effective for non-Windows platform.
Also adjustments may be necessary for different R versions.
If it gets more complicated than this, you should probably just go for solutions 1 or 2.

NOTE: You may also face this issue if your Python and R versions are in different architechtures (x86 vs x64)

LoadLibrary failure with rpy2

I solved the problem by adding C:\Program Files\R\R-3.4.3\bin\x64 to the path. I think that this address was deleted from the path when I uninstalled the previous R version. And you need to install the address manually to the path after installing the new version, according to the R for Windows FAQ.

How to correctly set up rpy2?

You could use R interface integration with Python through a conda environment or a docker image. While the Docker approach is easier to set up, the conda approach is mainly because it allows you to manage different environments, in this case one with R and Python.

1. Using rpy2 with Docker Image

After installing Docker Desktop on your system, see this link. You could use the datasciencenotebook image from Jupyter. Just type on your terminal

docker run -it -e GRANT_SUDO=yes --user root --rm -p 8888:8888 -p 4040:4040 -v D:/:/home/jovyan/work jupyter/datascience-notebook

if it's the first time running this command it will pull first the docker image. Notice that we're mounting the local directory D:/ as a volume to the docker container. To allow this, enable file sharing inside Docker Desktop Settings, see the image below

Sample Image
Then, in a Jupyter Notebook cell just type import rpy2, rpy2 comes by default with this image.

Sample Image

2. Using rpy2 with Anaconda Environment

After succesfully installing Anaconda distribution, open the Anaconda prompt and create a new conda environment, in this case I'm calling it rpy2 environment.

conda create -n rpy2-env r-essentials r-base python=3.7

Notice that I'm including R and Python 3.7 for this environment. At the moment of writing, rpy2 is not yet compatible with the latest version of python. Then, activate your environment and install rpy2.

Sample Image

conda activate rpy2-env
conda install -c r rpy2

Now, you can use rpy2 by typing python or ipython on the terminal or through a Jupyter Notebook.

Sample Image

import rpy2.situation
for row in rpy2.situation.iter_info():
print(row)

3. Installing R packages (Optional)

Additionally, if you need to install R packages, you could type in the terminal

R -e install.packages("package_name")

or inside a Jupyter Notebook

import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector

# Choosing a CRAN Mirror
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)

# Installing required packages
packages = ('ggplot', 'stats')
utils.install_packages(StrVector(packages))


Related Topics



Leave a reply



Submit