#Load necessary packages library(learnr) library(tidyr) library(dplyr) library(checkr) #Set global options for the exercises knitr::opts_chunk$set(echo = FALSE) tutorial_options(exercise.eval=TRUE) knitr::opts_chunk$set(exercise.checker = checkr::checkr_tutor)
#Load data faithful_data <- sample_n(faithful, 100) #Run simulations
Here's a simple exercise with an empty code chunk provided for entering the answer.
Write the R code required to add two plus two Theory [@Ittner2007]:
Here's an exercise with some prepopulated code as well as exercise.lines = 5
to provide a bit more initial room to work.
Now write a function that adds any two numbers and then call it:
add <- function() { }
Here's an exercise where the chunk is pre-evaulated via the exercise.eval
option (so the user can see the default output we'd like them to customize). We also add a "hint" to the correct solution via the chunk immediate below labeled print-limit-hint
.
Modify the following code to limit the number of rows printed to 5:
mtcars
head(mtcars)
# Change the filter to select February rather than January mtcars <- filter(mtcars, cyl == 6)
mtcars <- filter(mtcars, cyl == 8)
You can include any number of single or multiple choice questions as a quiz. Use the question
function to define a question and the quiz
function for grouping multiple questions together.
Some questions to verify that you understand the purposes of various base and recommended R packages:
quiz( question("Which package contains functions for installing other R packages?", answer("base"), answer("tools"), answer("utils", correct = TRUE), answer("codetools"), allow_retry = TRUE ), question("Which of the R packages listed below are used to create plots?", answer("lattice", correct = TRUE), answer("tools"), answer("stats", message = "Sorry, but this is for statitics."), answer("grid", correct = TRUE), allow_retry = TRUE ) )
Change the filter to select 8 cylinders instead of 6.
# Compute 2 + 2 4 + 4
# code to check exercise here test_1 <- find_call("whatever + whatever", message = "need to use addition (+)") test_2 <- find_call("2 + whatever", message = "first argument should be 2") test_3 <- find_call("whatever + 2", message = "second argument should be 2") test_4 <- check_value(agrees(x == 4), message = "the result should be 4") USER_CODE %>% test_1 %>% test_2 %>% test_3 %>% final_ %>% test_4
selectInput("n_breaks", label = "Number of bins:", choices = c(10, 20, 35, 50), selected = 20) sliderInput("bw_adjust", label = "Bandwidth adjustment:", min = 0.2, max = 2, value = 1, step = 0.2)
plotOutput("eruptions")
output$eruptions <- renderPlot({ hist(faithful_data$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks), xlab = "Duration (minutes)", main = "Geyser Eruption Duration") dens <- density(faithful_data$eruptions, adjust = input$bw_adjust) lines(dens, col = "blue") })
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.