LLAPYoda
LLAPYoda

Reputation: 11

Passing a variable name to a dplyr function within another function returns object not found error

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.

Upvotes: 1

Views: 140

Answers (1)

user17714926
user17714926

Reputation:

It looks like a visibility issue. R has pretty complicated scoping rules. To ensure you're passing correctly specify the calls as follows:

fun_1(iris, iris$Sepal.Length)
fun_2(iris, iris$Sepal.Length)

Alternatively, just pass var as a string:

library(dplyr)

fun_1 <- function(data, var) {
  data %>% summarise(mean=mean(data[[var]]))
}

fun_2 <- function(data, var) {
  fun_1(data, var)
}

fun_1(iris, 'Sepal.Length')
fun_2(iris, 'Sepal.Length')

Upvotes: 1

Related Questions