I am trying to create a quiz app using learnr package in R.I want to score the candidate at the or in progress and save the score in RData format.
I think the function ,get_tutorial_state(), may help but I don't know how to use it in my code.
Below is my code. Any help is appreciated.
---
title: "Quiz"
output:
learnr::tutorial:
progressive: true
allow_skip: true
language:
en:
button:
nexttopic: Next Question
previoustopic: Previous Question
runtime: shiny_prerendered
---
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
Question 1
question("Check all that you have NOT done:",
answer("installed R on my computer", message = "* Install R"),
answer("installed the RStudio IDE", message = "* Install RStudio IDE"),
answer("installed the tidyverse R package", message = "* Install Packages"),
answer("None of the above. I've done them all.", correct = TRUE, message = "You can skip this tutorial!"),
type = "multiple",
incorrect = "This tutorial is here to help! To get set up read:"
)
Question 2
question("Where do you download R?",
answer("www.rstudio.com/download"),
answer("[cloud.r-project.org](http://cloud.r-project.org)", correct = TRUE, message = "You can also download R from [cran.r-project.org](http://cran.r-project.org)"),
answer("www.r-project.org", message = "Good try, but not exactly. www.r-project.org doesn't provide a download link, but it does provide a link to one the websites above."),
answer("www.r.com"),
allow_retry = TRUE
)
Questions
Some questions to verify that you understand the purposes of various base and recommended R packages:
quiz(caption = "",
question("Which package contains functions for installing other R packages?",
answer("base"),
answer("tools"),
answer("utils", correct = TRUE),
answer("codetools")
),
question("Which of the R packages listed below are used to create plots?",
answer("lattice", correct = TRUE),
answer("tools"),
answer("stats"),
answer("grid", correct = TRUE)
)
)
A basic implementation of
get_tutorial_state()to display a score looks like this. You can append it to your .Rmd-document.The output looks like this:
How I got there
You can find a demo of how to use
get_tutorial_state()in the the learnrhash package at inst/demo_state.Rmd:Focus on the Check State section.
It is basically like writing a tiny shiny app within your tutorial: You need a
context = "server"code chunk where the server logic goes. And you need a standard code chunk where the user interface part goes (context = "ui"is the default option for all code chunks, hence you can omit it from code chunk options and just use a standard code chunk).If you have not already, I suggest that you acquire some basic knowledge in writing a shiny app before you proceed.
The output of the above tiny shiny app to call
get_tutorial_state()looks like this:So
get_tutorial_statereturns a list.This list contains one list item for each question (and, important: exercises as well, there are just no exercises in this demo). Inside each list item that represents a question, there is:
$correctentry containingTRUEorFALSE.In all list items, regardless whether it is for an exercise or a question, there is:
$typeentry containing "exercise" or "question".You can harvest this information like I did in my solution code at the beginning.
My solution from above is extracting the necessary data out of the state list, this is done inside the
serverchunk.I also changed the output format in the ui chunk to normal text because
verbatimis not what I was looking for.getting hands on the tutorial state table
If you want to better understand the structure of the tutorial state output list, it would be good to access it outside the shiny app.
In the above demo-state app, change the line
to
which yields the following if I apply this tiny shiny app in your quiz tutorial. You can copy paste that and hence recreate the list in your "normal" RStudio Environment, to experiment with it.