When passing a variable name within a function to another function which has some dplyr code, I get a object not found error message. This only happens when I try to pass the variable name within a function, I can run the the dplyr function directly without problems.
library(dplyr)
fun_1 <- function(data, var){
return(data %>%
summarise(mean = mean ({{var}}))
)
}
fun_2 <- function(data, var){
output <- fun_1(data = data,
var = var)
return(output)
}
Running the first function directly works fine, but when trying to pass a variable name within another function to dpylr I get an error message.
fun_1(iris, Sepal.Length)
mean
1 5.843333
fun_2(iris, Sepal.Length)
Error: Problem with `summarise()` column `mean`.
i `mean = mean(var)`.
x object 'Sepal.Length' not found
I try to unterstand why this happens and how I can pass data and variable names to a dyplr function within a function.