Skip to content

Instantly share code, notes, and snippets.

@mschmidty
Created June 3, 2025 00:37
Show Gist options
  • Select an option

  • Save mschmidty/a38951317d6cadd98a7245bb543ae742 to your computer and use it in GitHub Desktop.

Select an option

Save mschmidty/a38951317d6cadd98a7245bb543ae742 to your computer and use it in GitHub Desktop.
How to make a function param a column name in dplyr
library(dplyr)
df <- tibble(
apple = 1:3,
banana = c("x", "foo", "z"),
cherry = 4:6
)
test_cc <- function(x, col) {
filter(x, {{ col }} == "foo")
}
test_base <- function(.data, name) {
filter(.data, .data[[name]] == "foo")
}
test_sym <- function(.data, col) {
col <- ensym(col)
filter(.data, !!col == "foo")
}
df |> test_cc(banana)
df |> test_base("banana")
df |> test_sym(banana)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment