Open andrie opened 5 years ago
@hadley suggests we make it possible to provide R code as the value for initial
, and provides this sample code to parse the AST:
library(rlang)
parsonize <- function(x) {
x <- enexpr(x)
if (is.character(x)) {
# Literal character string, so split up into lines, using glue's
# triming algorithm to remove leading whitespace
strsplit(glue::trim(x), "\n")[[1]]
} else if (is.call(x)) {
depipe(x)
} else {
abort("`x` must be a string or a function call.")
}
}
# Returns a character vector
depipe <- function(x) {
if (is_call(x, "+", 2) || is_call(x, "%>%", 2)) {
c(depipe(x[[2]]), depipe(x[[3]]))
} else {
expr_text(x)
}
}
parsonize("
a +
b +
c
")
parsonize(a + b + c)
parsonize(a %>% b %>% c(d + e))
Intent: use a similar API to the
grader
project, as described in https://github.com/rstudio-education/grader/issues/11In a nutshell, you should be able to write:
initial
contains the initial valuepass_if()
fail_if()
For both
pass_if()
andfail_if()
, support:function(x)...
~. == ...