Short version:
Can I make this:df %>% select({{datecol}}) %>% pull() shorter in a function without quoting (so not df[["datecol"]]?
Long version:
I need to select a column in a function without quoting to fit in with an existing function. I am writing some error checking in to the function and am currently using {{ then pulling the column. Is there a better way of doing this? I am aware that I could use things like dataset[[date_col]] if I quoted the column.
library(lubridate)
library(dplyr)
# test data
df <- tibble(date = c(ymd("20210101"), ymd("20210102")),
date2 = c("20210101", "20210102"))
check_date <- function(dataset, date_col){
# need more efficient way to get the column here
if (!is.Date(dataset %>%
select({{date_col}}) %>%
pull())
){
rlang::abort(paste0("STOP!!!!")) # change later
}
dataset # output - not important here, but things get done in the function to the initial dataset.
}
check_date(df, date) # correct column
check_date(df, date2) # should generate an error