Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix split list for pagination when with ordered factors #235

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions R/rlistings.R
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ setMethod(
)
)

if (any(grepl("([{}])", fullmat))) {
stop(
"Labels cannot contain { or } due to their use for indicating referential footnotes.\n",
"These are not supported at the moment in {rlistings}."
)
}

MatrixPrintForm(
strings = fullmat,
spans = matrix(1,
Expand Down Expand Up @@ -452,9 +459,10 @@ add_listing_col <- function(df,
#' Each listing can only be split by variable once. If this function is applied prior to
#' pagination, parameter values will be separated by page.
#'
#' @param lsting listing_df. The listing to split.
#' @param var character. Name of the variable to split on.
#' @param page_prefix character. Prefix to be appended with the split value (`var` level),
#' @param lsting (`listing_df`)\cr the listing to split.
#' @param var (`string`)\cr name of the variable to split on. If the column is a factor,
#' the resulting list follows the order of the levels.
#' @param page_prefix (`string`)\cr prefix to be appended with the split value (`var` level),
#' at the end of the subtitles, corresponding to each resulting list element (listing).
#'
#' @return A list of `lsting_df` objects each corresponding to a unique value of `var`.
Expand Down Expand Up @@ -482,8 +490,17 @@ split_into_pages_by_var <- function(lsting, var, page_prefix = var) {
checkmate::assert_class(lsting, "listing_df")
checkmate::assert_choice(var, names(lsting))

# Pre-processing in case of factor variable
levels_or_vals <- if (is.factor(lsting[[var]])) {
lvls <- levels(lsting[[var]])
lvls[lvls %in% unique(lsting[[var]])] # Filter out missing values
} else {
unique(lsting[[var]])
}

# Main list creator (filters rows by var)
lsting_by_var <- list()
for (lvl in unique(lsting[[var]])) {
for (lvl in levels_or_vals) {
var_desc <- paste0(page_prefix, ": ", lvl)
lsting_by_var[[lvl]] <- lsting[lsting[[var]] == lvl, ]
subtitles(lsting_by_var[[lvl]]) <- c(subtitles(lsting), var_desc)
Expand Down
7 changes: 4 additions & 3 deletions man/split_into_pages_by_var.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-listings.R
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ testthat::test_that("split_into_pages_by_var works as expected", {
split_into_pages_by_var("SEX", page_prefix = "Patient Subset - Sex")

testthat::expect_equal(length(lsting), length(unique(tmp_data[["SEX"]])))
testthat::expect_equal(subtitles(lsting[[1]]), "Patient Subset - Sex: M")
testthat::expect_equal(subtitles(lsting[[1]]), "Patient Subset - Sex: F")

lsting <- as_listing(
tmp_data,
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-matrix_form.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,31 @@ testthat::test_that("matrix_form keeps relevant information and structure about
testthat::expect_equal(ncol(rlmf), ncol(rlmf$strings))
testthat::expect_false(mf_has_rlabels(rlmf))
})

test_that("matrix_form detects { or } in labels and sends meaningful error message", {
dat <- ex_adae[1:10, ]
dat$AENDY[3:6] <- "something {haha} something"
lsting <- as_listing(
dat,
key_cols = c("USUBJID"),
disp_cols = c("STUDYID", "AENDY")
)
expect_error(
matrix_form(lsting),
"Labels cannot contain"
)

# Workaround for ref_fnotes works
levels(dat$ARM)[1] <- "A: Drug X(1)"

# Generate listing
lsting <- as_listing(
df = adae,
key_cols = c("ARM"),
disp_cols = c("BMRKR1"),
main_footer = "(1) adasdasd"
)

expect_true(grepl(toString(lsting), pattern = "\\(1\\) adasdasd"))
expect_true(grepl(toString(lsting), pattern = "A: Drug X\\(1\\)"))
})
7 changes: 5 additions & 2 deletions tests/testthat/test-paginate_listing.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,14 @@ testthat::test_that("paginate_listing works with split_into_pages_by_var", {
add_listing_col("BMRKR1", format = "xx.x") %>%
split_into_pages_by_var("SEX", page_prefix = "Patient Subset - Sex")

# split keeps the order of the levels
expect_equal(names(lsting), levels(tmp_data$SEX)[seq(2)])

pag_listing <- paginate_listing(lsting, lpp = 20, cpp = 65, print_pages = FALSE)[[3]]
testthat::expect_equal(main_title(pag_listing), "title")
testthat::expect_equal(subtitles(pag_listing), "Patient Subset - Sex: F")
testthat::expect_equal(subtitles(pag_listing), "Patient Subset - Sex: M")
testthat::expect_equal(main_footer(pag_listing), "foot")
testthat::expect_true(all(pag_listing$strings[-1, 3] == "F"))
testthat::expect_true(all(pag_listing$strings[-1, 3] == "M"))
testthat::expect_snapshot(fast_print(list(pag_listing)))

# This works also for the pagination print
Expand Down
5 changes: 3 additions & 2 deletions vignettes/ref_footnotes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ df_lbls <- var_labels(adae)
# Specify order of levels with new referential footnotes added
adae <- adae %>% dplyr::mutate(
ARM = factor(
ifelse(ARM == "A: Drug X" & ASEQ %in% 1:2, paste0(ARM, "*"), as.character(ARM)),
levels = c(sapply(levels(adae$ARM), paste0, c("", "*")))
ifelse(ARM == "A: Drug X" & ASEQ %in% 1:2, paste0(ARM, " (1)"), as.character(ARM)),
levels = c(sapply(levels(adae$ARM), paste0, c("", "(1)")))
)
)

Expand All @@ -100,6 +100,7 @@ lsting <- as_listing(
df = adae,
key_cols = c("ARM", "USUBJID", "ASEQ", "ASTDY"),
disp_cols = c("BMRKR1", "AESEV"),
main_footer = "(1) ASEQ 1 or 2"
)

lsting
Expand Down
Loading