From 94821b881c024f9e02e3dd61a6f644c010fd3c90 Mon Sep 17 00:00:00 2001 From: mitchelloharawild Date: Thu, 14 Dec 2023 00:05:24 +1100 Subject: [PATCH] Added automatic cloze functionality --- NAMESPACE | 2 ++ R/answer.R | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 4c50605..265d92e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,7 @@ # Generated by roxygen2: do not edit by hand +S3method(cloze,character) +S3method(cloze,numeric) export(answer_multichoice) export(answer_numerical) export(answer_shortanswer) diff --git a/R/answer.R b/R/answer.R index 9315864..e3936eb 100644 --- a/R/answer.R +++ b/R/answer.R @@ -80,3 +80,28 @@ choices <- function(options, answer) { names(i) <- options i } + +#' Succinctly create a suitable cloze question +#' +#' @param x The correct answer +#' @param ... Options passed on to other methods +#' +#' @export +cloze <- function(x, ...) { + UseMethod("cloze") +} + +#' @export +cloze.numeric <- function(x, ...) { + answer_numerical(x, ...) +} + +#' @export +cloze.character <- function(x, choices = NULL, ...) { + if(is.null(choices)) + answer_shortanswer(x, ...) + else if(length(x) > 1) + answer_multichoice(choices(choices, x)) + else + answer_singlechoice(choices(choices, x)) +}