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.
Usage
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()
)Arguments
- cond
A logical value or an expression that will evaluate to a
TRUEorFALSEvalue. If the value isTRUE, or would be consideredTRUEin anif (cond)statement, then a passing or failing grade is returned to the user.- message
A character string of the message to be displayed. In all grading helper functions other than
graded(),messageis a template string that will be processed withglue::glue().- ...
Passed to
graded()ingrade_this().- env
environment to evaluate the glue
message. Most users of gradethis will not need to use this argument.- praise
Include a random praising phrase with
random_praise()? The default value ofpraisecan be set usinggradethis_setup()or thegradethis.pass.praiseoption.- x
Deprecated. Replaced with
cond.- hint
Include a code feedback hint with the failing message? This argument only applies to
fail()andfail_if_equal()and the message is added using the default options ofgive_code_feedback()andmaybe_code_feedback(). The default value ofhintcan be set usinggradethis_setup()or thegradethis.fail.hintoption.- encourage
Include a random encouraging phrase with
random_encouragement()? The default value ofencouragecan be set usinggradethis_setup()or thegradethis.fail.encourageoption.
Value
pass_if() and fail_if() signal a correct or incorrect grade if
the provided condition is TRUE.
See also
Other grading helper functions: graded(), pass(), fail(),
pass_if(), fail_if(), pass_if_equal(), fail_if_equal().
Examples
# 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
#> Axel Heiberg Hainan Kyushu Melville
#> 16 13 14 16
#> New Britain Prince of Wales Southampton Spitsbergen
#> 15 13 16 15
#> Taiwan Tierra del Fuego Timor Vancouver
#> 14 19 13 12
# Has missing values somehow
grader(mock_this_exercise(islands["foo"], !!.solution))
#> <gradethis_graded: [Incorrect] You shouldn't have missing values.>
# Has too many islands
grader(mock_this_exercise(islands[islands < 29], !!.solution))
#> <gradethis_graded: [Incorrect] You included 4 too many islands.>
# Has too few islands
grader(mock_this_exercise(islands[islands < 16], !!.solution))
#> <gradethis_graded: [Incorrect] You missed 4 island(s).>
# Just right!
grader(mock_this_exercise(islands[islands < 20], !!.solution))
#> <gradethis_graded: [Correct] Great work!>
