'How to connect R conda env to jupyter notebook

I am creating conda environment using following code

 conda create --prefix r_venv_conda r=3.3  r-essentials  r-base --y

Then I am activating this env by following

 conda activate r_venv_conda/

Then I tried to run Jupyter Notebook (by running jupyter notebook to run jupyter hoping that will connect R env. However, I am getting following error

Traceback (most recent call last):
  File "/home/Documents/project/r_venv_conda/bin/jupyter-notebook", line 7, in <module>
    from notebook.notebookapp import main
  File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/notebook/__init__.py", line 25, in <module>
    from .nbextensions import install_nbextension
  File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/notebook/nbextensions.py", line 26, in <module>
    from .config_manager import BaseJSONConfigManager
  File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/notebook/config_manager.py", line 14, in <module>
    from traitlets.config import LoggingConfigurable
  File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/traitlets/config/__init__.py", line 6, in <module>
    from .application import *
  File "/home/Documents/project/r_venv_conda/lib/python3.6/site-packages/traitlets/config/application.py", line 38, in <module>
    import api.helper.background.config_related
ModuleNotFoundError: No module named 'api'

How can I fix this issue?



Solution 1:[1]

Jupyter does not automatically recognize Conda environments, activated or not.

Kernel Module

First, for an environment to run as a kernel, it must have the appropriate kernel package installed. For R environments, that is r-irkernel, so that one needs to run

conda install -n r_venv_conda r-irkernel

For Python kernels, it's ipykernel.

Kernel Registration

Second, kernels need to be registered with Jupyter. If one has Jupyter installed via Conda (say in an Anaconda base env), then I recommend using the nb_conda_kernels package, which enables auto-discovery of kernel-ready Conda environments. This must be installed in the environment that has jupyter installed (only one installation is needed!), for example, if this is base, then

conda install -n base nb_conda_kernels

Please read the documentation for details.

If using a system-level installation of Jupyter (i.e., not installed by Conda), then one needs to manually register the kernel. For example, something like

conda run -n r_venv_conda Rscript -e 'IRkernel::installspec(name="ir33", displayname="R 3.3")'

where one can set arbitrary values for name and displayname. See IRkernel for details.

Running Jupyter

If using a Conda-installed Jupyter, again, it only needs to be installed in a single env. This is the environment that should be activated before running jupyter notebook. The kernel will be available to select from within Jupyter.

Solution 2:[2]

I found the best approach for me was

conda create -n viper r python=3.8.8 #check your conda python version
conda activate viper
conda install -c r r-essentials

This will set up those useful r packages and you can install more later. And give access to the r-Kernel.

Solution 3:[3]

I use these bash commands to create R environment and connect it to the jupyter session:

# 1. pick a name for the conda environment
name='r_env'
# 2. create the environment
conda create -n $name r-essentials r-base 
# 3. install `irkernel` and `devtools` for `IRkernel/repr`
conda install -c conda-forge r-devtools r-irkernel
# 4. setup `irkernel`
Rscript -e 'IRkernel::installspec(name="$name", displayname="$name")'
# 4. install and setup `IRkernel/repr` (for displaying help messages in jupyter)
Rscript -e 'devtools::install_github("IRkernel/repr")'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sunday Ikpe
Solution 3