diff --git a/NAMESPACE b/NAMESPACE index 961a7dc6..52a9cc81 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -55,6 +55,7 @@ export(apply_conditional_format) export(apply_formats) export(apply_row_masks) export(build) +export(collapse_row_labels) export(f_str) export(get_by) export(get_count_layer_formats) @@ -138,6 +139,7 @@ importFrom(dplyr,bind_rows) importFrom(dplyr,case_when) importFrom(dplyr,cur_column) importFrom(dplyr,cur_group) +importFrom(dplyr,desc) importFrom(dplyr,distinct) importFrom(dplyr,do) importFrom(dplyr,everything) @@ -160,10 +162,12 @@ importFrom(dplyr,rename) importFrom(dplyr,row_number) importFrom(dplyr,rowwise) importFrom(dplyr,select) +importFrom(dplyr,slice_head) importFrom(dplyr,summarize) importFrom(dplyr,tally) importFrom(dplyr,ungroup) importFrom(dplyr,vars) +importFrom(dplyr,where) importFrom(forcats,fct_collapse) importFrom(forcats,fct_drop) importFrom(forcats,fct_expand) @@ -177,6 +181,7 @@ importFrom(purrr,flatten) importFrom(purrr,imap) importFrom(purrr,map) importFrom(purrr,map2) +importFrom(purrr,map2_chr) importFrom(purrr,map2_dfr) importFrom(purrr,map_chr) importFrom(purrr,map_dbl) diff --git a/R/collapse_row_labels.R b/R/collapse_row_labels.R index cc6b5dd0..23c1f376 100644 --- a/R/collapse_row_labels.R +++ b/R/collapse_row_labels.R @@ -2,8 +2,10 @@ #' #' @param .x The number of levels to indent #' @param .y Input variable for which indentation will be done +#' @param indent Indentation string to be used, which is multiplied at each indentation level #' #' @return Character string with indentation applied +#' @noRd add_indentation <- function(.x, .y, indent = " ") { paste(c(rep("",.x-1), .y), collapse=indent) } diff --git a/R/zzz.R b/R/zzz.R index 0c8ec46a..c111ad2a 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -3,7 +3,7 @@ #' @importFrom rlang expr exprs enexprs enexpr is_named env_parent env_label is_logical is_empty is_quosures quo_is_symbol sym syms := as_name #' @importFrom rlang quos quo env_names env_bind_active as_label eval_tidy warn quo_is_call #' @importFrom stringr str_split str_extract_all regex str_detect str_replace_all str_replace str_locate_all fixed str_count str_trim str_wrap -#' @importFrom purrr flatten map map_lgl pmap_chr imap reduce map_chr map_int map_dbl map_dfr pmap_dfr walk2 map2 map2_dfr walk +#' @importFrom purrr flatten map map_lgl pmap_chr imap reduce map_chr map_int map_dbl map_dfr pmap_dfr walk2 map2 map2_dfr map2_chr walk #' @importFrom stringr str_sub str_sub<- str_extract str_pad str_starts str_remove_all str_match_all #' @importFrom tidyr pivot_longer pivot_wider replace_na #' @importFrom magrittr %>% extract extract2 @@ -11,7 +11,7 @@ #' @importFrom stats IQR median sd quantile var #' @importFrom dplyr n summarize filter vars tally ungroup group_by mutate lag select bind_rows full_join add_tally distinct rowwise #' @importFrom dplyr everything rename mutate_at mutate_all as_tibble bind_cols do case_when arrange left_join row_number between mutate_if -#' @importFrom dplyr across anti_join n_distinct if_else group_keys cur_group cur_column pull matches +#' @importFrom dplyr across anti_join n_distinct if_else group_keys cur_group cur_column pull matches slice_head where desc #' @importFrom tidyr complete nesting pivot_wider pivot_longer replace_na starts_with fill #' @importFrom utils str head tail #' @importFrom tidyselect all_of vars_select any_of @@ -303,3 +303,8 @@ l <- NULL w <- NULL s <- NULL out <- NULL +og_row <- NULL +desc <- NULL +id <- NULL +stub_sort <- NULL + diff --git a/man/add_indentation.Rd b/man/add_indentation.Rd deleted file mode 100644 index 1c9b9813..00000000 --- a/man/add_indentation.Rd +++ /dev/null @@ -1,19 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/collapse_row_labels.R -\name{add_indentation} -\alias{add_indentation} -\title{Add indentation level based} -\usage{ -add_indentation(.x, .y, indent = " ") -} -\arguments{ -\item{.x}{The number of levels to indent} - -\item{.y}{Input variable for which indentation will be done} -} -\value{ -Character string with indentation applied -} -\description{ -Add indentation level based -} diff --git a/vignettes/post_processing.Rmd b/vignettes/post_processing.Rmd index 0578797c..b2fdab3f 100644 --- a/vignettes/post_processing.Rmd +++ b/vignettes/post_processing.Rmd @@ -100,6 +100,38 @@ There are a few considerations when using `apply_row_masks()`: - This function is order dependent, so make sure your data are sorted before submitting to `apply_row_masks()` - When inserting row breaks, by default the Tpylr variable `ord_layer_index` is used. You can submit other variables via the ellipsis parameter (`...`) if you'd like to use a different variable grouping to insert rows +## Collapsing Row Labels + +Different table formats call for different handling of row labels, depending on the preferences of an individual organization and the specifics of the table at hand. **Tplyr** inherently creates row labels as separate columns, but similar to the way that count layers nest the inner and the outer layer, we also offer the `collapse_row_labels()` function to pull multiple row labels into a single column. + +```{r collapse_row_labels} +dat <- tplyr_table(adsl, TRT01P) %>% + add_layer( + group_count(RACE, by = vars("Race n (%)", SEX)) + ) %>% + add_layer( + group_desc(AGE, by = vars("Age (years)", SEX)) + ) %>% + build() + +collapse_row_labels(dat, row_label1, row_label2, row_label3) %>% + select(row_label, var1_Placebo) +``` +By default, indentation is set to 2 spaces, but by using the `indent` parameter you can change this to any string you desire. + +```{r collapse_row_labels2} +collapse_row_labels(dat, row_label1, row_label2, row_label3, indent = "  ") %>% + select(row_label, var1_Placebo) %>% + kable(escape=FALSE) +``` +You also have control over which columns you collapse, allowing you to keep separate row labels if you don't want all collapsed together + +```{r collapse_row_labels3} +collapse_row_labels(dat, row_label1, row_label2, indent = "  ") %>% + select(row_label, row_label3, var1_Placebo) %>% + kable(escape=FALSE) +``` + ## Conditional Formatting In some circumstances, like `add_total_row()`, **Tplyr** lets you specify special formats separate from those in `set_format_strings()`. But within the table body there's no other way to set specific, conditional formats based on the table data itself. To address this, we've added the post-processing function `apply_conditional_format()` to allow you to set conditional formats on result cells.