rdplyrstandard-evaluation

How to pass a string to dplyr filter in a function?


I am looking for a way to pass a string as an input to the filter_ function in dplyr package within my own function. I have set up the data frame as follows:

df = data.frame(
    X1 = LETTERS[1:5], 
    X2 = c("apple", "apple", "apple", "banana", "banana")
)

I am looking for a way to write a function in which I can pass either "apple" or "banana" to filter the data frame.

I have tried:

filterFruit = function(Data, Fruit){
   retVal = filter_(Data, "X2 == Fruit")
   return(retVal)
}

Then passing the values :

apple1 = filterFruit(df, "apple")
apple1

This would returne an error:

Error: object 'Fruit' not found

I have tried a few other ways of doing this with no success, I hope that someone could help.

Edit:

I have realised that I do not need to use filter_ for this operation as it I am not selecting which column I am filtering by and can just pass the arguments into filter with no quotes. The question still stands however for the case in which you have:

df = data.frame(
    X1 = LETTERS[1:5], 
    X2 = c("apple", "apple", "apple", "banana", "banana")
    X3 = c("apple", "banana", "apple", banana", "apple")
)

and need to decide which column (X2 or X3) that you need to filter by.


Solution

  • This should answer your edit:

    library(dplyr)
    
    df = data.frame(
      X1 = LETTERS[1:5], 
      X2 = c("apple", "apple", "apple", "banana", "banana"),
      X3 = c("apple", "banana", "apple", "banana", "apple"), 
      stringsAsFactors=FALSE
    )
    
    column_string = "X2"
    column_value = "banana"
    column_name <- rlang::sym(column_string)
    
    filtered_df <- df %>%
      filter(UQ(column_name) == UQ(column_value))
    
    filtered_df