I am passing a tibble to a user-defined function where column names are variables. After studying this, this, and this, I came up with the below working function. My goal is to include an equivalent function in an R package. My question, while this function works, is there a more correct best practice within the dplyr/tidyeval/tidyverse world?
library(tidyverse)
dat0 <- tibble( a = seq(as.Date('2022-02-10'), as.Date('2022-03-01'), by = "5 days")
, b = seq(10,40,10))
myCalc <- function(data, dateIn, numIn, yearOut, numOut) {
data <- data %>%
mutate(.
, {{yearOut}} := lubridate::year(.data[[dateIn]])
, {{numOut}} := 10 * .data[[numIn]]
) %>%
filter(.
, .data[[numOut]] > 250
)
}
dat2 <- myCalc(dat0
, dateIn = "a"
, numIn = "b"
, yearOut = "c"
, numOut = "d")
dat2
# A tibble: 2 × 4
a b c d
<date> <dbl> <dbl> <dbl>
1 2022-02-20 30 2022 300
2 2022-02-25 40 2022 400