Open EA-Ammar opened 4 months ago
If it is too much effort to rework tabyl to use tidyselect syntax an alternative would be to implement a formula method.
library(dplyr)
library(janitor)
tabyl.formula <- function(dat, data, ..., envir = parent.frame()) {
if (missing(data)) data <- as.data.frame(mget(all.vars(dat), envir))
vars <- names(model.frame(dat, data))
tabyl |> do.call(c(list(data), lapply(vars, as.name), list(...)))
}
Then we could do things like this:
# test
mydat %>% tabyl(reformulate(vec2), .)
mydat %>% select(any_of(vec2)) %>% tabyl(~., .)
The above uses the do.call code form https://stackoverflow.com/questions/78483724/r-pass-character-vector-of-column-names-to-function-which-can-optionally-take-m?noredirect=1#comment138367271_78483724
Feature requests
It would be useful if
tabyl()
accepted a character vector of column names as the variables to be summarised, similar to manytidyselect()
functions.An example of how I imagine the code would work:
Currently
tabyl
only accepts the first element of the character vector as a parameter, so the tables are always one-way. Allowing the vector to be fed in with either 1, 2 or 3 arguments would allow a more flexible workflow, e.g. in custom functions integratingtabyl
.