Intro Thoughts
Status Quo
library(tidyverse)
tinypivot <- function(data = data.frame(), rows = NULL, cols = NULL){
# create s3
tp <- list(data = data,
rows = rlang::enquo(rows),
cols = {{cols}})
# declare to be new class
class(tp) <- "tp"
# define print method
print.tp <- function(data, rows, cols){
data %>%
group_by(all_of(c({{rows}}, {{cols}}))) %>%
count() ->
out
if(!is.null(cols)){
out %>%
pivot_wider(id_cols = {{rows}},
names_from == {{cols}},
values_from = n) ->
out
}
print(out)
}
tp
invisible(tp)
}
tinypivot()
tinypivot() |>
str()
## List of 3
## $ data:'data.frame': 0 obs. of 0 variables
## $ rows: language ~NULL
## ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
## $ cols: NULL
## - attr(*, "class")= chr "tp"
Experiment