insightsengineering / rlistings

Value formatting and ASCII rendering infrastructure for tables and listings.
https://insightsengineering.github.io/rlistings/
22 stars 5 forks source link

the column names (cols 2, 5, 6) appear in the wrong order (contents of columns are in the correct places) #8

Closed shajoezhu closed 2 years ago

shajoezhu commented 2 years ago

Table design from DSL01 design template. Formatting changes applied to initial design.

@shajoezhu when this listing is created, the column names (cols 2, 5, 6) appear in the wrong order (contents of columns are in the correct places).

Listing Code:

library(scda)
library(dplyr)
library(rlistings)

adsl <- synthetic_cdisc_data("rcd_2022_06_27")$adsl

out <- adsl %>%
  filter(AEWITHFL == 'Y') %>%
  mutate(ID = paste(SITEID, SUBJID, sep = "/"),
         ASR = paste(AGE, SEX, RACE, sep = "/"),
         DISCONT = ifelse(!is.na(DCSREAS) & EOSSTT != 'COMPLETED', "Yes", 'No'),
         SSADM = as.POSIXct(
           strftime(TRTSDTM, format = "%Y-%m-%d %H:%M:%S"),
           format = "%Y-%m-%d",
           tz = "UTC")
  ) %>%
  select(ID, ASR, TRT01A, SSADM, EOSDY, DISCONT)

var_labels(out) <- c(
  ID = "Center/Patient ID",
  ASR = "Age/Sex/Race",
  DISCONT = "Discontinued Early from Study?",
  SSADM = "Date of First Study Drug Administration",
  TRT01A = "Treatment",
  EOSDY = "Study Day of Withdrawal"
)

lsting <- as_listing(
  out,
  cols = names(out)
)

head(lsting, n = 10)

Output Listing:

Center/Patient ID            Age/Sex/Race            Discontinued Early from Study?   Date of First Study Drug Administration   Treatment   Study Day of Withdrawal
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
BRA-1/id-42         36/M/BLACK OR AFRICAN AMERICAN             A: Drug X                            2020-01-01                     NA                 No           
BRA-11/id-237                 64/F/ASIAN                     C: Combination                         2020-03-10                     731                No           
CHN-1/id-26                   29/M/WHITE                       A: Drug X                            2020-07-16                     731                No           
CHN-1/id-62                   36/F/WHITE                       A: Drug X                            2020-11-22                     90                 Yes          
CHN-11/id-256                 23/M/ASIAN                       A: Drug X                            2020-03-17                     731                No           
CHN-11/id-257                 32/F/ASIAN                     C: Combination                         2020-11-15                     731                No           
CHN-11/id-263                 34/F/ASIAN                     C: Combination                         2019-04-03                     731                No           
CHN-11/id-349                 40/M/ASIAN                     C: Combination                         2020-11-13                     731                No           
CHN-11/id-91        44/M/BLACK OR AFRICAN AMERICAN           C: Combination                         2020-05-29                     260                Yes          
CHN-13/id-102                 37/M/ASIAN                       B: Placebo                           2019-03-23                     731                No           

Originally posted by @edelarua in https://github.com/insightsengineering/sme-tasks/issues/327#issuecomment-1203158410

gmbecker commented 2 years ago

The issue is actually not with rlistings per se (though there were other problems I had to fix to run this code, so I'm not really sure how you were doing it, I suspect you have an (possibly very) out of date copy of formatters installed wherever you were trying this.

That said, the issue is actually that var_labels<- is purely positional, and you're giving the labels in an order other than the one the columns appear in.

So rlistings is actually doing the fully correct thing, using the column labels formatters incorrectly placed. I will fix this in formatters.

shajoezhu commented 2 years ago

I see! Thanks for the explanation. I understand, I was wondering which way do you think is a sensible work flow

  1. sort the column order, then call the var_labels setter, or
  2. call the var_labels setter, and sort the column order
gmbecker commented 2 years ago

After the fix to formatters (ie formatters >= 0.3.2.5) var_labels no longer needs to be in the same order as the df columns, and var_label order has not and never will dictate order of the columns, so it should be irrelevant if you set them before or after sorting the columns.

On Mon, Aug 15, 2022 at 6:21 PM Joe Zhu @.***> wrote:

I see! Thanks for the explanation. I understand, I was wondering which way do you think is a sensible work flow

  1. sort the column order, then call the var_labels setter, or
  2. call the var_labels setter, and sort the column order

— Reply to this email directly, view it on GitHub https://github.com/insightsengineering/rlistings/issues/8#issuecomment-1216039063, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG53MLMNJMEPJHKS5IEMUDVZLUJ7ANCNFSM56MCJWKA . You are receiving this because you modified the open/close state.Message ID: @.***>