Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly set up rpy2?

Tags:

python

r

rpy2

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!

like image 895
random Avatar asked May 05 '20 20:05

random


1 Answers

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))
like image 167
Miguel Trejo Avatar answered Oct 11 '22 09:10

Miguel Trejo