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.
Here are some examples of tutorials created with the learnr package.
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.
An example tutorial teaching a common data
transformation: filtering rows of a data frame with dplyr::filter()
.
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()
.
Install the latest official learnr release from CRAN:
install.packages("learnr")
Or you can install the most recent version in-development from GitHub with the remotes package:
# install.packages("remotes")
remotes::install_github("rstudio/learnr")
learnr works best with a recent version of RStudio (v1.0.136 or later) which includes tools for easily running and previewing tutorials.
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:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.