diff --git a/NEWS.md b/NEWS.md index 60fdc3c7..4a68803f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,8 @@ - When the `LC_ALL` environment variable is `"C"` or `"C.UTF-8"`, R may ignore the `LANGUAGE` environment variable, which means that learnr may not be able to control the language of R's messages. learnr's tests no longer test R message translations in these cases. If you are deploying a tutorial written in a language other than English, you should ensure that the `LC_ALL` environment variable is not set to `"C"` or `"C.UTF-8"` and you may need to set the `LANGUAGE` variable via an `.Renviron` file rather than relying on learnr (#801). +- Added a new quick restore option that restores both the last submitted exercise code and the output of that submission, if the output is available to be restored. This option is enabled by setting the global option `tutorial.quick_restore = 2` or the environment variable `TUTORIAL_QUICK_RESTORE=2`. This option augments the quick restore value when `TRUE` or `1`, wherein only the last submitted **code** is restored, such that users will need to click the "Submit" button to evaluate and see the output. (#794) + # learnr 0.11.4 - Moved curl from Imports to Suggests. curl is only required when using an external evaluator (#776). diff --git a/R/exercise.R b/R/exercise.R index ce833942..a468af1f 100644 --- a/R/exercise.R +++ b/R/exercise.R @@ -9,6 +9,16 @@ cache_complete_exercise <- function(exercise) { exercise } +get_opt_quick_restore <- function() { + env <- Sys.getenv("TUTORIAL_QUICK_RESTORE", NA) + if (!is.na(env)) return(env) + + opt <- getOption("tutorial.quick_restore", NA) + if (isTRUE(opt)) return("1") + if (isFALSE(opt)) return("0") + if (!is.na(opt)) as.character(opt) else "0" +} + # run an exercise and return HTML UI setup_exercise_handler <- function(exercise_rx, session) { @@ -36,13 +46,8 @@ setup_exercise_handler <- function(exercise_rx, session) { # short circuit for restore (we restore some outputs like errors so that # they are not re-executed when bringing the tutorial back up) if (exercise$restore) { - if ( - getOption( - "tutorial.quick_restore", - identical(Sys.getenv("TUTORIAL_QUICK_RESTORE", "0"), "1") - ) - ) { - # don't evaluate at all if quick_restore is enabled + if (identical(get_opt_quick_restore(), "1")) { + # quick_restore: don't evaluate at all if super fast restore is enabled rv$result <- list() return() } @@ -70,6 +75,12 @@ setup_exercise_handler <- function(exercise_rx, session) { rv$result <- output return() } + + if (identical(get_opt_quick_restore(), "2")) { + # quick_restore: previous evaluated output wasn't stored, don't eval now + rv$result <- list() + return() + } } # get exercise evaluator factory function (allow replacement via global option)