Closed mstackhouse closed 7 months ago
This is actually not a bug - but rather an issue with the order of operations and an opportunity to improve documentation.
tplyr_meta <- data %>% tplyr_table(TRT01A) %>%
set_pop_data(adsl_subset) %>%
add_total_group("Overall") %>%
set_pop_treat_var(TRT01A) %>%
set_distinct_by(USUBJID) %>%
set_count_layer_formats(n_counts = f_str("xx (xx.x)", distinct_n, distinct_pct)) %>%
add_layer(group_count(teae, by = "Participants with TEAE"))
dat <- build(tplyr_meta, metadata = TRUE)
big_Ns <- header_n(tplyr_meta)
The add_total_group()
function extracts the levels from population data at the time of call, but does all the additional pre-processing at the time of build. If add_total_group()
is called before set_pop_data()
, internally the population data is still marked as the initial target dataset, which in this case is adae
. By calling add_total_group()
after set_pop_data()
, the levels are instead pulled from adsl
and the results are correct.
4 years after release, we recognize the flaw in this design - and that's why in the concepts we're drawing up for tplyr2
, we want to move all data processing to the build stage - because the order of calling function is definitely not intuitive without understanding internals.
Tplyr - Problem with Overall N