rstudio / gradethis

Tools for teachers to use with learnr
https://pkgs.rstudio.com/gradethis/
Other
161 stars 40 forks source link

Custom solution evaluation functions for non-R exercise engines #316

Closed gadenbuie closed 2 years ago

gadenbuie commented 2 years ago

Adds solution_eval_fn to gradethis_exercise_checker(), which takes a function with two arguments:

function(code, envir) {
  # evaluate the solution `code`
  # within `envir` or using objects gathered from `envir`
  # where `envir` is basically `envir_prep` from learnr
}

This function can be passed directly to gradethis_exercise_checker() but more often than not, you'd want to register the solution evaluation function for a specific exercise engine by providing a named list to the option gradethis.exercise_checker.solution_eval_fn, e.g.

options(
  gradethis.exercise_checker.solution_eval_fn = list(
    echo = function(code, envir) code
  )
)

This powers .solution and .solution_all out of the box. If the solution evaluation function needs to protect against missing solution code, it can throw an error with rlang::abort(class = "error_missing_solution") to signal that no solution was provided. (This is used, for example, if parse(text = code) returns no expressions in R.)