benjaminrich / table1

78 stars 26 forks source link

Feature Request: New method of handling labels that survives tibbles #102

Closed billdenney closed 1 year ago

billdenney commented 1 year ago

Thanks for the table1 package. I use it often.

I would love to have the label settings survive going through tibbles. I often need to make various modifications to the data while working on them, and I'd typically like to have some settings applied at the beginning that survive through to the end. In this case, I'd like to set the labels at the beginning of the data management and have them survive to the end.

I see that you use the label attribute for labeling outputs, and that makes sense as other packages also use the same. I wonder if you'd consider a second method to support where there is something like a label class that could contain any other class and its only job would be to keep the attribute.

An example of what I'd like to have work is here:

library(table1)
library(tidyverse)
newmt1 <-
  mtcars %>%
  mutate(
    cyl = setLabel(cyl, "Cylinders")
  )
# label is maintained
table1(~cyl, data = newmt1)
newmt2 <-
  mtcars %>%
  group_by(disp) %>%
  mutate(
    cyl = setLabel(cyl, "Cylinders")
  ) %>%
  ungroup()
# label is lost
table1(~cyl, data = newmt2)
benjaminrich commented 1 year ago

I understand the problem, but I'm not sure I undertand the solution you are proposing in terms of implementation. If anything, it seems like something you might want to take up with the tibble package developers/maintainers (maybe you have already?).

Anyways, I think this may be a bit out of scope for table1, but you should check out the yamlet package by @bergsmat, which is designed to help preserve attributes (in addition to its other nice features) and may be what you are looking for. For instance, this works:

library(yamlet)

newmt2 <-
  mtcars %>%
  group_by(disp) %>%
  decorate('
      cyl: Cylinders
  ') %>%
  ungroup()

table1(~cyl, data = newmt2)
billdenney commented 1 year ago

The tidyverse generally doesn't maintain attributes, and while it is somewhat inconsistent, I don't think asking again is going to change behavior (see tidyverse/tibble#155).

I'll check out yamlet. And, I'm guessing that my suggestion for a new class is more than what is needed.