| Title: | Grade Students' Code |
|---|---|
| Description: | Provides functions for inspeacting and providing feedback for code input by students with 'gradethis'. |
| Authors: | Alexander Rossell Hayes [aut, cre] (ORCID: <https://orcid.org/0000-0001-9412-0457>), Garrick Aden-Buie [aut] (ORCID: <https://orcid.org/0000-0002-7111-0077>), RStudio, PBC [cph, fnd] |
| Maintainer: | Alexander Rossell Hayes <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.3 |
| Built: | 2026-05-07 08:38:43 UTC |
| Source: | https://github.com/rstudio/gradecode |
Return a failing grade based on the result of a find function
fail_if_not_found(gradecode_found, message = NULL, ..., env = parent.frame()) fail_if_found(gradecode_found, message = NULL, ..., env = parent.frame())fail_if_not_found(gradecode_found, message = NULL, ..., env = parent.frame()) fail_if_found(gradecode_found, message = NULL, ..., env = parent.frame())
gradecode_found |
The result of a |
message |
A character string of the message to be displayed. In all
grading helper functions other than |
... |
Arguments passed on to
|
env |
environment to evaluate the glue |
fail_if_not_found() returns a failing gradethis grade if the last
find_*() function did not find a result.
Otherwise, it returns the gradecode_found object unaltered.
fail_if_found() returns a failing gradethis grade if the last
find_*() function did not find a result.
Otherwise, it returns the gradecode_found object unaltered.
library(dplyr) .user_code <- "mtcars %>% mutate(mpg = mean(mpg))" .solution_code <- "mtcars %>% group_by(cyl) %>% summarize(mpg = mean(mpg))" find_functions(.user_code) %>% fail_if_not_found() group_by_message <- "Remember to use `group_by()` to summarize each group individually." find_functions(.user_code, match = group_by) %>% fail_if_not_found(message = group_by_message) find_functions(.solution_code, match = group_by) %>% fail_if_not_found(message = group_by_message) mutate_message <- "`mutate()` returns a value for each observation, not each group." find_functions(.user_code, match = mutate) %>% fail_if_found(message = mutate_message) find_functions(.solution_code, match = mutate) %>% fail_if_found(message = mutate_message)library(dplyr) .user_code <- "mtcars %>% mutate(mpg = mean(mpg))" .solution_code <- "mtcars %>% group_by(cyl) %>% summarize(mpg = mean(mpg))" find_functions(.user_code) %>% fail_if_not_found() group_by_message <- "Remember to use `group_by()` to summarize each group individually." find_functions(.user_code, match = group_by) %>% fail_if_not_found(message = group_by_message) find_functions(.solution_code, match = group_by) %>% fail_if_not_found(message = group_by_message) mutate_message <- "`mutate()` returns a value for each observation, not each group." find_functions(.user_code, match = mutate) %>% fail_if_found(message = mutate_message) find_functions(.solution_code, match = mutate) %>% fail_if_found(message = mutate_message)
Find the values of arguments in R code
find_argument_values( code = .user_code, match = NULL, recurse = NULL, env = parent.frame() ) uses_argument_value( code = .user_code, match = NULL, recurse = NULL, env = parent.frame() )find_argument_values( code = .user_code, match = NULL, recurse = NULL, env = parent.frame() ) uses_argument_value( code = .user_code, match = NULL, recurse = NULL, env = parent.frame() )
code |
A character string containing the code to search within.
Defaults to |
match |
What argument value to find in |
recurse |
If If
|
env |
Environment in which to find |
find_argument_values() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_argument_value() returns a TRUE/FALSE value.
.user_code <- "mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)" find_argument_values(.user_code) .user_code %>% find_functions(mean) %>% find_argument_values() .user_code %>% find_functions(rnorm) %>% find_argument_values() # If `match` is specified, `find_argument_values()` only finds that argument find_argument_values(.user_code, match = TRUE) # If `match` does not exist in the code, `find_argument_values()` returns nothing find_argument_values(.user_code, match = FALSE) # `uses_argument_value()` returns `TRUE` if `match` appears in the code uses_argument_value(.user_code, TRUE) uses_argument_value(.user_code, FALSE) .user_code %>% find_functions(mean) %>% uses_argument_value(10) .user_code %>% find_functions(rnorm) %>% uses_argument_value(10).user_code <- "mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)" find_argument_values(.user_code) .user_code %>% find_functions(mean) %>% find_argument_values() .user_code %>% find_functions(rnorm) %>% find_argument_values() # If `match` is specified, `find_argument_values()` only finds that argument find_argument_values(.user_code, match = TRUE) # If `match` does not exist in the code, `find_argument_values()` returns nothing find_argument_values(.user_code, match = FALSE) # `uses_argument_value()` returns `TRUE` if `match` appears in the code uses_argument_value(.user_code, TRUE) uses_argument_value(.user_code, FALSE) .user_code %>% find_functions(mean) %>% uses_argument_value(10) .user_code %>% find_functions(rnorm) %>% uses_argument_value(10)
Find arguments in R code
find_arguments( code = .user_code, match = NULL, recurse = NULL, env = parent.frame() ) uses_argument( code = .user_code, match = arg(), recurse = NULL, env = parent.frame() ) arg(...)find_arguments( code = .user_code, match = NULL, recurse = NULL, env = parent.frame() ) uses_argument( code = .user_code, match = arg(), recurse = NULL, env = parent.frame() ) arg(...)
code |
A character string containing the code to search within.
Defaults to |
match |
What argument to find in If If Defaults to |
recurse |
If If
|
env |
Environment in which to find |
... |
Any argument passed to |
find_arguments() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_argument() returns a TRUE/FALSE value.
.user_code <- "mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)" find_arguments(.user_code) .user_code %>% find_functions(mean) %>% find_arguments() .user_code %>% find_functions(rnorm) %>% find_arguments() # If `match` is specified, `find_arguments()` only finds that argument # Use a character string to find an argument by name (regardless of value) find_arguments(.user_code, match = "na.rm") # Use `arg()` to find an argument by name and value find_arguments(.user_code, match = arg(na.rm = TRUE)) find_arguments(.user_code, match = arg(na.rm = FALSE)) # If `match` is a character vector of length > 1, find any of those arguments find_arguments(.user_code, match = c("mean", "sd")) # If `match` is an empty string, find unnamed arguments find_arguments(.user_code, match = "") # Chained calls to `find_arguments()` can be useful for targeted feedback .user_code %>% find_arguments("na.rm") %>% fail_if_not_found("Make sure to specify an `na.rm` argument.") %>% find_arguments(arg(na.rm = TRUE)) %>% fail_if_not_found("Make sure to set the `na.rm` argument to `TRUE`.") # `uses_argument()` returns `TRUE` if `match` appears in the code .user_code %>% find_functions(mean) %>% uses_argument("na.rm") .user_code %>% find_functions(mean) %>% uses_argument(arg(na.rm = TRUE)) .user_code %>% find_functions(mean) %>% uses_argument("sd") .user_code %>% find_functions(mean) %>% uses_argument(arg(sd = 0.1)).user_code <- "mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)" find_arguments(.user_code) .user_code %>% find_functions(mean) %>% find_arguments() .user_code %>% find_functions(rnorm) %>% find_arguments() # If `match` is specified, `find_arguments()` only finds that argument # Use a character string to find an argument by name (regardless of value) find_arguments(.user_code, match = "na.rm") # Use `arg()` to find an argument by name and value find_arguments(.user_code, match = arg(na.rm = TRUE)) find_arguments(.user_code, match = arg(na.rm = FALSE)) # If `match` is a character vector of length > 1, find any of those arguments find_arguments(.user_code, match = c("mean", "sd")) # If `match` is an empty string, find unnamed arguments find_arguments(.user_code, match = "") # Chained calls to `find_arguments()` can be useful for targeted feedback .user_code %>% find_arguments("na.rm") %>% fail_if_not_found("Make sure to specify an `na.rm` argument.") %>% find_arguments(arg(na.rm = TRUE)) %>% fail_if_not_found("Make sure to set the `na.rm` argument to `TRUE`.") # `uses_argument()` returns `TRUE` if `match` appears in the code .user_code %>% find_functions(mean) %>% uses_argument("na.rm") .user_code %>% find_functions(mean) %>% uses_argument(arg(na.rm = TRUE)) .user_code %>% find_functions(mean) %>% uses_argument("sd") .user_code %>% find_functions(mean) %>% uses_argument(arg(sd = 0.1))
Find arithmetic operators in R code
find_arithmetic(code = .user_code, match = NULL, env = parent.frame()) uses_arithmetic(code = .user_code, match = NULL, env = parent.frame())find_arithmetic(code = .user_code, match = NULL, env = parent.frame()) uses_arithmetic(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which arithmetic operator to find in Either If Defaults to |
env |
Environment in which to find |
find_arithmetic() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_arithmetic() returns a TRUE/FALSE value.
Other operator functions:
find_assigns(),
find_comparisons(),
find_extractions(),
find_infixes(),
find_logical_operators(),
find_operators(),
find_pipes()
find_arithmetic("-1 + 2 * 3 %/% 4") # `match` lets you look for a specific operator find_arithmetic("-1 + 2 * 3 %/% 4", match = "+") find_arithmetic("-1 + 2 * 3 %/% 4", match = "-") find_arithmetic("-1 + 2 * 3 %/% 4", match = "%/%") # If `match` is a vector, find multiple operators find_arithmetic("-1 + 2 * 3 %/% 4", match = c("+", "-")) # It's okay if not all elements of a vector `match` are present find_arithmetic("-1 + 2 * 3 %/% 4", match = c("*", "/")) # `uses_arithmetic()` returns a TRUE or FALSE value uses_arithmetic("-1 + 2 * 3 %/% 4", match = "+") uses_arithmetic("-1 + 2 * 3 %/% 4", match = "-") uses_arithmetic("-1 + 2 * 3 %/% 4", match = "^")find_arithmetic("-1 + 2 * 3 %/% 4") # `match` lets you look for a specific operator find_arithmetic("-1 + 2 * 3 %/% 4", match = "+") find_arithmetic("-1 + 2 * 3 %/% 4", match = "-") find_arithmetic("-1 + 2 * 3 %/% 4", match = "%/%") # If `match` is a vector, find multiple operators find_arithmetic("-1 + 2 * 3 %/% 4", match = c("+", "-")) # It's okay if not all elements of a vector `match` are present find_arithmetic("-1 + 2 * 3 %/% 4", match = c("*", "/")) # `uses_arithmetic()` returns a TRUE or FALSE value uses_arithmetic("-1 + 2 * 3 %/% 4", match = "+") uses_arithmetic("-1 + 2 * 3 %/% 4", match = "-") uses_arithmetic("-1 + 2 * 3 %/% 4", match = "^")
Find assignments in R code
find_assigns(code = .user_code, match = NULL, env = parent.frame()) uses_assign(code = .user_code, match = NULL, env = parent.frame())find_assigns(code = .user_code, match = NULL, env = parent.frame()) uses_assign(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which assignment operator to find in Either If Defaults to |
env |
Environment in which to find |
find_assigns() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_assign() returns a TRUE/FALSE value.
Other operator functions:
find_arithmetic(),
find_comparisons(),
find_extractions(),
find_infixes(),
find_logical_operators(),
find_operators(),
find_pipes()
find_assigns("x <- 1; y = 2; 3 -> z") # `match` lets you look for a specific operator find_assigns("x <- 1; y = 2; 3 -> z", match = "<-") find_assigns("x <- 1; y = 2; 3 -> z", match = "=") find_assigns("x <- 1; y = 2; 3 -> z", match = "->") # If `match` is a vector, find multiple operators find_assigns("x <- 1; y = 2; 3 -> z", match = c("<-", "->")) # It's okay if not all elements of a vector `match` are present find_assigns("x <- 1; y = 2; 3 -> z", match = c("=", ":=")) # `uses_assign()` returns a TRUE or FALSE value uses_assign("3 -> z", match = "<-") uses_assign("3 -> z", match = "=") uses_assign("3 -> z", match = "->")find_assigns("x <- 1; y = 2; 3 -> z") # `match` lets you look for a specific operator find_assigns("x <- 1; y = 2; 3 -> z", match = "<-") find_assigns("x <- 1; y = 2; 3 -> z", match = "=") find_assigns("x <- 1; y = 2; 3 -> z", match = "->") # If `match` is a vector, find multiple operators find_assigns("x <- 1; y = 2; 3 -> z", match = c("<-", "->")) # It's okay if not all elements of a vector `match` are present find_assigns("x <- 1; y = 2; 3 -> z", match = c("=", ":=")) # `uses_assign()` returns a TRUE or FALSE value uses_assign("3 -> z", match = "<-") uses_assign("3 -> z", match = "=") uses_assign("3 -> z", match = "->")
Find comparison operators in R code
find_comparisons(code = .user_code, match = NULL, env = parent.frame()) uses_comparison(code = .user_code, match = NULL, env = parent.frame())find_comparisons(code = .user_code, match = NULL, env = parent.frame()) uses_comparison(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which comparison operator to find in Either If Defaults to |
env |
Environment in which to find |
find_comparisons() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_comparison() returns a TRUE/FALSE value.
Other operator functions:
find_arithmetic(),
find_assigns(),
find_extractions(),
find_infixes(),
find_logical_operators(),
find_operators(),
find_pipes()
find_comparisons("x < 1; x >= -1; x != 0") # `match` lets you look for a specific operator find_comparisons("x < 1; x >= -1; x != 0", match = "<") find_comparisons("x < 1; x >= -1; x != 0", match = ">=") find_comparisons("x < 1; x >= -1; x != 0", match = "!=") # If `match` is a vector, find multiple operators find_comparisons("x < 1; x >= -1; x != 0", match = c("<", ">=")) # It's okay if not all elements of a vector `match` are present find_comparisons("x < 1; x >= -1; x != 0", match = c("<", "<=", ">", ">=")) # `uses_comparison()` returns a TRUE or FALSE value uses_comparison("x < 1; x >= -1; x != 0", match = "<") uses_comparison("x < 1; x >= -1; x != 0", match = "!=") uses_comparison("x < 1; x >= -1; x != 0", match = ">")find_comparisons("x < 1; x >= -1; x != 0") # `match` lets you look for a specific operator find_comparisons("x < 1; x >= -1; x != 0", match = "<") find_comparisons("x < 1; x >= -1; x != 0", match = ">=") find_comparisons("x < 1; x >= -1; x != 0", match = "!=") # If `match` is a vector, find multiple operators find_comparisons("x < 1; x >= -1; x != 0", match = c("<", ">=")) # It's okay if not all elements of a vector `match` are present find_comparisons("x < 1; x >= -1; x != 0", match = c("<", "<=", ">", ">=")) # `uses_comparison()` returns a TRUE or FALSE value uses_comparison("x < 1; x >= -1; x != 0", match = "<") uses_comparison("x < 1; x >= -1; x != 0", match = "!=") uses_comparison("x < 1; x >= -1; x != 0", match = ">")
Find extraction operators in R code
find_extractions(code = .user_code, match = NULL, env = parent.frame()) uses_extraction(code = .user_code, match = NULL, env = parent.frame())find_extractions(code = .user_code, match = NULL, env = parent.frame()) uses_extraction(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which extraction operator to find in Either If Defaults to |
env |
Environment in which to find |
find_extractions() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_extraction() returns a TRUE/FALSE value.
Other operator functions:
find_arithmetic(),
find_assigns(),
find_comparisons(),
find_infixes(),
find_logical_operators(),
find_operators(),
find_pipes()
find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]") # `match` lets you look for a specific operator find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "[") find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "$") find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "[[") # If `match` is a vector, find multiple operators find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = c("[", "[[")) # It's okay if not all elements of a vector `match` are present find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = c("$", "@")) # `uses_extraction()` returns a TRUE or FALSE value uses_extraction("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "[") uses_extraction("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "[[") uses_extraction("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "@")find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]") # `match` lets you look for a specific operator find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "[") find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "$") find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "[[") # If `match` is a vector, find multiple operators find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = c("[", "[[")) # It's okay if not all elements of a vector `match` are present find_extractions("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = c("$", "@")) # `uses_extraction()` returns a TRUE or FALSE value uses_extraction("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "[") uses_extraction("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "[[") uses_extraction("mtcars[mtcars$cyl == 4, 'mpg'][[1]]", match = "@")
Find functions in R code
find_functions( code = .user_code, match = NULL, recurse = TRUE, env = parent.frame() ) uses_function(code = .user_code, match = NULL, env = parent.frame())find_functions( code = .user_code, match = NULL, recurse = TRUE, env = parent.frame() ) uses_function(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which function to find in If Defaults to Unquoted function names search by value,
e.g. Character strings search by name,
e.g. |
recurse |
If If Defaults to |
env |
Environment in which to find |
find_functions() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_function() returns a TRUE/FALSE value.
library(dplyr) .user_code <- "mtcars %>% group_by(cyl) %>% summarize(mpg = mean(mpg))" find_functions(.user_code) # If `match` is specified, `find_functions()` only finds that function find_functions(.user_code, match = group_by) # If `match` does not exist in the code, `find_functions()` returns nothing find_functions(.user_code, match = mutate) # `match = summarise` can find the function `summarize()`, # because they are two names for the same function find_functions(.user_code, match = summarise) # but `match = "summarise"` looks for an exact name, # so it can't find the function `summarize()` find_functions(.user_code, match = "summarise") # If `match` has length > 1, `find_functions()` finds any of those operators find_functions(.user_code, match = c(group_by, summarize)) # There is no issue if only some of the `match` functions exist in the code find_functions(.user_code, match = c(mean, median)) # `uses_function()` returns `TRUE` if `match` appears in the code uses_function(.user_code, match = group_by) uses_function(.user_code, match = mean) uses_function(.user_code, match = median) uses_function(.user_code, match = summarise) uses_function(.user_code, match = "summarise")library(dplyr) .user_code <- "mtcars %>% group_by(cyl) %>% summarize(mpg = mean(mpg))" find_functions(.user_code) # If `match` is specified, `find_functions()` only finds that function find_functions(.user_code, match = group_by) # If `match` does not exist in the code, `find_functions()` returns nothing find_functions(.user_code, match = mutate) # `match = summarise` can find the function `summarize()`, # because they are two names for the same function find_functions(.user_code, match = summarise) # but `match = "summarise"` looks for an exact name, # so it can't find the function `summarize()` find_functions(.user_code, match = "summarise") # If `match` has length > 1, `find_functions()` finds any of those operators find_functions(.user_code, match = c(group_by, summarize)) # There is no issue if only some of the `match` functions exist in the code find_functions(.user_code, match = c(mean, median)) # `uses_function()` returns `TRUE` if `match` appears in the code uses_function(.user_code, match = group_by) uses_function(.user_code, match = mean) uses_function(.user_code, match = median) uses_function(.user_code, match = summarise) uses_function(.user_code, match = "summarise")
Infixes are R functions that are surrounded by %s and are placed between
two arguments, like %in%, %%, or %>%.
find_infixes(code, match = NULL, env = parent.frame()) uses_infix(code = .user_code, match = NULL, env = parent.frame())find_infixes(code, match = NULL, env = parent.frame()) uses_infix(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
What infix to find in Defaults to Unquoted infixes search by value, while character strings search by name. |
env |
Environment in which to find |
find_infixes() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_infix() returns a TRUE/FALSE value.
Other operator functions:
find_arithmetic(),
find_assigns(),
find_comparisons(),
find_extractions(),
find_logical_operators(),
find_operators(),
find_pipes()
find_infixes("'mpg' %in% names(mtcars)") # `match` lets you look for a specific operator find_infixes("'mpg' %in% names(mtcars)", match = `%in%`) find_infixes("'mpg' %in% names(mtcars)", match = `%*%`) # `uses_pipe()` returns a TRUE or FALSE value uses_infix("'mpg' %in% names(mtcars)", match = `%in%`) uses_infix("'mpg' %in% names(mtcars)", match = `%*%`)find_infixes("'mpg' %in% names(mtcars)") # `match` lets you look for a specific operator find_infixes("'mpg' %in% names(mtcars)", match = `%in%`) find_infixes("'mpg' %in% names(mtcars)", match = `%*%`) # `uses_pipe()` returns a TRUE or FALSE value uses_infix("'mpg' %in% names(mtcars)", match = `%in%`) uses_infix("'mpg' %in% names(mtcars)", match = `%*%`)
find_lhs() and uses_lhs() look for the left-hand side of an operator.
find_rhs() and uses_rhs() look for the right-hand side of an operator.
find_lhs(code, env = parent.frame()) uses_lhs(code = .user_code, env = parent.frame()) find_rhs(code, env = parent.frame()) uses_rhs(code = .user_code, env = parent.frame())find_lhs(code, env = parent.frame()) uses_lhs(code = .user_code, env = parent.frame()) find_rhs(code, env = parent.frame()) uses_rhs(code = .user_code, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
env |
Environment in which to find |
find_lhs() and find_rhs() return a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_lhs() and uses_rhs() return a TRUE/FALSE value.
"rnorm(10) %>% mean(na.rm = TRUE)" %>% find_pipes() %>% find_lhs() "rnorm(10) %>% mean(na.rm = TRUE)" %>% find_pipes() %>% find_rhs() "2 ^ 5" %>% find_operators() %>% find_lhs() "2 ^ 5" %>% find_operators() %>% find_rhs() "!1" %>% find_operators() %>% uses_lhs() "!1" %>% find_operators() %>% uses_rhs()"rnorm(10) %>% mean(na.rm = TRUE)" %>% find_pipes() %>% find_lhs() "rnorm(10) %>% mean(na.rm = TRUE)" %>% find_pipes() %>% find_rhs() "2 ^ 5" %>% find_operators() %>% find_lhs() "2 ^ 5" %>% find_operators() %>% find_rhs() "!1" %>% find_operators() %>% uses_lhs() "!1" %>% find_operators() %>% uses_rhs()
Find logical operators in R code
find_logical_operators(code = .user_code, match = NULL, env = parent.frame()) uses_logical_operator(code = .user_code, match = NULL, env = parent.frame())find_logical_operators(code = .user_code, match = NULL, env = parent.frame()) uses_logical_operator(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which logical operator to find in Either If Defaults to |
env |
Environment in which to find |
find_logical_operators() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_logical_operator() returns a TRUE/FALSE value.
Other operator functions:
find_arithmetic(),
find_assigns(),
find_comparisons(),
find_extractions(),
find_infixes(),
find_operators(),
find_pipes()
find_logical_operators("FALSE | !FALSE && TRUE") # `match` lets you look for a specific operator find_logical_operators("FALSE | !FALSE && TRUE", match = "|") find_logical_operators("FALSE | !FALSE && TRUE", match = "!") find_logical_operators("FALSE | !FALSE && TRUE", match = "&&") # If `match` is a vector, find multiple operators find_logical_operators("FALSE | !FALSE && TRUE", match = c("|", "!")) # It's okay if not all elements of a vector `match` are present find_logical_operators("FALSE | !FALSE && TRUE", match = c("&", "&&")) # `uses_logical_operator()` returns a TRUE or FALSE value uses_logical_operator("FALSE | !FALSE && TRUE", match = "|") uses_logical_operator("FALSE | !FALSE && TRUE", match = "!") uses_logical_operator("FALSE | !FALSE && TRUE", match = "&")find_logical_operators("FALSE | !FALSE && TRUE") # `match` lets you look for a specific operator find_logical_operators("FALSE | !FALSE && TRUE", match = "|") find_logical_operators("FALSE | !FALSE && TRUE", match = "!") find_logical_operators("FALSE | !FALSE && TRUE", match = "&&") # If `match` is a vector, find multiple operators find_logical_operators("FALSE | !FALSE && TRUE", match = c("|", "!")) # It's okay if not all elements of a vector `match` are present find_logical_operators("FALSE | !FALSE && TRUE", match = c("&", "&&")) # `uses_logical_operator()` returns a TRUE or FALSE value uses_logical_operator("FALSE | !FALSE && TRUE", match = "|") uses_logical_operator("FALSE | !FALSE && TRUE", match = "!") uses_logical_operator("FALSE | !FALSE && TRUE", match = "&")
Find objects in R code
find_objects(code = .user_code, match = NULL, env = parent.frame()) uses_object(code = .user_code, match = NULL, env = parent.frame())find_objects(code = .user_code, match = NULL, env = parent.frame()) uses_object(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which object to find in Defaults to Unquoted object names search by value,
e.g. Character strings search by name,
e.g. |
env |
Environment in which to find |
find_objects() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_object() returns a TRUE/FALSE value.
library(dplyr) .user_code <- "band_members %>% full_join(band_instruments)" find_objects(.user_code) # If `match` is specified, `find_objects()` only finds that object find_objects(.user_code, match = band_members) # If `match` does not exist in the code, `find_objects()` returns nothing find_objects(.user_code, match = storms) # `match = dplyr::band_members` can find the object `band_members`, # because they are two ways of referring to the same object find_objects(.user_code, match = dplyr::band_members) # but `match = "dplyr::band_members"` looks for an exact name, # so it can't find the object `band_members` find_objects(.user_code, match = "dplyr::band_members") # `uses_function()` returns `TRUE` if `match` appears in the code uses_object(.user_code, match = band_members) uses_object(.user_code, match = band_instruments) uses_object(.user_code, match = storms) uses_object(.user_code, match = dplyr::band_members) uses_object(.user_code, match = "dplyr::band_members")library(dplyr) .user_code <- "band_members %>% full_join(band_instruments)" find_objects(.user_code) # If `match` is specified, `find_objects()` only finds that object find_objects(.user_code, match = band_members) # If `match` does not exist in the code, `find_objects()` returns nothing find_objects(.user_code, match = storms) # `match = dplyr::band_members` can find the object `band_members`, # because they are two ways of referring to the same object find_objects(.user_code, match = dplyr::band_members) # but `match = "dplyr::band_members"` looks for an exact name, # so it can't find the object `band_members` find_objects(.user_code, match = "dplyr::band_members") # `uses_function()` returns `TRUE` if `match` appears in the code uses_object(.user_code, match = band_members) uses_object(.user_code, match = band_instruments) uses_object(.user_code, match = storms) uses_object(.user_code, match = dplyr::band_members) uses_object(.user_code, match = "dplyr::band_members")
Operators are R functions that are placed between two arguments,
like +, &&, >= or %in%,
or that are placed directly in front of an argument,
like -, ! or ?.
find_operators(code = .user_code, match = NULL, env = parent.frame()) uses_operator(code = .user_code, match = NULL, env = parent.frame())find_operators(code = .user_code, match = NULL, env = parent.frame()) uses_operator(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which operator to find in Either If Defaults to |
env |
Environment in which to find |
find_operators() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_operator() returns a TRUE/FALSE value.
Other operator functions:
find_arithmetic(),
find_assigns(),
find_comparisons(),
find_extractions(),
find_infixes(),
find_logical_operators(),
find_pipes()
# By default, `find_operators()` finds all operators in the code find_operators("-1 + 2 * 3 %/% 4") find_operators("x <- 1; y = 2; 3 -> z") find_operators("x < 1; x >= -1; x != 0") find_operators("mtcars[mtcars$cyl == 4, 'mpg'][[1]]") find_operators("FALSE | !FALSE && TRUE") .user_code <- "3 ^ 4 > 4 ^ 3 && -1 + -1 < -1 * -1" find_operators(.user_code) # If `match` is specified, `find_operators()` only finds that operator find_operators(.user_code, match = "-") # If `match` does not exist in the code, `find_operators()` returns nothing find_operators(.user_code, match = "/") # If `match` has length > 1, `find_operators()` finds any of those operators find_operators(.user_code, match = c("<", ">")) # There is no issue if only some of the `match` operators exist in the code find_operators(.user_code, match = c("*", "/")) # `uses_operator()` returns `TRUE` if `match` appears in the code uses_operator(.user_code, match = "&&") # and `FALSE` if it does not uses_operator(.user_code, match = "&")# By default, `find_operators()` finds all operators in the code find_operators("-1 + 2 * 3 %/% 4") find_operators("x <- 1; y = 2; 3 -> z") find_operators("x < 1; x >= -1; x != 0") find_operators("mtcars[mtcars$cyl == 4, 'mpg'][[1]]") find_operators("FALSE | !FALSE && TRUE") .user_code <- "3 ^ 4 > 4 ^ 3 && -1 + -1 < -1 * -1" find_operators(.user_code) # If `match` is specified, `find_operators()` only finds that operator find_operators(.user_code, match = "-") # If `match` does not exist in the code, `find_operators()` returns nothing find_operators(.user_code, match = "/") # If `match` has length > 1, `find_operators()` finds any of those operators find_operators(.user_code, match = c("<", ">")) # There is no issue if only some of the `match` operators exist in the code find_operators(.user_code, match = c("*", "/")) # `uses_operator()` returns `TRUE` if `match` appears in the code uses_operator(.user_code, match = "&&") # and `FALSE` if it does not uses_operator(.user_code, match = "&")
Find the parent of an expression in R code
find_parent(code, env = parent.frame())find_parent(code, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
env |
Environment in which to find |
find_parent() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
.user_code <- "mtcars %>% mutate(kg = wt * 1000 / 2.2) %>% summarize(avg_kg = mean(kg, na.rm = TRUE))" # The parent of an argument is the call containing the argument .user_code %>% find_arguments(arg(na.rm = TRUE)) %>% find_parent() # The parent of a function is the call containing the function and its arguments .user_code %>% find_functions(mean) %>% find_parent() .user_code %>% find_functions("summarize") %>% find_parent() # The parent of an operator is the left- and right-hand side of the operator .user_code %>% find_operators("*") %>% find_parent() # But remember that, based on order of operations, # an operator's parent may contain more than you expect .user_code %>% find_operators("/") %>% find_parent() # The same applies to pipes .user_code %>% find_pipes() %>% find_parent().user_code <- "mtcars %>% mutate(kg = wt * 1000 / 2.2) %>% summarize(avg_kg = mean(kg, na.rm = TRUE))" # The parent of an argument is the call containing the argument .user_code %>% find_arguments(arg(na.rm = TRUE)) %>% find_parent() # The parent of a function is the call containing the function and its arguments .user_code %>% find_functions(mean) %>% find_parent() .user_code %>% find_functions("summarize") %>% find_parent() # The parent of an operator is the left- and right-hand side of the operator .user_code %>% find_operators("*") %>% find_parent() # But remember that, based on order of operations, # an operator's parent may contain more than you expect .user_code %>% find_operators("/") %>% find_parent() # The same applies to pipes .user_code %>% find_pipes() %>% find_parent()
Find pipes in R code
find_pipes(code = .user_code, match = NULL, env = parent.frame()) uses_pipe(code = .user_code, match = NULL, env = parent.frame())find_pipes(code = .user_code, match = NULL, env = parent.frame()) uses_pipe(code = .user_code, match = NULL, env = parent.frame())
code |
A character string containing the code to search within.
Defaults to |
match |
Which pipe operator to find in Either If Defaults to |
env |
Environment in which to find |
find_pipes() returns a gradecode_found object,
which can be passed to another find_*() function
or to fail_if_not_found().
uses_pipe() returns a TRUE/FALSE value.
Other operator functions:
find_arithmetic(),
find_assigns(),
find_comparisons(),
find_extractions(),
find_infixes(),
find_logical_operators(),
find_operators()
find_pipes("10 %>% rnorm() |> mean()") # `match` lets you look for a specific operator find_pipes("10 %>% rnorm() |> mean()", match = "%>%") find_pipes("10 %>% rnorm() |> mean()", match = "|>") # `uses_pipe()` returns a TRUE or FALSE value uses_pipe("10 %>% rnorm() |> mean()") uses_pipe("10 %>% rnorm() |> mean()", match = "%>%") uses_pipe("10 %>% rnorm() |> mean()", match = "|>")find_pipes("10 %>% rnorm() |> mean()") # `match` lets you look for a specific operator find_pipes("10 %>% rnorm() |> mean()", match = "%>%") find_pipes("10 %>% rnorm() |> mean()", match = "|>") # `uses_pipe()` returns a TRUE or FALSE value uses_pipe("10 %>% rnorm() |> mean()") uses_pipe("10 %>% rnorm() |> mean()", match = "%>%") uses_pipe("10 %>% rnorm() |> mean()", match = "|>")
Standardize arguments to function calls
standardize_arguments( code, recurse = TRUE, add_defaults = TRUE, env = parent.frame() )standardize_arguments( code, recurse = TRUE, add_defaults = TRUE, env = parent.frame() )
code |
The code to standardize. Either a character string, a call, or a list of character strings or calls. |
recurse |
If |
add_defaults |
If |
env |
Environment in which to find function definitions and gradethis placeholder objects. Most users will not need to use this argument. |
An object of the same type as code.
.user_code <- "mean(rnorm(10))" standardize_arguments(.user_code) standardize_arguments(.user_code, recurse = FALSE) standardize_arguments(.user_code, add_defaults = FALSE).user_code <- "mean(rnorm(10))" standardize_arguments(.user_code) standardize_arguments(.user_code, recurse = FALSE) standardize_arguments(.user_code, add_defaults = FALSE)