How to use the R programming language in Jupyter Notebook¶
R is a popular programing language for statistics. This topic explains how to use R in a Jupyter Notebook.
Make sure Jupyter Notebook is installed on your Home page.
To install the R language and r-essentials packages, go to the Environments page.
Name the environment “r-tutorial”.
Next to Packages, select version 3.7.13 of Python.
Check the box next to R and select the version of R you want to use.
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.
Navigator creates the r-tutorial environment and selects it as active, as shown by the highlighted green bar and green play button.
Click the green play button on the r-tutorial environment and select the Open with Jupyter Notebook option.
To create a new notebook for the R language, in the Jupyter Notebook menu, select New, then select R.
We will use
dplyrto read and manipulate Fisher’s Iris multivariate data set in this tutorial. Copy and paste the following code into the first cell:
To run the code, in the menu bar, click Cell then select Run Cells, or use the keyboard shortcut Ctrl-Enter.
The iris data table is displayed.
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)
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.