Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve last value from evaluating exercises. Pass this to checker functions #228

Merged
merged 7 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: learnr
Type: Package
Title: Interactive Tutorials for R
Version: 0.9.2.9000
Version: 0.9.2.9001
Authors@R: c(
person("Barret", "Schloerke", role = c("aut", "cre"), email = "barret@rstudio.com"),
person("JJ", "Allaire", role = "aut", email = "jj@rstudio.com"),
Expand Down
38 changes: 34 additions & 4 deletions R/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ evaluate_exercise <- function(exercise, envir) {
check_code = exercise$code_check,
envir_result = NULL,
evaluate_result = NULL,
envir_prep = envir_prep
envir_prep = envir_prep,
last_value = NULL
)

# if it's an 'incorrect' feedback result then return it
Expand Down Expand Up @@ -197,9 +198,37 @@ evaluate_exercise <- function(exercise, envir) {
keep_md = FALSE
)

# capture the last value and use a regular output handler for value
# https://github.com/r-lib/evaluate/blob/e81ba2ba181827a86525767371e6dfdeb364c8b7/R/output.r#L54-L56
# @param value Function to handle the values returned from evaluation. If it
# only has one argument, only visible values are handled; if it has more
# arguments, the second argument indicates whether the value is visible.
last_value <- NULL
default_output_handler <- evaluate::new_output_handler()
has_visible <- length(formals(default_output_handler$value)) > 1
learnr_output_handler <- evaluate::new_output_handler(value = function(x, visible) {
last_value <<- x

if (has_visible) {
default_output_handler$value(x, visible)
} else {
if (visible) {
default_output_handler$value(x)
} else {
NULL
schloerke marked this conversation as resolved.
Show resolved Hide resolved
}
}
})

evaluate_result <- NULL
knitr_options$knit_hooks$evaluate = function(code, envir, ...) {
evaluate_result <<- evaluate::evaluate(code, envir, ...)
knitr_options$knit_hooks$evaluate = function(
code, envir, ...,
output_handler # set to avoid name collision
) {
evaluate_result <<- evaluate::evaluate(
code, envir, ...,
output_handler = learnr_output_handler
)
evaluate_result
}
output_format <- rmarkdown::output_format(
Expand Down Expand Up @@ -265,7 +294,8 @@ evaluate_exercise <- function(exercise, envir) {
check_code = exercise$check,
envir_result = envir,
evaluate_result = evaluate_result,
envir_prep = envir_prep
envir_prep = envir_prep,
last_value = last_value
)

# validate the feedback
Expand Down