Borrowing from another question's answer:
library(dplyr)
df <- data.frame(
color = c("blue", "black", "blue", "blue", "black"),
value = 1:5)
The typically presented example takes the form of ...
# As of dplyr 0.7, new functions were introduced to simplify the situation
col_name <- quo(color)
df %>% filter((!!col_name) == val)
# Remember to use enquo within a function
filter_col <- function(df, col_name, val){
col_name <- enquo(col_name)
df %>% filter((!!col_name) == val)
}
filter_col(df, color, 'blue')
... but what if you wanted to have the name of the color column specified by a string?
E.g.
column_name <- "color"
col_name <- quo(column_name) # <- something else goes here
df %>% filter((!!col_name) == "blue")
Or call a function with a signature like, filter_col(df, "color", "blue")
?