View source: R/check-fun-def.R
| check_fun_def | R Documentation |
check_fun_def checks whether an object is defined in the student enviornment, and returns a state that can be piped to:
check_arguments, to check whether the correct arguments where specified.
check_call, to call the function with the provided arguments, and produces a state that can be piped to check_output, check_result and check_error to compare the output, result or error from calling the function between student and solution.
check_body, that returns a state that focuses on the body that defines the function. Note that you cannot use check_object to compare variables that are limited to the function scope.
check_fun_def(state, name, undefined_msg = NULL, no_fundef_msg = NULL, append = TRUE) check_arguments(state, incorrect_number_arguments_msg = NULL, append = TRUE) ## S3 method for class 'FunDefState' check_body(state, not_found_msg = NULL, append = TRUE, ...) check_call(state, ...)
state |
the state to start from |
name |
The name of the function to test |
undefined_msg |
Custom message in case the specified function was not defined |
no_fundef_msg |
Custom message in case the function specified in |
append |
Whether or not to append the feedback to feedback built in previous states |
incorrect_number_arguments_msg |
Optional feedback message in case the function does not have the correct number of arguments. |
not_found_msg |
Custom feedback message if function definition was not found. |
... |
arguments to pass to the user-defined function to test result, output or error in a later stage |
## Not run:
# Example:
my_op <- function(a, b) {
stopifnot(length(a) == length(b))
return(abs(a) + abs(b))
}
# Robust SCT
ex() %>% check_fun_def('my_op') %>% check_correct(
{
check_call(., c(1, 2), c(3, 4)) %>% check_result() %>% check_equal()
check_call(., c(1, -2), c(3, -4)) %>% check_result() %>% check_equal()
check_call(., c(-1, 2), c(-3, 4)) %>% check_result() %>% check_equal()
check_call(., 1, c(3, 4)) %>% check_error()
check_call(., c(1, -2), 3) %>% check_error()
},
{
check_arguments(.)
check_body(.) %>% {
check_function(., 'stopifnot') %>% check_arg('...') %>% {
check_function(., 'length', index = 1) %>% check_arg('x') %>% check_equal(eval = FALSE)
check_function(., 'length', index = 2) %>% check_arg('x') %>% check_equal(eval = FALSE)
check_code(., '==')
}
check_function(., 'abs', index = 1) %>% check_arg('x') %>% check_equal(eval = FALSE)
check_function(., 'abs', index = 2) %>% check_arg('x') %>% check_equal(eval = FALSE)
}
}
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.