Create an R Virtual Environment

The steps below will walk you through creating an R virtual environment and kernel so that it may be used in your notebooks.


1. Open a Terminal.

If you're not already logged in, follow the steps found here. In the upper-right corner, click the New button and select Terminal.


2. Create virtual environment

In the Terminal, enter the command below to create a virtual environment. In this example, r_env is the name used for the virtual environment, but you may name this whatever you'd like.

conda create -n r_env r-essentials

Press return when asked to proceed. All packages needed by the virtual environment will be downloaded and installed.

Note: This will install the default R interpreter and the R Essentials bundle. If you need a more custom installation, please refer to https://docs.anaconda.com/anaconda/user-guide/tasks/use-r-language.


3. Activate virtual environment

Run source activate r_env to activate your virtual environment. Again, you may have used a different name, so substitute as appropriate. You should now see a prompt with the name of your virtual environment in parenthesis as shown.

(r_env) grahamhenke@bacon:~$

4. Run R

Once inside the virtual environment, start R by simply typing R and pressing Return. You will see some R version information and help text, followed by a prompt as shown:

>

5. Create a kernel using IRkernel

Enter the command IRkernel::installspec(). This creates an R kernel which will now be available in Jupyter.

Note: This will install a kernel with the name "ir" and a display name of "R". If you need a more custom installation, please refer to https://github.com/IRkernel/IRkernel/.


6. Open a notebook with the kernel

Navigate back to the Home tab. Make sure to first refresh the page. Next, click the New button and select the name of your kernel, which is R in this example.


7. Run R commands

You can now run cells with R code as shown.