6

I'm trying to run rpy2 with no success. After installing R and rpy2, I tried testing rpy2 as instructed in the rp2 documentation:

from IDLE:

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

I'm getting the following output:

rpy2 version:
3.3.2
Python version:
3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)]
Looking for R's HOME:
    Environment variable R_HOME: None
    InstallPath in the registry: C:\Program Files\R\R-4.0.0
    Environment variable R_USER: None
    Environment variable R_LIBS_USER: None
R version:
    In the PATH: None
    Loading R library from rpy2: cannot load library 'C:\Program Files\R\R-4.0.0\bin\x64\R.dll': error 0xc1
Additional directories to load R packages from:
None

I set the environment variables that's not found as mentioned in the output, because I thought that maybe R's installation location is the issue, but it still doesn't work. I also looked for solutions for the R.dll error or dll files errors in general.

Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
random
  • 146
  • 2
  • 10

3 Answers3

11

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

enter image description here Then, in a Jupyter Notebook cell just type import rpy2, rpy2 comes by default with this image.

enter image description here

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.

enter image description here

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.

enter image description here

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))
Miguel Trejo
  • 5,913
  • 5
  • 24
  • 49
0

It seems that your Python version is 32 bit and R version is 64 bit. Try linking to the R in the bin\i386 instead of the bin\x64 folder.

Feng Mai
  • 2,749
  • 1
  • 28
  • 33
0

You just need to add the location of the R.dll library (x64 or i386 depending on whether you use python 32 or 64) to path.

From the error you got, this is in your case : C:/Program Files/R/R-4.0.0/bin/x64

This is a common problem observed with other libraries and the solution is straight forward :

import os
os.environ['PATH'] = 'C:/Program Files/R/R-4.0.0/bin/x64' + os.pathsep + os.environ.get('PATH', '')
import rpy2.situation
for row in rpy2.situation.iter_info():print(row)

rpy2 version:
3.3.5
Python version:
3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
Looking for R's HOME:
    Environment variable R_HOME: None
    InstallPath in the registry: C:\Program Files\R\R-4.0.0
    Environment variable R_USER: None
    Environment variable R_LIBS_USER: None
R version:
    In the PATH: 
    Loading R library from rpy2: OK
Additional directories to load R packages from:
None
Waldi
  • 39,242
  • 6
  • 30
  • 78