Is it possible to create an r conda environment from an existing version of r?

Viewed 1172

I had R 3.6 installed on my computer a long time ago and it has all of the packages that I typically use. I wanted to make a jupyter notebook using this version of R, however my R conda environment is a different version of r and has none of my typical packages. Is there a way to create a new R conda environment from my non-conda version of R? And/or, is there still a way I can use my preferred non-conda version of R as a kernel in Jupyter Notebooks?

2 Answers

No, a non-Conda version of R cannot be used as a basis for a Conda environment.

However, it is possible to use any R installation as a kernel in Jupyter, no matter where either are installed. The key to doing so is to install IRkernel package in the R environment.

Steps for Registering R as a Jupyter Kernel

  1. If the Jupyter is installed in a Conda env, e.g., my_jupyter_env, first activate that env:

    $ conda activate my_jupyter_env
    (my_jupyter_env) $ 
    

    Otherwise, i.e., jupyter is on PATH, then just continue without any Conda envs activated.

  2. Start an R session using the native R:

    (my_jypyter_env) $ path/to/native/R
    
  3. Install IRkernel:

    > install.packages("IRkernel")
    

    Note that if this R is in a Conda env, instead use conda install r-irkernel outside of the R session.

  4. From the R session, register R as a kernel. For example, if I had an R 3.6.0 natively installed I might call it:

    > IRkernel::installspec(name="ir36_native", displayname="R 3.6.0 (native)")
    

    but the arguments are whatever you want.

  5. Quit the session and start Jupyter.

    > q()
    (my_jupyter_env) $ jupyter notebook
    
  6. Use the "New" button to create a new notebook and select the R kernel from the dropdown.

Yes it is possible. You can run conda search r-base which in my case returns

Loading channels: done
# Name                       Version           Build  Channel             
r-base                         3.1.2               0  pkgs/r              
r-base                         3.1.3               0  pkgs/r              
r-base                         3.1.3               1  pkgs/r              
r-base                         3.1.3               2  pkgs/r              
r-base                         3.2.0               0  pkgs/r              
r-base                         3.2.1               0  pkgs/r              
r-base                         3.2.2               0  pkgs/r              
r-base                         3.3.1               1  pkgs/r              
r-base                         3.3.1               2  pkgs/r              
r-base                         3.3.1               3  pkgs/r              
r-base                         3.3.1               5  pkgs/r              
r-base                         3.3.1               6  pkgs/r              
r-base                         3.3.2               0  pkgs/r              
r-base                         3.3.2               1  pkgs/r              
r-base                         3.4.1               0  pkgs/r              
r-base                         3.4.1               1  pkgs/r              
r-base                         3.4.2      haf99962_0  pkgs/r              
r-base                         3.4.3      h1c2f66e_4  pkgs/r              
r-base                         3.4.3      h1e0a451_2  pkgs/r              
r-base                         3.4.3      h290ecf8_0  pkgs/r              
r-base                         3.4.3      h290ecf8_1  pkgs/r              
r-base                         3.4.3      h9bb98a2_5  pkgs/r              
r-base                         3.5.0      h1c2f66e_1  pkgs/r              
r-base                         3.5.0      h1e0a451_1  pkgs/r              
r-base                         3.5.1      h1e0a451_2  pkgs/r              
r-base                         3.5.3      h067a564_0  pkgs/r              
r-base                         3.5.3      h26b83e4_0  pkgs/r              
r-base                         3.6.0      hce969dd_0  pkgs/r              
r-base                         3.6.1      h9bb98a2_1  pkgs/r              
r-base                         3.6.1      haffb61f_2  pkgs/r              
r-base                         3.6.1      hce969dd_0  pkgs/r

and you install your favorite version with conda install r-base=x.x.x

Related