I want to use pivot_longer
within a function that takes arbitrary arguments. I tried this, which doesn't work:
tolong <- function(x, ...) {
x %>%
pivot_longer(cols = ...)
}
tolong(pheno, fev1, fvc)
Error in build_longer_spec(data, !!cols, names_to = names_to, values_to = values_to, :
object 'fvc' not found
I also tried cols = c(...)
, cols = vars(...)
and cols = list(...)
, none of which work. The only way I have found so far to get it to work is this:
tolong <- function(x, ...) {
x %>%
pivot_longer(cols = vars(...) %>% as.character %>% sub("~", "", .))
}
Surely there must be a neater way?