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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -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
4 changes: 2 additions & 2 deletions NEWS.md
@@ -1,8 +1,6 @@
learnr 0.10.0 (unreleased)
===========

## Breaking changes

## New features

* Aggressively rerender prerendered tutorials in favor of a cohesive exercise environment ([#169](https://github.com/rstudio/learnr/issues/169), [#179](https://github.com/rstudio/learnr/pull/179), and [rstudio/rmarkdown#1420](https://github.com/rstudio/rmarkdown/pull/1420))
Expand All @@ -11,6 +9,8 @@ learnr 0.10.0 (unreleased)

## Minor new features and improvements

* Added the last evaluated exercise submission value, `last_value`, as an exercise checker function argument. ([#228](https://github.com/rstudio/learnr/pull/228))

* Added tabset support. ([#219](https://github.com/rstudio/learnr/pull/219) [#213](https://github.com/rstudio/learnr/issues/213))

* Question width will expand to the container width. ([#222](https://github.com/rstudio/learnr/pull/222))
Expand Down
34 changes: 30 additions & 4 deletions R/exercise.R
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,33 @@ 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_arg <- length(formals(default_output_handler$value)) > 1
learnr_output_handler <- evaluate::new_output_handler(value = function(x, visible) {
last_value <<- x

if (has_visible_arg) {
default_output_handler$value(x, visible)
} else {
default_output_handler$value(x)
}
})

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 +290,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