The learnr package makes it easy to turn any R Markdown document into an interactive tutorial. Tutorials consist of content along with interactive components for checking and reinforcing understanding. Tutorials can include any or all of the following:
Narrative, figures, illustrations, and equations.
Code exercises (R code chunks that users can edit and execute directly).
Quiz questions.
Videos (supported services include YouTube and Vimeo).
Interactive Shiny components.
Tutorials automatically preserve work done within them, so if a user works on a few exercises or questions and returns to the tutorial later they can pick up right where they left off.
Examples
Here are some examples of tutorials created with the learnr package.
Setting Up R
A tutorial featuring videos and interactive questions to guide a new R user through the installation and set up of everything they’ll need to get started with R.
Filtering Observations
An example tutorial teaching a common data
transformation: filtering rows of a data frame with dplyr::filter()
.
Summarizing Data
An example tutorial where learners are introduced to dplyr::summarise()
. Along the way, learners also gain practice with the pipe operator, %>%
, and dplyr::group_by()
.
Installation
Install the latest version of learnr from GitHub with the remotes package:
# install.packages("remotes")
::install_github("rstudio/learnr") remotes
Or you can install the learnr package from CRAN:
install.packages("learnr")
learnr works best with a recent version of RStudio (v1.0.136 or later) which include tools for easily running and previewing tutorials.
Hello, Tutorial!
To create a tutorial, set runtime: shiny_prerendered
in the YAML frontmatter of your .Rmd
file to turn your R Markdown document into an interactive app.
Then, call library(learnr)
within your Rmd file to activate tutorial mode, and use the exercise = TRUE
chunk option to turn code chunks into exercises. Users can edit and execute the R code and see the results right within their browser.
For example, here’s a very simple tutorial:
---
title: "Hello, Tutorial!"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
```
This code computes the answer to one plus one,
change it so it computes two plus two:
```{r addition, exercise=TRUE}
1 + 1
```
This is what the running tutorial document looks like after the user has entered their answer: