Skip to content

Commit

Permalink
Add second level of quick restore that can pull from tutorial storage…
Browse files Browse the repository at this point in the history
… if available (#794)
  • Loading branch information
gadenbuie authored Sep 27, 2023
1 parent 0e1c0d6 commit 58aa962
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
25 changes: 18 additions & 7 deletions R/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 58aa962

Please sign in to comment.