mock_this_exercise | R Documentation |
This function helps you test your grade_this()
and grade_this_code()
logic by helping you quickly create the environment that these functions
expect when used to grade a user submission to an exercise in a learnr
tutorial.
mock_this_exercise(
.user_code,
.solution_code = NULL,
...,
.label = "mock",
.engine = "r",
.stage = "check",
.result = rlang::missing_arg(),
setup_global = NULL,
setup_exercise = NULL
)
.user_code |
A single string or expression in braces representing the user submission to this exercise. |
.solution_code |
An optional single string or expression in braces representing the solution code to this exercise. |
... |
Ignored |
.label |
The label of the mock exercise, defaults to |
.engine |
The engine of the mock exercise. If the engine is not |
.stage |
The stage of the exercise evaluation, defaults to |
.result |
The result of the evaluation of the |
setup_global |
An optional single string or expression in braces
representing the global |
setup_exercise |
An optional single string or expression in braces representing the code in the exercise's setup chunk(s). |
Returns the checking environment that is expected by grade_this()
and grade_this_code()
. Both of these functions themselves return a
function that gets called on the checking environment. In other words, the
object returned by this function can be passed to the function returned
from either grade_this()
or grade_this_code()
to test the grading
logic used in either.
# First we'll create a grading function with grade_this(). The user's code
# should return the value 42, and we have some specific messages if they're
# close but miss this target. Otherwise, we'll fall back to the default fail
# message, which will include code feedback.
this_grader <-
grade_this({
pass_if_equal(42, "Great Work!")
fail_if_equal(41, "You were so close!")
fail_if_equal(43, "Oops, just missed!")
fail()
})
# Our first mock submission is almost right...
this_grader(mock_this_exercise(.user_code = 41, .solution_code = 42))
# Our second mock submission is a little too high...
this_grader(mock_this_exercise(.user_code = 43, .solution_code = 42))
# A third submission takes an unusual path, but arrives at the right answer.
# Notice that you can use braces around an expression.
this_grader(
mock_this_exercise(
.user_code = {
x <- 31
y <- 11
x + y
},
.solution_code = 42
)
)
# Our final submission changes the prompt slightly. Suppose we have provided
# an `x` object in our global setup with a value of 31. We also have a `y`
# object that we create for the user in the exercise setup chunk. We then ask
# the student to add `x` and `y`. What happens if the student subtracts
# instead? That's what this mock submission tests:
this_grader(
mock_this_exercise(
.user_code = x - y,
.solution_code = x + y,
setup_global = x <- 31,
setup_exercise = y <- 11
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.