pass_if | R Documentation |
pass_if()
and fail_if()
both create passing or failing grades if a given
condition is TRUE
. See graded()
for more information on gradethis
grade-signaling functions.
These functions are also used in legacy gradethis code, in particular
in the superseded function grade_result()
. While previous versions of
gradethis allowed the condition to be determined by a function or
formula, when used in grade_this()
the condition must be a logical TRUE
or FALSE
.
pass_if(
cond,
message = NULL,
...,
env = parent.frame(),
praise = getOption("gradethis.pass.praise", FALSE),
x = deprecated()
)
fail_if(
cond,
message = NULL,
...,
env = parent.frame(),
hint = getOption("gradethis.fail.hint", FALSE),
encourage = getOption("gradethis.fail.encourage", FALSE),
x = deprecated()
)
cond |
A logical value or an expression that will evaluate to a |
message |
A character string of the message to be displayed. In all
grading helper functions other than |
... |
Passed to |
env |
environment to evaluate the glue |
praise |
Include a random praising phrase with |
x |
Deprecated. Replaced with |
hint |
Include a code feedback hint with the failing message? This
argument only applies to |
encourage |
Include a random encouraging phrase with
|
pass_if()
and fail_if()
signal a correct or incorrect grade if
the provided condition is TRUE
.
pass_if()
: Pass if cond
is TRUE
.
fail_if()
: Fail if cond
is TRUE
.
Other grading helper functions: graded()
, pass()
, fail()
,
pass_if()
, fail_if()
, pass_if_equal()
, fail_if_equal()
.
# Suppose the prompt is to find landmasses in `islands` with land area of
# less than 20,000 square miles. (`islands` reports land mass in units of
# 10,000 sq. miles.)
grader <-
# ```{r example-check}
grade_this({
fail_if(any(is.na(.result)), "You shouldn't have missing values.")
diff_len <- length(.result) - length(.solution)
fail_if(diff_len < 0, "You missed {abs(diff_len)} island(s).")
fail_if(diff_len > 0, "You included {diff_len} too many islands.")
pass_if(all(.result < 20), "Great work!")
# Fall back grade
fail()
})
# ```
.solution <-
# ```{r example-solution}
islands[islands < 20]
# ```
# Peek at the right answer
.solution
# Has missing values somehow
grader(mock_this_exercise(islands["foo"], !!.solution))
# Has too many islands
grader(mock_this_exercise(islands[islands < 29], !!.solution))
# Has too few islands
grader(mock_this_exercise(islands[islands < 16], !!.solution))
# Just right!
grader(mock_this_exercise(islands[islands < 20], !!.solution))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.