17.5 Customizing evaluations with functions
- Can also bind names to functions in supplied environment
- Allows overriding function behaviour
- This is how dplyr generates SQL for working with databases
For example…
string_math <- function(x) {
e <- env(
caller_env(),
`+` = function(x, y) paste(x, y),
`*` = function(x, y) strrep(x, y)
)
eval(enexpr(x), e)
}
cohort <- 9
string_math("Hello" + "cohort" + cohort)
#> [1] "Hello cohort 9"
string_math(("dslc" + "is" + "awesome---") * cohort)
#> [1] "dslc is awesome---dslc is awesome---dslc is awesome---dslc is awesome---dslc is awesome---dslc is awesome---dslc is awesome---dslc is awesome---dslc is awesome---"