I want to write a function that has two inputs: The name of a new variable and a mathematical expression. Both arguments come as strings.
This function should take a data.frame and add the specified new variable which should be the result of the given mathematical expression.
This is a minimal working example of what I tried:
df <- tibble(A = 1:10, B = 1:10)
new_var <- "C"
expression <- "A + B"
example_fun <- function(new_var, expression) {
new_var_sym <- sym(new_var)
expression_sym <- sym(expression)
mutate(df, !! new_var_sym := !! expression_sym)
}
example_fun(new_var, expression)
This yields the following error:
Error in mutate_impl(.data, dots) : Binding not found: A + B.
When I wrap the mutate line within the function with expr()
, I get
mutate(df, `:=`(C, `A + B`))
It seems as if the ticks around A + B
should not be there, but I could not figure out how to get rid of them. At least, enquo()
and quo_name()
did not help.