R kernel crashes while loading R package using rpy2

Question:

First of all, I’m new to rpy2 / jupyter so please don’t judge me if this isn’t the correct place to ask my question.

I am trying to set up an integrated workflow for data analysis using R and Python and I encounter the following error:

I am on Ubuntu 19.04. running a conda environment using Jupyter 1.0.0, Python 3.7.4, R 3.5.1, r-irkernel 1.0.2 and rpy2 3.1.0 and I installed the R-package Seurat through R.

When I create a Jupyter notebook using the R-kernel, I can load Seurat with library(Seurat) just fine.

I can also use R code in python using rpy2 and the rmagic such as:

%load_ext rpy2.ipython
%%R
data(allen, package = 'scRNAseq')
adata_allen <- as(allen, 'SingleCellExperiment')

However when I try to load Seurat using rpy2 the kernel crashes:

%%R
library(Seurat)

And I get the following message:

Kernel Restarting
The kernel appears to have died. It will restart automatically

Jupyter gives the following message in the command line:

[I 16:39:01.388 NotebookApp] KernelRestarter: restarting kernel (1/5), keep random ports
kernel 23284ec0-63d5-4b61-9ffa-b52d19851eab restarted

Note that other libraries such as library(dplyr) load just fine using rpy2.

My complete conda environment can be found in the attached text file.

I just can’t seem to figure out what is causing the problem. Is there a way to get a more verbose error message from Jupyter?

Your help would be greatly appreciated!

Regards Felix

Asked By: fkoegel

||

Answers:

The R package Seurat is using an other R package called reticulate, providing a bridge to Python from R.

Unfortunately, whenever rpy2 and reticulate are involved R ends up being initialized twice, which results inevitably in a segfault. This is still an open bug at the time of writing. The issue tracking on the rpy2 side (a link to the reticulate side of the tracking can be found there) is here:

https://bitbucket.org/rpy2/rpy2/issues/456/reticulate-rpy2-sharing-r-process

Answered By: lgautier

I’ve got the same problem with you. But I downgrade to Seurat 3.0.2, your problem will be fixed. To use the user defined R kernel for rpy2 with conda, run the code before at the very beginning (before imoort rpy2)

# user defined R installation
import os
os.environ['R_HOME'] = '/path/to/miniconda/envs/seurat/lib/R' #path to your R installation
os.environ['R_USER'] = '/path/to/miniconda/lib/python3.7/site-packages/rpy2' #path depends on where you installed Python.
Answered By: maxima

This worked for me, while facing issue of kernel getting dead during importing robjects from rpy2:

import os
os.environ['R_HOME'] = '/Users/<your user>/anaconda3/envs/<env name>/lib/R'

# import your desired module
from rpy2.robjects.packages import importr
Answered By: Vivek Prajapati

I had the same problem and I am also using R and python with a Jupyter notebook in docker.

I solved the Kernel crash issue by starting my notebook or Python code with this:

import os 
os.environ['R_HOME'] = '/usr/lib/R'

/usr/lib/R is where I have my system’s R installation and libraries, and should be an R version needed by rpy2. Hope this helps.

Answered By: Luis Peraza