when programming using dplyr, to programmatically use variables in dplyr vers from function arguments, they need to be references by {{var}}
This works well, but I would like to use lapply with the var
argument supplied in a list. This is throwing me an error. I have tried back and forth using substitute
and rlang vars like sym
but to no avail.
any suggestions? Thanks!
library(tidyverse)
tb <- tibble(a = 1:10, b = 10:1)
foo <- function(var, scalar){
tb %>% mutate(new_var = {{var}}*scalar)
}
foo(a, pi) #works
lapply(X = list(
list(sym("a"), pi),
list(substitute(b), exp(1))), FUN = function(ll) foo(var = ll$a, scalar = ll$pi) ) #err