diff --git a/DESCRIPTION b/DESCRIPTION index 643c99b44..feac7c0e5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: rtables Title: Reporting Tables -Version: 0.6.6.9002 -Date: 2024-01-08 +Version: 0.6.6.9003 +Date: 2024-01-10 Authors@R: c( person("Gabriel", "Becker", , "gabembecker@gmail.com", role = "aut", comment = "Original creator of the package"), diff --git a/NEWS.md b/NEWS.md index 40957fe1c..1f209a3a9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -## rtables 0.6.6.9002 +## rtables 0.6.6.9003 ### New Features * Added `top_level_section_div` for `basic_table` to set section dividers for top level rows. diff --git a/R/tree_accessors.R b/R/tree_accessors.R index 09c786fd0..10ef009e6 100644 --- a/R/tree_accessors.R +++ b/R/tree_accessors.R @@ -1173,18 +1173,22 @@ setMethod("content_na_str<-", "Split", function(obj, value) { #' Returns a matrix of formats for the cells in a table #' @param obj A table or row object. #' @param default `FormatSpec`. -#' @export +#' #' @return Matrix (storage mode list) containing the effective format for each #' cell position in the table (including 'virtual' cells implied by label rows, -#' whose formats are always `NULL`) -#' @examples +#' whose formats are always `NULL`). +#' +#' @seealso [table_shell()] and [table_shell_str()] for information on the table format structure. #' +#' @examples #' lyt <- basic_table() %>% #' split_rows_by("RACE", split_fun = keep_split_levels(c("ASIAN", "WHITE"))) %>% #' analyze("AGE") #' #' tbl <- build_table(lyt, DM) #' value_formats(tbl) +#' +#' @export setGeneric("value_formats", function(obj, default = obj_format(obj)) standardGeneric("value_formats")) #' @rdname value_formats setMethod( @@ -1193,7 +1197,6 @@ setMethod( obj_format(obj) %||% default } ) - #' @rdname value_formats setMethod( "value_formats", "TableRow", diff --git a/R/tt_toString.R b/R/tt_toString.R index 3b9763c43..ca5341c55 100644 --- a/R/tt_toString.R +++ b/R/tt_toString.R @@ -72,8 +72,10 @@ setMethod("toString", "VTableTree", function(x, #' @inheritParams tostring #' @inheritParams gen_args #' @return for `table_shell_str` the string representing the table shell, for `table_shell`, -#' `NULL`, as the function is called for the side effect of printing the shell to the console -#' @export +#' `NULL`, as the function is called for the side effect of printing the shell to the console. +#' +#' @seealso [value_formats()] for a matrix of formats for each cell in a table. +#' #' @examples #' library(dplyr) #' @@ -89,6 +91,8 @@ setMethod("toString", "VTableTree", function(x, #' #' tbl <- build_table(lyt, iris2) #' table_shell(tbl) +#' +#' @export table_shell <- function(tt, widths = NULL, col_gap = 3, hsep = default_hsep(), tf_wrap = FALSE, max_width = NULL) { cat(table_shell_str( diff --git a/man/table_shell.Rd b/man/table_shell.Rd index 31fa94f06..891298d14 100644 --- a/man/table_shell.Rd +++ b/man/table_shell.Rd @@ -51,7 +51,7 @@ completely if \code{tf_wrap} is \code{FALSE}.} } \value{ for \code{table_shell_str} the string representing the table shell, for \code{table_shell}, -\code{NULL}, as the function is called for the side effect of printing the shell to the console +\code{NULL}, as the function is called for the side effect of printing the shell to the console. } \description{ A table shell is a rendering of the table which maintains the structure, but does not @@ -72,4 +72,8 @@ lyt <- basic_table() \%>\% tbl <- build_table(lyt, iris2) table_shell(tbl) + +} +\seealso{ +\code{\link[=value_formats]{value_formats()}} for a matrix of formats for each cell in a table. } diff --git a/man/value_formats.Rd b/man/value_formats.Rd index 69160fc6a..0671439c5 100644 --- a/man/value_formats.Rd +++ b/man/value_formats.Rd @@ -26,17 +26,20 @@ value_formats(obj, default = obj_format(obj)) \value{ Matrix (storage mode list) containing the effective format for each cell position in the table (including 'virtual' cells implied by label rows, -whose formats are always \code{NULL}) +whose formats are always \code{NULL}). } \description{ Returns a matrix of formats for the cells in a table } \examples{ - lyt <- basic_table() \%>\% split_rows_by("RACE", split_fun = keep_split_levels(c("ASIAN", "WHITE"))) \%>\% analyze("AGE") tbl <- build_table(lyt, DM) value_formats(tbl) + +} +\seealso{ +\code{\link[=table_shell]{table_shell()}} and \code{\link[=table_shell_str]{table_shell_str()}} for information on the table format structure. } diff --git a/vignettes/introspecting_tables.Rmd b/vignettes/introspecting_tables.Rmd index 567ffc091..d924142d3 100644 --- a/vignettes/introspecting_tables.Rmd +++ b/vignettes/introspecting_tables.Rmd @@ -120,6 +120,20 @@ row_paths_summary(tbl) col_paths_summary(tbl) ``` +## Insights on Value Format Structure + +We can gain insight into the value formatting structure of a table using `table_shell()`, which returns a table with the same output as `print()` but with the cell values replaced by their underlying format strings (e.g. instead of `40.0`, `xx.x` is displayed, and so on). This is useful for understanding the structure of the table, and for debugging purposes. Another useful tool is the `value_formats()` function which instead of a table returns a matrix of the format strings for each cell value in the table. + +See below the printout for the above examples: + +```{r} +table_shell(tbl) +``` + +```{r} +value_formats(tbl) +``` + ## Applications Knowing the structure of an `rtable` object is helpful for retrieving specific values from the table.