pweide
pweide

Reputation: 51

installing R kernel to jupyter notebook in a different conda environment

I have a conda environment containing all packages for jupyter notebook (say it's called jupyter_env. In a different conda environment I have R installed including r-irkernel (say the env is called R_env).

For python kernels I can easily make a python kernel in a specific environment (called e.g. pyth27) available to my jupyter installation in a different environment:

(pyth27) > python -m ipykernel install --prefix=/path/to/jupyter/env --name "python27"

Is there anything similar possible for the R kernel? So far I can only run the R kernel using a jupyter installation within the same environment(R_env).

One solution might be the nb-conda_kernels package. However there I'm not clear if it always adds all available kernels from all environments or whether I can specify which environments should be searched.

My question is similar to this one https://github.com/jupyter/jupyter/issues/397. Only that I don't want to use the base environment to start jupyter but a dedicated environment.

Upvotes: 5

Views: 1904

Answers (1)

jankur
jankur

Reputation: 21

As described on https://github.com/IRkernel/IRkernel, the r-ikernel package provides a mechanism similar to python -m ipykernel install, to be run in R:

R> IRkernel::installspec() 

To run this from Bash, you can do

(R_env)> Rscript -e "IRkernel::installspec()"

Now the tricky part, due to Jupyter and R being in different environments: According to https://github.com/IRkernel/IRkernel/issues/499, IRkernel::installspec() requires the jupyter-kernelspec command. I've tested two methods to provide it (to be done before issuing the above commands):

  • jupyter-kernelspec is part of Jupyter and hence in the file tree of jupyter_env, so add its path to PATH (I found it's better to add to the end so as to not disrupt other path lookups during the Rscript call)

    (R_env)> export PATH="$PATH:</path/to/conda>/envs/jupyter_env/bin"
    
  • jupyter-kernelspec is included in the jupyter_client conda package, so you can do

    (R_env)> conda install jupyter_client
    

    Caveat: this installs a number of dependencies, including Python.

I opted for the first method to keep R_env free of Python packages.

Upvotes: 2

Related Questions