Skip to content

Instantly share code, notes, and snippets.

@MayaGans
Created December 1, 2022 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MayaGans/e3ec931568ef463ab2468feb483d9a87 to your computer and use it in GitHub Desktop.
Save MayaGans/e3ec931568ef463ab2468feb483d9a87 to your computer and use it in GitHub Desktop.
library(dplyr)
library(rlang)
library(purrr)
library(learnr)
# TODO step 1 - turn the input DF into this list
# Input
quiz_list <- list(
# list item for each question
list(
# question string
question = 'this is question 1',
# data frame of questions, correct, and message
answers = data.frame(
questions = c("a1", "a2", "a3"),
correct = c(T,T,F),
message = c(NA, "message", "other message")
),
# incorrect string
incorrect = "reason why q1 is wrong"
),
# list item for each question
list(
question = 'this is question 2',
answers = data.frame(
questions = c("b1", "b2", "b3"),
correct = c(T,T,F),
message = c(NA, "message", "other message")
),
incorrect = "reason why q2 is wrong"
),
caption = ""
)
# step 2 - put it in the quiz function and eval
create_question <- function(q, a, i) {
args <- c(
# loop over the answer data.frame
# to create an answer function for each
purrr::pmap(a, ~answer(
..1,
..2,
)
),
text = q,
incorrect = i,
allow_retry = TRUE,
random_answer_order = TRUE
)
# this function takes the function as the first argument
# and the arguments as the second
# doing it this way lets us supply the answer loop as a list!
rlang::exec(learnr::question, !!!args)
}
# step 3 - TODO operationalize looping over list
create_quiz <- function(df_list) {
# this needs to be operationalized
# use a loop or purrr!
args2 <- list(
create_question(
df_list[[1]]$question,
df_list[[1]]$answers,
df_list[[1]]$incorrect
),
create_question(
df_list[[2]]$question,
df_list[[2]]$answers,
df_list[[2]]$incorrect
),
# does this need to be more robust
# to accept everything else in the list?
# are there other arguments we need to include here?
caption = df_list$caption
)
rlang::exec(learnr::quiz, !!!args2)
}
# it works!
create_quiz(quiz_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment