rtables icon indicating copy to clipboard operation
rtables copied to clipboard

rtables - cell numerical (fraction) output alignment

Open wwojciech opened this issue 4 years ago • 2 comments

Hi @gmbecker, @anajens

How feasible would be to improve numerical output alignment in a cell? For example

df <- data.frame(
  USUBJID = as.character(c(1:15, 1)),
  ARM = factor(c("A", "B", "B", "A", "A", "A", "B", "A", "B", "B", "B", "B", "B", "B", "B", "B"), 
               levels = c("A", "B")),
  AETOXGR = factor(c(1, 1, 2, 3, 4, 1, 2, 5, 4, 4, 4, 4, 4, 4, 4, 4), levels = c(1:5)),
  stringsAsFactors = FALSE
)

basic_table() %>%
  split_cols_by("ARM") %>%
  summarize_occurrences_by_grade(var = "AETOXGR") %>%
  build_table(df)

       A          B    
-----------------------
1   2 (40%)   1 (9.1%) 
2      0      2 (18.2%)
3   1 (20%)       0    
4   1 (20%)   8 (72.7%)
5   1 (20%)       0  

It think this below formatting would be more nicer (note the alignment to the . for the percentage:

       A          B    
-----------------------
1   2 (40%)   1 ( 9.1%) 
2   0         2 (18.2%)
3   1 (20%)   0    
4   1 (20%)   8 (72.7%)
5   1 (20%)   0  

In other words, the numbers should be aligned according to their significance digits, i.e.

120
 22
 30

wwojciech avatar Jul 19 '21 11:07 wwojciech

The short answer is ... medium hard. It is on the list of future features (in particular, ones @waddella intends to put in, as most of the formatting stuff has historically been his domain) but its not slotted for immediate work/priority currently.

We agree that it would be nice and will be better one it does that, though

gmbecker avatar Jul 19 '21 18:07 gmbecker

Thank you @gmbecker

wwojciech avatar Jul 19 '21 22:07 wwojciech

I tried to do it w/o adding new formats and only with rtables. I think the output with left alignment is decent enough. If it is fine I would close this issue @gmbecker @shajoezhu

basic_table() %>%
  split_cols_by("ARM") %>%
  analyze("AETOXGR", afun = function(x) {
    a <- as.list(table(x))
    irl <- lapply(a, function(x) {
      if (x > 0)
        c(x, x / sum(unlist(a)))
      else
        x
    })
    irf <- lapply(a, function(x) ifelse(x > 0, "xx (xx.xx%)", "xx"))
    in_rows(.list = irl, .formats = irf, .aligns = "left")
    
  }) %>% 
  build_table(df)
        A            B     
———————————————————————————
1   2 (40.00%)   1 (9.09%) 
2   0            2 (18.18%)
3   1 (20.00%)   0         
4   1 (20.00%)   8 (72.73%)
5   1 (20.00%)   0         

Melkiades avatar Apr 17 '23 13:04 Melkiades

Indeed, I think the choice between left and the newly under development decimal alignments are sufficient to close this issue. Significant digits, which he mentions but are nto actually used in his example, are a separate issue but one we already have an issue for.

gmbecker avatar Apr 17 '23 20:04 gmbecker