How to use the R programming language in Jupyter Notebook#

R is a popular programming language for statistics. This topic explains how to use R in a Jupyter Notebook.

  1. Start Navigator.

  2. Make sure Jupyter Notebook is installed on your Home page.

  3. To install the R language and r-essentials packages, go to the Environments page.

  4. Click Create.

  5. Name the environment “r-tutorial”.

  6. Next to Packages, select version 3.7.13 of Python.

  7. Check the box next to R and select the version of R you want to use.

    Note

    This topic selects a version of Python that is compatible with most versions of R. Not all versions of Python and R are compatible. If you attempt to create an environment with incompatible versions, Navigator will error, list the incompatible package versions, and stop creating the environment.

  8. Click Create.

  9. Navigator creates the r-tutorial environment and selects it as active, as shown by the highlighted green bar and green play button.

  10. Click the green play button on the r-tutorial environment and select the Open with Jupyter Notebook option.

  11. To create a new notebook for the R language, in the Jupyter Notebook menu, select New, then select R.

  12. We will use dplyr to read and manipulate Fisher’s Iris multivariate data set in this tutorial. Copy and paste the following code into the first cell:

    library(dplyr)
    iris
    
  13. To run the code, in the menu bar, click Cell then select Run Cells, or use the keyboard shortcut Ctrl-Enter.

  14. The iris data table is displayed.

  15. Using ggplot, we can create a scatter plot comparing sepal length and width of three iris species. Click + to open a second cell, then copy and paste the following code into the second cell:

    library(ggplot2)
    ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + geom_point(size=3)
    
  16. To run the code, in the menu bar, click Cell then select Run Cells, or use the keyboard shortcut Ctrl + Enter (or Ctrl + Return in macOS).

For more resources on using R with Anaconda, see Using R language with Anaconda.