diff --git a/R/00tabletrees.R b/R/00tabletrees.R index 6fbc709ac..26eac6a83 100644 --- a/R/00tabletrees.R +++ b/R/00tabletrees.R @@ -1107,7 +1107,7 @@ setClass("LayoutAxisLeaf", representation( func = "function", display_columncounts = "logical", - columncount_format = "FormatSpec", #character", + columncount_format = "FormatSpec", # character", col_footnotes = "list", column_count = "integer" ) @@ -1117,7 +1117,7 @@ setClass("LayoutColTree", contains = "LayoutAxisTree", representation( display_columncounts = "logical", - columncount_format = "FormatSpec", #"character", + columncount_format = "FormatSpec", # "character", col_footnotes = "list", column_count = "integer" ) @@ -1803,7 +1803,7 @@ setClass("PreDataColLayout", contains = "PreDataAxisLayout", representation( display_columncounts = "logical", - columncount_format = "FormatSpec" #"character" + columncount_format = "FormatSpec" # "character" ) ) diff --git a/R/colby_constructors.R b/R/colby_constructors.R index 1e163b6a0..e1050b9fb 100644 --- a/R/colby_constructors.R +++ b/R/colby_constructors.R @@ -2061,7 +2061,7 @@ basic_table <- function(title = "", subtitles = character(), main_footer = character(), prov_footer = character(), - show_colcounts = NA, #FALSE, + show_colcounts = NA, # FALSE, colcount_format = "(N=xx)", header_section_div = NA_character_, top_level_section_div = NA_character_, diff --git a/R/make_split_fun.R b/R/make_split_fun.R index 877e68534..32794b2bb 100644 --- a/R/make_split_fun.R +++ b/R/make_split_fun.R @@ -346,8 +346,10 @@ add_combo_facet <- function(name, label = name, levels, extra = list()) { } - val <- LevelComboSplitValue(val = name, extr = extra, combolevels = levels, label = label, - sub_expr = subexpr) + val <- LevelComboSplitValue( + val = name, extr = extra, combolevels = levels, label = label, + sub_expr = subexpr + ) add_to_split_result(ret, values = list(val), labels = label, datasplit = datpart diff --git a/R/make_subset_expr.R b/R/make_subset_expr.R index 120f21b8d..0bd589bfe 100644 --- a/R/make_subset_expr.R +++ b/R/make_subset_expr.R @@ -224,8 +224,9 @@ create_colinfo <- function(lyt, df, rtpos = TreePos(), cc_format <- colcount_format(lyt) %||% "(N=xx)" ## do it this way for full backwards compatibility - if (is.null(alt_counts_df)) + if (is.null(alt_counts_df)) { alt_counts_df <- df + } ctree <- coltree(clayout, df = df, rtpos = rtpos, alt_counts_df = alt_counts_df, ccount_format = cc_format) if (!is.na(disp_ccounts(lyt))) { leaf_pths <- make_col_df(ctree, visible_only = TRUE, na_str = "", ccount_format = cc_format)$path @@ -253,7 +254,7 @@ create_colinfo <- function(lyt, df, rtpos = TreePos(), } counts_df_name <- "alt_counts_df" - if (identical(alt_counts_df, df)) { #is.null(alt_counts_df)) { + if (identical(alt_counts_df, df)) { # is.null(alt_counts_df)) { alt_counts_df <- df counts_df_name <- "df" } diff --git a/R/tree_accessors.R b/R/tree_accessors.R index 91c887a03..f380868c6 100644 --- a/R/tree_accessors.R +++ b/R/tree_accessors.R @@ -2268,7 +2268,8 @@ setMethod( all_paths <- make_col_df(obj, visible_only = TRUE)$path } if (length(value) != length(all_paths)) { - stop("Got ", length(value), "values for ", + stop( + "Got ", length(value), "values for ", length(all_paths), " column paths", if (is.null(path)) " (from path = NULL)", "." @@ -2452,8 +2453,10 @@ match_path_by_pos <- function(kidlst, path) { kidlst, function(kd) { pos <- tree_pos(kd) - c(obj_name(tail(pos_splits(pos), 1)[[1]]), - value_names(tail(pos_splvals(pos), 1))[[1]]) + c( + obj_name(tail(pos_splits(pos), 1)[[1]]), + value_names(tail(pos_splvals(pos), 1))[[1]] + ) } ) @@ -2464,16 +2467,16 @@ match_path_by_pos <- function(kidlst, path) { }, TRUE ) - if (any(matches)) + if (any(matches)) { ret <- which(matches) + } ret - } ## this is a horrible hack but when we have non-nested siblings at the top level ## the beginning of the "path <-> position" relationship breaks down. ## we probably *should* have e.g., c("root", "top_level_splname_1", -##"top_level_splname_1, "top_level_splname_1_value", ...) +## "top_level_splname_1, "top_level_splname_1_value", ...) ## but its pretty clear why no one will be happy with that, I think ## so we punt on the problem for now with an explicit workaround ## @@ -2483,12 +2486,13 @@ root_match_finder <- function(kidlst, path) { matches <- vapply(kidlst, function(kid) { obj_name(kid) == path[1] }, TRUE) - if (sum(matches) == 0) + if (sum(matches) == 0) { stop("unable to find first-step match in path: ", path[1]) - else if (sum(matches) > 1) + } else if (sum(matches) > 1) { stop("multiple matches for first-step in path: ", path[1]) - else + } else { which(matches) + } } @@ -2507,28 +2511,32 @@ pos_singleton_path <- function(obj) { ## close to a duplicate of tt_at_path, but... not quite :( #' @rdname int_methods coltree_at_path <- function(obj, path, ...) { - if (length(path) == 0) + if (length(path) == 0) { return(obj) + } stopifnot( is(path, "character"), length(path) > 0 ) - if (any(grepl("@content", path, fixed = TRUE))) + if (any(grepl("@content", path, fixed = TRUE))) { stop("@content token is not valid for column paths.") + } ## if(obj_name(obj) == path[1]) { ## path <- path[-1] ## } cur <- obj - curpath <- pos_to_path(tree_pos(obj)) #path + curpath <- pos_to_path(tree_pos(obj)) # path num_consume_path <- 2 - while (!identical(curpath, path) && !is(cur, "LayoutColLeaf")) { #length(curpath) > 0) { + while (!identical(curpath, path) && !is(cur, "LayoutColLeaf")) { # length(curpath) > 0) { kids <- tree_children(cur) kidmatch <- find_kid_path_match(kids, path) if (length(kidmatch) == 0) { - stop("unable to match full path: ", paste(path, sep = "->"), - "\n path of last match: ", paste(curpath, sep = "->")) + stop( + "unable to match full path: ", paste(path, sep = "->"), + "\n path of last match: ", paste(curpath, sep = "->") + ) } cur <- kids[[kidmatch]] curpath <- pos_to_path(tree_pos(cur)) @@ -2537,8 +2545,9 @@ coltree_at_path <- function(obj, path, ...) { } find_kid_path_match <- function(kids, path) { - if (length(kids) == 0) + if (length(kids) == 0) { return(integer()) + } kidpaths <- lapply(kids, function(k) pos_to_path(tree_pos(k))) matches <- vapply(kidpaths, function(kpth) identical(path[seq_along(kpth)], kpth), NA) @@ -2556,8 +2565,10 @@ ct_recursive_replace <- function(ctree, path, value, pos = 1) { if (identical(path, curpth)) { return(value) } else if (is(ctree, "LayoutColLeaf")) { - stop("unable to match full path: ", paste(path, sep = "->"), - "\n path at leaf: ", paste(curpth, sep = "->")) + stop( + "unable to match full path: ", paste(path, sep = "->"), + "\n path at leaf: ", paste(curpth, sep = "->") + ) } kids <- tree_children(ctree) kids_singl <- pos_singleton_path(kids[[1]]) @@ -2566,12 +2577,16 @@ ct_recursive_replace <- function(ctree, path, value, pos = 1) { if (length(kidind) == 0) { stop("Path appears invalid for this tree at step ", path[1]) } else if (length(kidind) > 1) { - stop("singleton step (root, cbind_root, etc) in path appears to have matched multiple children. ", - "This shouldn't happen, please contact the maintainers.") + stop( + "singleton step (root, cbind_root, etc) in path appears to have matched multiple children. ", + "This shouldn't happen, please contact the maintainers." + ) } - kids[[kidind]] <- ct_recursive_replace(kids[[kidind]], - path, value) + kids[[kidind]] <- ct_recursive_replace( + kids[[kidind]], + path, value + ) tree_children(ctree) <- kids ctree } @@ -2631,8 +2646,10 @@ ct_recursive_replace <- function(ctree, path, value, pos = 1) { #' @examples #' lyt <- basic_table() %>% #' split_cols_by("ARM", show_colcounts = TRUE) %>% -#' split_cols_by("SEX", split_fun = keep_split_levels(c("F", "M")), -#' show_colcounts = TRUE) %>% +#' split_cols_by("SEX", +#' split_fun = keep_split_levels(c("F", "M")), +#' show_colcounts = TRUE +#' ) %>% #' split_cols_by("STRATA1", show_colcounts = TRUE) %>% #' analyze("AGE") #' @@ -2648,12 +2665,15 @@ ct_recursive_replace <- function(ctree, path, value, pos = 1) { #' ## show black space for certain counts by assign NA #' #' facet_colcount(tbl, c("ARM", "A: Drug X", "SEX", "F", "STRATA1", "C")) <- NA -setGeneric("facet_colcount", - function(obj, path) standardGeneric("facet_colcount")) +setGeneric( + "facet_colcount", + function(obj, path) standardGeneric("facet_colcount") +) #' @rdname facet_colcount #' @export -setMethod("facet_colcount", "LayoutColTree", +setMethod( + "facet_colcount", "LayoutColTree", function(obj, path = NULL) { ## if(length(path) == 0L) ## stop("face_colcount requires a non-null path") #nocov @@ -2664,7 +2684,8 @@ setMethod("facet_colcount", "LayoutColTree", #' @rdname facet_colcount #' @export -setMethod("facet_colcount", "LayoutColLeaf", +setMethod( + "facet_colcount", "LayoutColLeaf", function(obj, path = NULL) { ## not sure if we should check for null here as above obj@column_count @@ -2758,13 +2779,17 @@ setGeneric("colcount_visible", function(obj, path) standardGeneric("colcount_vis #' @rdname colcount_visible #' @export -setMethod("colcount_visible", "VTableTree", - function(obj, path) colcount_visible(coltree(obj), path)) +setMethod( + "colcount_visible", "VTableTree", + function(obj, path) colcount_visible(coltree(obj), path) +) #' @rdname colcount_visible #' @export -setMethod("colcount_visible", "InstantiatedColumnInfo", - function(obj, path) colcount_visible(coltree(obj), path)) +setMethod( + "colcount_visible", "InstantiatedColumnInfo", + function(obj, path) colcount_visible(coltree(obj), path) +) #' @rdname colcount_visible #' @export diff --git a/R/tt_compatibility.R b/R/tt_compatibility.R index 6788178b1..56677f166 100644 --- a/R/tt_compatibility.R +++ b/R/tt_compatibility.R @@ -551,15 +551,17 @@ EmptyTreePos <- TreePos() ## must be pathable, which they aren't if siblings have ## identical names fix_col_nm_recursive <- function(ct, newname, rename_obj = TRUE, oldnm) { - if (rename_obj) + if (rename_obj) { obj_name(ct) <- newname + } if (is(ct, "LayoutColTree")) { kids <- tree_children(ct) kidnms <- names(kids) newkids <- lapply(kids, fix_col_nm_recursive, - newname = newname, - rename_obj = FALSE, - oldnm = oldnm) + newname = newname, + rename_obj = FALSE, + oldnm = oldnm + ) names(newkids) <- kidnms tree_children(ct) <- newkids } @@ -577,19 +579,25 @@ fix_col_nm_recursive <- function(ct, newname, rename_obj = TRUE, oldnm) { if (!rename_obj) { spls <- pos_splits(mypos) splvals <- pos_splvals(mypos) - pos_splits(mypos) <- c(list(AllSplit(split_name = newname)), - spls) - pos_splvals(mypos) <- c(list(SplitValue(NA_character_, - sub_expr = quote(TRUE))), - splvals) + pos_splits(mypos) <- c( + list(AllSplit(split_name = newname)), + spls + ) + pos_splvals(mypos) <- c( + list(SplitValue(NA_character_, + sub_expr = quote(TRUE) + )), + splvals + ) tree_pos(ct) <- mypos } ct } fix_nms <- function(ct) { - if (is(ct, "LayoutColLeaf")) + if (is(ct, "LayoutColLeaf")) { return(ct) + } kids <- lapply(tree_children(ct), fix_nms) names(kids) <- vapply(kids, obj_name, "") tree_children(ct) <- kids @@ -619,7 +627,7 @@ combine_cinfo <- function(..., new_total = NULL, sync_count_vis) { nms <- make_cbind_names(num = length(oldnms), tokens = path_els) ctrees <- mapply(function(ct, nm, oldnm) { - ct <- fix_col_nm_recursive(ct, nm, rename_obj = TRUE, oldnm = "")#oldnm) + ct <- fix_col_nm_recursive(ct, nm, rename_obj = TRUE, oldnm = "") # oldnm) ct }, ct = ctrees, nm = nms, oldnm = oldnms, SIMPLIFY = FALSE) names(ctrees) <- nms @@ -632,10 +640,11 @@ combine_cinfo <- function(..., new_total = NULL, sync_count_vis) { } newexprs <- unlist(lapply(cinfs, col_exprs), recursive = FALSE) newexargs <- unlist(lapply(cinfs, col_extra_args), recursive = FALSE) %||% vector("list", length(newcounts)) - if (!sync_count_vis) + if (!sync_count_vis) { newdisp <- NA - else + } else { newdisp <- any(vapply(cinfs, disp_ccounts, NA)) + } alltls <- lapply(cinfs, top_left) newtl <- character() if (!are(tabs, "TableRow")) { diff --git a/R/tt_showmethods.R b/R/tt_showmethods.R index 16bff6d80..bacfaeca8 100644 --- a/R/tt_showmethods.R +++ b/R/tt_showmethods.R @@ -186,9 +186,10 @@ lastposmsg <- function(pos) { spls <- pos_splits(pos) splvals <- value_names(pos_splvals(pos)) indiv_msgs <- unlist(mapply(function(spl, valnm) paste(obj_name(spl), valnm, sep = ": "), - spl = spls, - valnm = splvals, - SIMPLIFY = FALSE)) + spl = spls, + valnm = splvals, + SIMPLIFY = FALSE + )) paste(indiv_msgs, collapse = " -> ") } diff --git a/R/tt_toString.R b/R/tt_toString.R index 624376fa1..606942072 100644 --- a/R/tt_toString.R +++ b/R/tt_toString.R @@ -341,19 +341,22 @@ check_ccount_vis_ok <- function(tt) { ccvis_check_subtree <- function(ctree) { kids <- tree_children(ctree) - if (is.null(kids)) + if (is.null(kids)) { return(invisible(NULL)) + } vals <- vapply(kids, disp_ccounts, TRUE) if (length(unique(vals)) > 1) { unmatch <- which(!duplicated(vals))[1:2] - stop("Detected different colcount visibility among sibling facets (those ", - "arising from the same split_cols_by* layout instruction). This is ", - "not supported.\n", - "Set count values to NA if you want a blank space to appear as the ", - "displayed count for particular facets.\n", - "First disagreement occured at paths:\n", - .path_to_disp(pos_to_path(tree_pos(kids[[unmatch[1]]]))), "\n", - .path_to_disp(pos_to_path(tree_pos(kids[[unmatch[2]]])))) + stop( + "Detected different colcount visibility among sibling facets (those ", + "arising from the same split_cols_by* layout instruction). This is ", + "not supported.\n", + "Set count values to NA if you want a blank space to appear as the ", + "displayed count for particular facets.\n", + "First disagreement occured at paths:\n", + .path_to_disp(pos_to_path(tree_pos(kids[[unmatch[1]]]))), "\n", + .path_to_disp(pos_to_path(tree_pos(kids[[unmatch[2]]]))) + ) } lapply(kids, ccvis_check_subtree) invisible(NULL) @@ -524,9 +527,12 @@ get_formatted_fnotes <- function(tt) { if (lens[i] < padto) { chk <- chunks[[i]] span <- sum(vapply(chk[[length(chk)]], cell_cspan, 1L)) - chunks[[i]] <- c(replicate(list(list(rcell("", colspan = span))), - n = padto - lens[i]), - chk) + chunks[[i]] <- c( + replicate(list(list(rcell("", colspan = span))), + n = padto - lens[i] + ), + chk + ) } } chunks @@ -578,7 +584,7 @@ get_formatted_fnotes <- function(tt) { cellii <- rcell( val, colspan = rws$total_span[ri], - format = rws$ccount_format[ri], #cc_format, + format = rws$ccount_format[ri], # cc_format, format_na_str = na_str ) cellii diff --git a/tests/testthat/test-binding.R b/tests/testthat/test-binding.R index e0ccd585d..12b30cea6 100644 --- a/tests/testthat/test-binding.R +++ b/tests/testthat/test-binding.R @@ -31,8 +31,10 @@ test_that("cbind_rtables works with 3 tables", { ## this was not previously the case which broke higher-level ns display machinery cpaths <- col_paths(newtab) for (i in seq_along(cpaths)) { - expect_equal(newtab[, i], - newtab[, cpaths[[i]]]) + expect_equal( + newtab[, i], + newtab[, cpaths[[i]]] + ) } }) diff --git a/tests/testthat/test-printing.R b/tests/testthat/test-printing.R index e5dfe31de..8e86d212b 100644 --- a/tests/testthat/test-printing.R +++ b/tests/testthat/test-printing.R @@ -789,7 +789,7 @@ test_that("horizontal separator is propagated from table to print and export", { ## higher-level showing ncols works: test_that("showing higher-level ncols works", { - mydat <- subset(ex_adsl, SEX %in% c("M", "F")) + mydat <- subset(ex_adsl, SEX %in% c("M", "F")) mydat$SEX2 <- factor( ifelse( mydat$SEX == "M", @@ -840,18 +840,24 @@ test_that("showing higher-level ncols works", { col_counts(tbl2) <- new_ccs mpf2 <- matrix_form(tbl2, TRUE) - expect_equal(mf_strings(mpf2)[mf_nlheader(mpf2), -1, drop = TRUE], - sprintf("(N=%d)", new_ccs)) + expect_equal( + mf_strings(mpf2)[mf_nlheader(mpf2), -1, drop = TRUE], + sprintf("(N=%d)", new_ccs) + ) ## NA counts (to display blank) work correctly for higher level facets tbl3 <- tbl facet_colcount(tbl3, c("ARM", "C: Combination")) <- NA_integer_ mpf3 <- matrix_form(tbl3, TRUE) ## starting at "column" 2 because topleft/row labels - expect_equal(mf_strings(mpf3)[2, 2:13], - mf_strings(mpf)[2, 2:13]) - expect_equal(mf_strings(mpf3)[2, 14:19], - rep("", 6)) + expect_equal( + mf_strings(mpf3)[2, 2:13], + mf_strings(mpf)[2, 2:13] + ) + expect_equal( + mf_strings(mpf3)[2, 14:19], + rep("", 6) + ) tbl4 <- tbl2 col_counts(tbl4)[rep(c(FALSE, TRUE), times = c(14, 4))] <- NA_integer_ @@ -877,8 +883,10 @@ test_that("showing higher-level ncols works", { expect_silent(toString(tbl5)) col_counts(tbl5)[c(FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE)] <- NA_integer_ mpf5 <- matrix_form(tbl5, TRUE) - expect_equal(mf_strings(mpf5)[3, c(3, 7, 8)], # cols 2, 6 and 7, remember row labels! - c("", "", "")) + expect_equal( + mf_strings(mpf5)[3, c(3, 7, 8)], # cols 2, 6 and 7, remember row labels! + c("", "", "") + ) lyt6 <- basic_table(show_colcounts = TRUE, colcount_format = "N=xx") %>% split_cols_by("active_trt", split_fun = trim_levels_in_group("ARM")) %>%