user_object | R Documentation |
Functions for interacting with objects created by student and solution code
user_object_get(x, mode = "any", ..., check_env = parent.frame())
solution_object_get(x, mode = "any", ..., check_env = parent.frame())
user_object_exists(x, mode = "any", ..., check_env = parent.frame())
solution_object_exists(x, mode = "any", ..., check_env = parent.frame())
user_object_list(
mode = "any",
exclude_envir = .envir_prep,
...,
check_env = parent.frame()
)
solution_object_list(
mode = "any",
exclude_envir = .envir_prep,
...,
check_env = parent.frame()
)
x |
An object name, given as a quoted character string. |
mode |
character specifying the |
exclude_envir |
An environment.
Objects that appear in |
... |
Additional arguments passed to underlying functions:
|
check_env |
The environment from which to retrieve
|
For user_object_get()
and solution_object_get()
, the object.
If the object is not found, an error.
For user_object_exists()
and solution_object_exists()
,
a TRUE
/FALSE
value.
For user_object_list()
and solution_object_list()
,
a character vector giving the names of the objects
created by the student or solution code.
user_code <- quote({
# ```{r example}
x <- "I'm student code!"
y <- list(1, 2, 3)
z <- function() print("Hello World!")
# ```
})
solution_code <- quote({
# ```{r example-solution}
x <- "I'm solution code!"
y <- list("a", "b", "c")
z <- function() print("Goodnight Moon!")
# ```
})
exercise <- mock_this_exercise(!!user_code, !!solution_code)
with_exercise(exercise, user_object_list())
with_exercise(exercise, user_object_exists("x"))
with_exercise(exercise, user_object_get("x"))
with_exercise(exercise, solution_object_list())
with_exercise(exercise, solution_object_exists("x"))
with_exercise(exercise, solution_object_get("x"))
# Use `mode` to find only objects of a certain type ----
with_exercise(exercise, user_object_list(mode = "character"))
with_exercise(exercise, user_object_list(mode = "list"))
with_exercise(exercise, user_object_list(mode = "function"))
with_exercise(exercise, user_object_exists("x", mode = "character"))
with_exercise(exercise, user_object_exists("y", mode = "character"))
with_exercise(exercise, user_object_get("z", mode = "function"))
# By default, `user_object_list()` ignores objects created by setup chunks ----
setup_code <- rlang::expr({
# ```{r example-setup}
setup_data <- mtcars
# ```
})
setup_exercise <- mock_this_exercise(
!!user_code, !!solution_code, setup_exercise = !!setup_code
)
with_exercise(setup_exercise, user_object_list())
## You can disable this by setting `exclude_envir = NULL` ----
with_exercise(setup_exercise, user_object_list(exclude_envir = NULL))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.