Creating Tutorials and Lessons in R using learnr

· by Hunter Glanz · Read in about 4 min · (846 words) ·

What is the learnr package?

The learnr package makes it easy to turn any R Markdown document into an interactive tutorial. With this vehicle instructors can offer interactive tools to their students to allow them to explore datasets in use from the class, a textbook, or even collected themselves. Straight from Garrett Grolemund’s fantastic introduction to the package, 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).

  • Multiple choice quizzes.

  • Videos (supported services include YouTube and Vimeo).

  • Interactive Shiny components.

With our two-part introduction to shiny last week (part 1 and part 2), your excitement for learnr should be multiplicative. Each learnr tutorial is a shiny interactive document.

Note that as with any shiny app, the learnr activity needs a home in the cloud if it is to be shared across users. If you have an RStudio server (see our recent blog on RStudio in the cloud), an RS Connect server can be set up in parallel. Alternatively, hosts shiny apps and learnr apps for free (with certain size and use limits).

Screenshot from RStudio’s Examples

In the example above you can see the section headings on the left, and the selected section’s content in the center which includes information and an area for code to be entered, run, and submitted.

Teaching with learnr

The example above is the tip of the iceberg. With learnr, instead of a single shiny application that you may have created for a particular activity, imagine building an entire lesson out as a series of sections/pages which inform students of new ideas and ask that they play around with R code, submit code, and answer questions… all using a single R Markdown document. Suddenly there are even fewer software applications to jump between for students.

Instead of giving students:

  • an R code file to run piece-wise in order to answer questions in a separate file, or

  • a pre-built R Markdown file to work through by reading and adding code to answer questions (which might then be knit at the end), or

  • a shiny app to play around with in order to answer questions within a separate document

they can now be directed to a single webpage hosting your learnr lesson. Your learnr lesson is experienced like an interactive section of a textbook that was custom-build by YOU.

Recall the recent blog entry on mosaic. Randy Pruim set up a learnr tutorial Less Volume, More Creativity (screen shot below) that can get a student up and running quickly with the fundamentals of working with mosaic before the student moves to the RStudio environment. Importantly, Randy has also provided open access to the R source code used to create the learnr tutorial for mosaic. The tutorials were part of a pair of JMM 2019 minicourses.

learnr to teach mosaic at the JMM minicourse

Want to explore on your own? Install the learnr package. Then open a new R Markdown file by selecting “File” / “New File” / “R Markdown” / “From Template”, then “Interactive Tutorial (learnr)”. Click “Run Document”.

Pedagogical notes

A word of caution, though. A common challenge for instructors is that students can tend to rush through tutorials such as these. Instructors need to ensure that students know they are responsible for what is covered, possibly with low-stakes quizzes on a regular basis or some other kind of check-in type assessment.

Despite the name of the package implying that the tutorials and lessons you create are intended for students learning R, this technology could be leveraged for many data science topics. In a given tutorial, whether the use of R is required, optional, or non-existent the learnr package provides a way to create interactive lessons for those instructors who are comfortable working with R and R Markdown files. We highly recommend you spending some time going through the examples below to get a sense for how learnr can be used and the power it has to help students work through new ideas.

Learn more

About this blog

Each day during the summer of 2019 we intend to add a new entry to this blog on a given topic of interest to educators teaching data science and statistics courses. Each entry is intended to provide a short overview of why it is interesting and how it can be applied to teaching. We anticipate that these introductory pieces can be digested daily in 20 or 30 minute chunks that will leave you in a position to decide whether to explore more or integrate the material into your own classes. By following along for the summer, we hope that you will develop a clearer sense for the fast moving landscape of data science. Sign up for emails at!forum/teach-data-science (you must be logged into Google to sign up).

We always welcome comments on entries and suggestions for new ones.