Skip to content

Commit

Permalink
Ensure empty levels are represented when ref_Group set. closes #323.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbecker committed May 19, 2022
1 parent cdf6fac commit 8838c7d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion R/tt_dotabulation.R
Expand Up @@ -1117,7 +1117,11 @@ setMethod("set_def_child_ord", "VarLevWBaselineSplit",
vals = sort(unlist(value_names(pinfo$values)))
} else {
vec = df[[spl_payload(lyt)]]
vals = unique(vec)
vals <- if(is.factor(vec))
levels(vec)
else
unique(vec)

if(is.factor(vals))
vals = levels(relevel(droplevels(vals), bline)) # this sorts the levels
}
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-lyt-tabulation.R
Expand Up @@ -963,3 +963,22 @@ test_that("cut functions work", {


})

## https://github.com/Roche/rtables/issues/323

test_that("empty factor levels represented correctly when ref group is set", {

df <- data.frame(
val = 1:10,
grp = factor(rep("a", 10), levels = c("a", "b"))
)


tbl <- basic_table() %>%
split_cols_by("grp", ref_group = "a") %>%
analyze("val") %>%
build_table(df)

expect_identical(ncol(tbl), 2L)

})

0 comments on commit 8838c7d

Please sign in to comment.