| 1 |
#' Paginate listings |
|
| 2 |
#' |
|
| 3 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 4 |
#' |
|
| 5 |
#' Pagination of a listing. This can be vertical for long listings with many |
|
| 6 |
#' rows and/or horizontal if there are many columns. This function is a wrapper of |
|
| 7 |
#' [formatters::paginate_to_mpfs()] and it is mainly meant for exploration and testing. |
|
| 8 |
#' |
|
| 9 |
#' @inheritParams formatters::pag_indices_inner |
|
| 10 |
#' @inheritParams formatters::vert_pag_indices |
|
| 11 |
#' @inheritParams formatters::page_lcpp |
|
| 12 |
#' @inheritParams formatters::toString |
|
| 13 |
#' @param lsting (`listing_df` or `list`)\cr the listing or list of listings to paginate. |
|
| 14 |
#' @param lpp (`numeric(1)` or `NULL`)\cr number of rows/lines (excluding titles and footers) |
|
| 15 |
#' to include per page. Standard is `70` while `NULL` disables vertical pagination. |
|
| 16 |
#' @param cpp (`numeric(1)` or `NULL`)\cr width (in characters) of the pages for horizontal |
|
| 17 |
#' pagination. `NULL` (the default) indicates no horizontal pagination should be done. |
|
| 18 |
#' @param print_pages (`flag`)\cr whether the paginated listing should be printed to the console |
|
| 19 |
#' (`cat(toString(x))`). |
|
| 20 |
#' |
|
| 21 |
#' @return A list of `listing_df` objects where each list element corresponds to a separate page. |
|
| 22 |
#' |
|
| 23 |
#' @examples |
|
| 24 |
#' dat <- ex_adae |
|
| 25 |
#' lsting <- as_listing(dat[1:25, ], disp_cols = c("USUBJID", "AESOC", "RACE", "AETOXGR", "BMRKR1"))
|
|
| 26 |
#' mat <- matrix_form(lsting) |
|
| 27 |
#' cat(toString(mat)) |
|
| 28 |
#' |
|
| 29 |
#' paginate_listing(lsting, lpp = 10) |
|
| 30 |
#' |
|
| 31 |
#' paginate_listing(lsting, cpp = 100, lpp = 40) |
|
| 32 |
#' |
|
| 33 |
#' paginate_listing(lsting, cpp = 80, lpp = 40, verbose = TRUE) |
|
| 34 |
#' |
|
| 35 |
#' @export |
|
| 36 |
#' @rdname paginate |
|
| 37 |
paginate_listing <- function(lsting, |
|
| 38 |
page_type = "letter", |
|
| 39 |
font_family = "Courier", |
|
| 40 |
font_size = 8, |
|
| 41 |
lineheight = 1, |
|
| 42 |
landscape = FALSE, |
|
| 43 |
pg_width = NULL, |
|
| 44 |
pg_height = NULL, |
|
| 45 |
margins = c(top = .5, bottom = .5, left = .75, right = .75), |
|
| 46 |
lpp = NA_integer_, |
|
| 47 |
cpp = NA_integer_, |
|
| 48 |
colwidths = NULL, |
|
| 49 |
tf_wrap = !is.null(max_width), |
|
| 50 |
rep_cols = NULL, |
|
| 51 |
max_width = NULL, |
|
| 52 |
col_gap = 3, |
|
| 53 |
fontspec = font_spec(font_family, font_size, lineheight), |
|
| 54 |
verbose = FALSE, |
|
| 55 |
print_pages = TRUE) {
|
|
| 56 | 22x |
checkmate::assert_multi_class(lsting, c("listing_df", "list"))
|
| 57 | 22x |
checkmate::assert_numeric(colwidths, lower = 0, len = length(listing_dispcols(lsting)), null.ok = TRUE) |
| 58 | 21x |
checkmate::assert_flag(tf_wrap) |
| 59 | 21x |
checkmate::assert_count(max_width, null.ok = TRUE) |
| 60 | 21x |
checkmate::assert_flag(verbose) |
| 61 | 21x |
checkmate::assert_flag(print_pages) |
| 62 | ||
| 63 | 21x |
pages <- paginate_to_mpfs(lsting, |
| 64 | 21x |
page_type = page_type, |
| 65 | 21x |
fontspec = fontspec, |
| 66 | 21x |
landscape = landscape, |
| 67 | 21x |
pg_width = pg_width, |
| 68 | 21x |
pg_height = pg_height, |
| 69 | 21x |
margins = margins, |
| 70 | 21x |
lpp = lpp, |
| 71 | 21x |
cpp = cpp, |
| 72 | 21x |
colwidths = colwidths, |
| 73 | 21x |
tf_wrap = tf_wrap, |
| 74 | 21x |
max_width = max_width, |
| 75 | 21x |
rep_cols = rep_cols, |
| 76 | 21x |
col_gap = col_gap, |
| 77 | 21x |
verbose = verbose |
| 78 |
) |
|
| 79 | ||
| 80 | 21x |
if (print_pages) {
|
| 81 | ! |
nothing <- lapply(seq_along(pages), function(pagi) {
|
| 82 | ! |
cat("--- Page", paste0(pagi, "/", length(pages)), "---\n")
|
| 83 |
# It is NULL because paginate_mpfs takes care of it |
|
| 84 | ! |
cat(toString(pages[[pagi]], widths = NULL, tf_wrap = tf_wrap, max_width = max_width, col_gap = col_gap)) |
| 85 | ! |
cat("\n")
|
| 86 |
}) |
|
| 87 |
} |
|
| 88 | 21x |
invisible(pages) |
| 89 |
} |
| 1 |
setOldClass(c("listing_df", "tbl_df", "tbl", "data.frame"))
|
|
| 2 |
setOldClass(c("MatrixPrintForm", "list"))
|
|
| 3 | ||
| 4 |
#' Create a listing from a `data.frame` or `tibble` |
|
| 5 |
#' |
|
| 6 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 7 |
#' |
|
| 8 |
#' Create listings displaying `key_cols` and `disp_cols` to produce a compact and |
|
| 9 |
#' elegant representation of the input `data.frame` or `tibble`. |
|
| 10 |
#' |
|
| 11 |
#' @param df (`data.frame` or `listing_df`)\cr the `data.frame` to be converted to a listing or |
|
| 12 |
#' `listing_df` to be modified. |
|
| 13 |
#' @param key_cols (`character`)\cr vector of names of columns which should be treated as *key columns* |
|
| 14 |
#' when rendering the listing. Key columns allow you to group repeat occurrences. |
|
| 15 |
#' @param disp_cols (`character` or `NULL`)\cr vector of names of non-key columns which should be |
|
| 16 |
#' displayed when the listing is rendered. Defaults to all columns of `df` not named in `key_cols` or |
|
| 17 |
#' `non_disp_cols`. |
|
| 18 |
#' @param non_disp_cols (`character` or `NULL`)\cr vector of names of non-key columns to be excluded as display |
|
| 19 |
#' columns. All other non-key columns are treated as display columns. Ignored if `disp_cols` is non-`NULL`. |
|
| 20 |
#' @param unique_rows (`flag`)\cr whether only unique rows should be included in the listing. Defaults to `FALSE`. |
|
| 21 |
#' @param default_formatting (`list`)\cr a named list of default column format configurations to apply when rendering |
|
| 22 |
#' the listing. Each name-value pair consists of a name corresponding to a data class (or "numeric" for all |
|
| 23 |
#' unspecified numeric classes) and a value of type `fmt_config` with the format configuration that should be |
|
| 24 |
#' implemented for columns of that class. If named element "all" is included in the list, this configuration will be |
|
| 25 |
#' used for all data classes not specified. Objects of type `fmt_config` can take 3 arguments: `format`, `na_str`, |
|
| 26 |
#' and `align`. |
|
| 27 |
#' @param col_formatting (`list`)\cr a named list of custom column formatting configurations to apply to specific |
|
| 28 |
#' columns when rendering the listing. Each name-value pair consists of a name corresponding to a column name and a |
|
| 29 |
#' value of type `fmt_config` with the formatting configuration that should be implemented for that column. Objects |
|
| 30 |
#' of type `fmt_config` can take 3 arguments: `format`, `na_str`, and `align`. Defaults to `NULL`. |
|
| 31 |
#' @param main_title (`string` or `NULL`)\cr the main title for the listing, or `NULL` (the default). |
|
| 32 |
#' @param subtitles (`character` or `NULL`)\cr a vector of subtitles for the listing, or `NULL` (the default). |
|
| 33 |
#' @param main_footer (`character` or `NULL`)\cr a vector of main footer lines for the listing, or `NULL` (the default). |
|
| 34 |
#' @param prov_footer (`character` or `NULL`)\cr a vector of provenance footer lines for the listing, or `NULL` |
|
| 35 |
#' (the default). Each string element is placed on a new line. |
|
| 36 |
#' @param split_into_pages_by_var (`character` or `NULL`)\cr the name of a variable for on the listing should be split |
|
| 37 |
#' into pages, with each page corresponding to one unique value/level of the variable. See |
|
| 38 |
#' [split_into_pages_by_var()] for more details. |
|
| 39 |
#' @param vec (`string`)\cr name of a column vector from a `listing_df` object to be annotated as a key column. |
|
| 40 |
#' |
|
| 41 |
#' @return A `listing_df` object, sorted by its key columns. |
|
| 42 |
#' |
|
| 43 |
#' @details |
|
| 44 |
#' At its core, a `listing_df` object is a `tbl_df` object with a customized |
|
| 45 |
#' print method and support for the formatting and pagination machinery provided by |
|
| 46 |
#' the `formatters` package. |
|
| 47 |
#' |
|
| 48 |
#' `listing_df` objects have two 'special' types of columns: key columns and display columns. |
|
| 49 |
#' |
|
| 50 |
#' Key columns act as indexes, which means a number of things in practice. |
|
| 51 |
#' |
|
| 52 |
#' All key columns are also display columns. |
|
| 53 |
#' |
|
| 54 |
#' `listing_df` objects are always sorted by their set of key columns at creation time. |
|
| 55 |
#' Any `listing_df` object which is not sorted by its full set of key columns (e.g., |
|
| 56 |
#' one whose rows have been reordered explicitly during creation) is invalid and the behavior |
|
| 57 |
#' when rendering or paginating that object is undefined. |
|
| 58 |
#' |
|
| 59 |
#' Each value of a key column is printed only once per page and per unique combination of |
|
| 60 |
#' values for all higher-priority (i.e., to the left of it) key columns. Locations |
|
| 61 |
#' where a repeated value would have been printed within a key column for the same |
|
| 62 |
#' higher-priority-key combination on the same page are rendered as empty space. |
|
| 63 |
#' Note, determination of which elements to display within a key column at rendering is |
|
| 64 |
#' based on the underlying value; any non-default formatting applied to the column |
|
| 65 |
#' has no effect on this behavior. |
|
| 66 |
#' |
|
| 67 |
#' Display columns are columns which should be rendered, but are not key columns. By |
|
| 68 |
#' default this is all non-key columns in the incoming data, but in need not be. |
|
| 69 |
#' Columns in the underlying data which are neither key nor display columns remain |
|
| 70 |
#' within the object available for computations but *are not rendered during |
|
| 71 |
#' printing or export of the listing*. |
|
| 72 |
#' |
|
| 73 |
#' @examples |
|
| 74 |
#' dat <- ex_adae |
|
| 75 |
#' |
|
| 76 |
#' # This example demonstrates the listing with key_cols (values are grouped by USUBJID) and |
|
| 77 |
#' # multiple lines in prov_footer |
|
| 78 |
#' lsting <- as_listing(dat[1:25, ], |
|
| 79 |
#' key_cols = c("USUBJID", "AESOC"),
|
|
| 80 |
#' main_title = "Example Title for Listing", |
|
| 81 |
#' subtitles = "This is the subtitle for this Adverse Events Table", |
|
| 82 |
#' main_footer = "Main footer for the listing", |
|
| 83 |
#' prov_footer = c( |
|
| 84 |
#' "You can even add a subfooter", "Second element is place on a new line", |
|
| 85 |
#' "Third string" |
|
| 86 |
#' ) |
|
| 87 |
#' ) %>% |
|
| 88 |
#' add_listing_col("AETOXGR") %>%
|
|
| 89 |
#' add_listing_col("BMRKR1", format = "xx.x") %>%
|
|
| 90 |
#' add_listing_col("AESER / AREL", fun = function(df) paste(df$AESER, df$AREL, sep = " / "))
|
|
| 91 |
#' |
|
| 92 |
#' mat <- matrix_form(lsting) |
|
| 93 |
#' |
|
| 94 |
#' cat(toString(mat)) |
|
| 95 |
#' |
|
| 96 |
#' # This example demonstrates the listing table without key_cols |
|
| 97 |
#' # and specifying the cols with disp_cols. |
|
| 98 |
#' dat <- ex_adae |
|
| 99 |
#' lsting <- as_listing(dat[1:25, ], |
|
| 100 |
#' disp_cols = c("USUBJID", "AESOC", "RACE", "AETOXGR", "BMRKR1")
|
|
| 101 |
#' ) |
|
| 102 |
#' |
|
| 103 |
#' mat <- matrix_form(lsting) |
|
| 104 |
#' |
|
| 105 |
#' cat(toString(mat)) |
|
| 106 |
#' |
|
| 107 |
#' # This example demonstrates a listing with format configurations specified |
|
| 108 |
#' # via the default_formatting and col_formatting arguments |
|
| 109 |
#' dat <- ex_adae |
|
| 110 |
#' dat$AENDY[3:6] <- NA |
|
| 111 |
#' lsting <- as_listing(dat[1:25, ], |
|
| 112 |
#' key_cols = c("USUBJID", "AESOC"),
|
|
| 113 |
#' disp_cols = c("STUDYID", "SEX", "ASEQ", "RANDDT", "ASTDY", "AENDY"),
|
|
| 114 |
#' default_formatting = list( |
|
| 115 |
#' all = fmt_config(align = "left"), |
|
| 116 |
#' numeric = fmt_config( |
|
| 117 |
#' format = "xx.xx", |
|
| 118 |
#' na_str = "<No data>", |
|
| 119 |
#' align = "right" |
|
| 120 |
#' ) |
|
| 121 |
#' ) |
|
| 122 |
#' ) %>% |
|
| 123 |
#' add_listing_col("BMRKR1", format = "xx.x", align = "center")
|
|
| 124 |
#' |
|
| 125 |
#' mat <- matrix_form(lsting) |
|
| 126 |
#' |
|
| 127 |
#' cat(toString(mat)) |
|
| 128 |
#' |
|
| 129 |
#' @export |
|
| 130 |
#' @rdname listings |
|
| 131 |
as_listing <- function(df, |
|
| 132 |
key_cols = names(df)[1], |
|
| 133 |
disp_cols = NULL, |
|
| 134 |
non_disp_cols = NULL, |
|
| 135 |
unique_rows = FALSE, |
|
| 136 |
default_formatting = list(all = fmt_config()), |
|
| 137 |
col_formatting = NULL, |
|
| 138 |
main_title = NULL, |
|
| 139 |
subtitles = NULL, |
|
| 140 |
main_footer = NULL, |
|
| 141 |
prov_footer = NULL, |
|
| 142 |
split_into_pages_by_var = NULL) {
|
|
| 143 | 51x |
if (length(non_disp_cols) > 0 && length(intersect(key_cols, non_disp_cols)) > 0) {
|
| 144 | 1x |
stop( |
| 145 | 1x |
"Key column also listed in non_disp_cols. All key columns are by", |
| 146 | 1x |
" definition display columns." |
| 147 |
) |
|
| 148 |
} |
|
| 149 | 50x |
if (!is.null(disp_cols) && !is.null(non_disp_cols)) {
|
| 150 | 1x |
stop("Got non-null values for both disp_cols and non_disp_cols. This is not supported.")
|
| 151 | 49x |
} else if (is.null(disp_cols)) {
|
| 152 |
## non_disp_cols NULL is ok here |
|
| 153 | 19x |
cols <- setdiff(names(df), c(key_cols, non_disp_cols)) |
| 154 |
} else {
|
|
| 155 |
## disp_cols non-null, non_disp_cols NULL |
|
| 156 | 30x |
cols <- disp_cols |
| 157 |
} |
|
| 158 | 49x |
if (!all(sapply(default_formatting, is, class2 = "fmt_config"))) {
|
| 159 | ! |
stop( |
| 160 | ! |
"All format configurations supplied in `default_formatting`", |
| 161 | ! |
" must be of type `fmt_config`." |
| 162 |
) |
|
| 163 |
} |
|
| 164 | 49x |
if (!(is.null(col_formatting) || all(sapply(col_formatting, is, class2 = "fmt_config")))) {
|
| 165 | ! |
stop( |
| 166 | ! |
"All format configurations supplied in `col_formatting`", |
| 167 | ! |
" must be of type `fmt_config`." |
| 168 |
) |
|
| 169 |
} |
|
| 170 | ||
| 171 | 49x |
df <- as_tibble(df) |
| 172 | 49x |
varlabs <- var_labels(df, fill = TRUE) |
| 173 | 49x |
o <- do.call(order, df[key_cols]) |
| 174 | 49x |
if (is.unsorted(o)) {
|
| 175 | 25x |
if (interactive()) {
|
| 176 | ! |
message("sorting incoming data by key columns")
|
| 177 |
} |
|
| 178 | 25x |
df <- df[o, ] |
| 179 |
} |
|
| 180 | ||
| 181 |
## reorder the full set of cols to ensure key columns are first |
|
| 182 | 49x |
ordercols <- c(key_cols, setdiff(names(df), key_cols)) |
| 183 | 49x |
df <- df[, ordercols] |
| 184 | 49x |
var_labels(df) <- varlabs[ordercols] |
| 185 | ||
| 186 | 49x |
for (cnm in key_cols) {
|
| 187 | 77x |
df[[cnm]] <- as_keycol(df[[cnm]]) |
| 188 |
} |
|
| 189 | ||
| 190 |
## key cols must be leftmost cols |
|
| 191 | 49x |
cols <- c(key_cols, setdiff(cols, key_cols)) |
| 192 | ||
| 193 | 49x |
row_all_na <- apply(df[cols], 1, function(x) all(is.na(x))) |
| 194 | 49x |
if (any(row_all_na)) {
|
| 195 | 1x |
warning("rows that only contain NA values have been trimmed")
|
| 196 | 1x |
df <- df[!row_all_na, ] |
| 197 |
} |
|
| 198 | ||
| 199 |
# set col format configs |
|
| 200 | 49x |
df[cols] <- lapply(cols, function(col) {
|
| 201 | 272x |
col_class <- tail(class(df[[col]]), 1) |
| 202 | 272x |
col_fmt_class <- if (!col_class %in% names(default_formatting) && is.numeric(df[[col]])) "numeric" else col_class |
| 203 | 272x |
col_fmt <- if (col %in% names(col_formatting)) {
|
| 204 | 6x |
col_formatting[[col]] |
| 205 | 272x |
} else if (col_fmt_class %in% names(default_formatting)) {
|
| 206 | 6x |
default_formatting[[col_fmt_class]] |
| 207 |
} else {
|
|
| 208 | 260x |
if (!"all" %in% names(default_formatting)) {
|
| 209 | ! |
stop( |
| 210 | ! |
"Format configurations must be supplied for all listing columns. ", |
| 211 | ! |
"To cover all remaining columns please add an 'all' configuration", |
| 212 | ! |
" to `default_formatting`." |
| 213 |
) |
|
| 214 |
} |
|
| 215 | 260x |
default_formatting[["all"]] |
| 216 |
} |
|
| 217 |
# ANY attr <- fmt_config slot |
|
| 218 | 272x |
obj_format(df[[col]]) <- obj_format(col_fmt) |
| 219 | 272x |
obj_na_str(df[[col]]) <- if (is.null(obj_na_str(col_fmt))) "NA" else obj_na_str(col_fmt) |
| 220 | 272x |
obj_align(df[[col]]) <- if (is.null(obj_align(col_fmt))) "left" else obj_align(col_fmt) |
| 221 | 272x |
df[[col]] |
| 222 |
}) |
|
| 223 | ||
| 224 | 2x |
if (unique_rows) df <- df[!duplicated(df[, cols]), ] |
| 225 | ||
| 226 | 49x |
class(df) <- c("listing_df", class(df))
|
| 227 | ||
| 228 |
## these all work even when the value is NULL |
|
| 229 | 49x |
main_title(df) <- main_title |
| 230 | 49x |
main_footer(df) <- main_footer |
| 231 | 49x |
subtitles(df) <- subtitles |
| 232 | 49x |
prov_footer(df) <- prov_footer |
| 233 | 49x |
listing_dispcols(df) <- cols |
| 234 | ||
| 235 | 49x |
if (!is.null(split_into_pages_by_var)) {
|
| 236 | 1x |
df <- split_into_pages_by_var(df, split_into_pages_by_var) |
| 237 |
} |
|
| 238 | ||
| 239 | 49x |
df |
| 240 |
} |
|
| 241 | ||
| 242 |
#' @export |
|
| 243 |
#' @rdname listings |
|
| 244 |
as_keycol <- function(vec) {
|
|
| 245 | 77x |
if (is.factor(vec)) {
|
| 246 | 13x |
lab <- obj_label(vec) |
| 247 | 13x |
vec <- as.character(vec) |
| 248 | 13x |
obj_label(vec) <- lab |
| 249 |
} |
|
| 250 | 77x |
class(vec) <- c("listing_keycol", class(vec))
|
| 251 | 77x |
vec |
| 252 |
} |
|
| 253 | ||
| 254 |
#' @export |
|
| 255 |
#' @rdname listings |
|
| 256 |
is_keycol <- function(vec) {
|
|
| 257 | 6897x |
inherits(vec, "listing_keycol") |
| 258 |
} |
|
| 259 | ||
| 260 |
#' @export |
|
| 261 |
#' @rdname listings |
|
| 262 |
get_keycols <- function(df) {
|
|
| 263 | 341x |
names(which(sapply(df, is_keycol))) |
| 264 |
} |
|
| 265 | ||
| 266 |
#' @inherit formatters::matrix_form |
|
| 267 |
#' @param indent_rownames (`flag`)\cr silently ignored, as listings do not have row names |
|
| 268 |
#' nor indenting structure. |
|
| 269 |
#' @param expand_newlines (`flag`)\cr this should always be `TRUE` for listings. We keep it |
|
| 270 |
#' for debugging reasons. |
|
| 271 |
#' |
|
| 272 |
#' @return a [formatters::MatrixPrintForm] object. |
|
| 273 |
#' |
|
| 274 |
#' @seealso [formatters::matrix_form()] |
|
| 275 |
#' |
|
| 276 |
#' @examples |
|
| 277 |
#' lsting <- as_listing(mtcars) |
|
| 278 |
#' mf <- matrix_form(lsting) |
|
| 279 |
#' |
|
| 280 |
#' @export |
|
| 281 |
setMethod( |
|
| 282 |
"matrix_form", "listing_df", |
|
| 283 |
rix_form <- function(obj, indent_rownames = FALSE, expand_newlines = TRUE, fontspec = font_spec, col_gap = 3L) {
|
|
| 284 |
## we intentionally silently ignore indent_rownames because listings have |
|
| 285 |
## no rownames, but formatters::vert_pag_indices calls matrix_form(obj, TRUE) |
|
| 286 |
## unconditionally. |
|
| 287 | 134x |
cols <- attr(obj, "listing_dispcols") |
| 288 | 134x |
listing <- obj[, cols] |
| 289 | 134x |
atts <- attributes(obj) |
| 290 | 134x |
atts$names <- cols |
| 291 | 134x |
attributes(listing) <- atts |
| 292 | 134x |
keycols <- get_keycols(listing) |
| 293 | ||
| 294 | 134x |
bodymat <- matrix("",
|
| 295 | 134x |
nrow = nrow(listing), |
| 296 | 134x |
ncol = ncol(listing) |
| 297 |
) |
|
| 298 | ||
| 299 | 134x |
colnames(bodymat) <- names(listing) |
| 300 | ||
| 301 | 134x |
curkey <- "" |
| 302 | 134x |
for (i in seq_along(keycols)) {
|
| 303 | 196x |
kcol <- keycols[i] |
| 304 | 196x |
kcolvec <- listing[[kcol]] |
| 305 | 196x |
kcolvec <- vapply(kcolvec, format_value, "", format = obj_format(kcolvec), na_str = obj_na_str(kcolvec)) |
| 306 | 196x |
curkey <- paste0(curkey, kcolvec) |
| 307 | 196x |
disp <- c(TRUE, tail(curkey, -1) != head(curkey, -1)) |
| 308 | 196x |
bodymat[disp, kcol] <- kcolvec[disp] |
| 309 |
} |
|
| 310 | ||
| 311 | 134x |
nonkeycols <- setdiff(names(listing), keycols) |
| 312 | 134x |
if (length(nonkeycols) > 0) {
|
| 313 | 133x |
for (nonk in nonkeycols) {
|
| 314 | 402x |
vec <- listing[[nonk]] |
| 315 | 402x |
vec <- vapply(vec, format_value, "", format = obj_format(vec), na_str = obj_na_str(vec)) |
| 316 | 402x |
bodymat[, nonk] <- vec |
| 317 |
} |
|
| 318 |
} |
|
| 319 | ||
| 320 | 134x |
fullmat <- rbind( |
| 321 | 134x |
var_labels(listing, fill = TRUE), |
| 322 | 134x |
bodymat |
| 323 |
) |
|
| 324 | ||
| 325 | 134x |
colaligns <- rbind( |
| 326 | 134x |
rep("center", length(cols)),
|
| 327 | 134x |
matrix(sapply(listing, obj_align), |
| 328 | 134x |
ncol = length(cols), |
| 329 | 134x |
nrow = nrow(fullmat) - 1, |
| 330 | 134x |
byrow = TRUE |
| 331 |
) |
|
| 332 |
) |
|
| 333 | ||
| 334 | 134x |
MatrixPrintForm( |
| 335 | 134x |
strings = fullmat, |
| 336 | 134x |
spans = matrix(1, |
| 337 | 134x |
nrow = nrow(fullmat), |
| 338 | 134x |
ncol = ncol(fullmat) |
| 339 |
), |
|
| 340 | 134x |
ref_fnotes = list(), |
| 341 | 134x |
aligns = colaligns, |
| 342 | 134x |
formats = matrix(1, |
| 343 | 134x |
nrow = nrow(fullmat), |
| 344 | 134x |
ncol = ncol(fullmat) |
| 345 |
), |
|
| 346 | 134x |
listing_keycols = keycols, # It is always something |
| 347 | 134x |
row_info = make_row_df(obj, fontspec = fontspec), |
| 348 | 134x |
nlines_header = 1, # We allow only one level of headers and nl expansion happens after |
| 349 | 134x |
nrow_header = 1, |
| 350 | 134x |
has_topleft = FALSE, |
| 351 | 134x |
has_rowlabs = FALSE, |
| 352 | 134x |
expand_newlines = expand_newlines, |
| 353 | 134x |
main_title = main_title(obj), |
| 354 | 134x |
subtitles = subtitles(obj), |
| 355 | 134x |
page_titles = page_titles(obj), |
| 356 | 134x |
main_footer = main_footer(obj), |
| 357 | 134x |
prov_footer = prov_footer(obj), |
| 358 | 134x |
col_gap = col_gap, |
| 359 | 134x |
fontspec = fontspec, |
| 360 | 134x |
rep_cols = length(keycols) |
| 361 |
) |
|
| 362 |
} |
|
| 363 |
) |
|
| 364 | ||
| 365 |
#' @export |
|
| 366 |
#' @rdname listings |
|
| 367 | 179x |
listing_dispcols <- function(df) attr(df, "listing_dispcols") %||% character() |
| 368 | ||
| 369 |
#' @param new (`character`)\cr vector of names of columns to be added to |
|
| 370 |
#' the set of display columns. |
|
| 371 |
#' |
|
| 372 |
#' @export |
|
| 373 |
#' @rdname listings |
|
| 374 |
add_listing_dispcol <- function(df, new) {
|
|
| 375 | 20x |
listing_dispcols(df) <- c(listing_dispcols(df), new) |
| 376 | 20x |
df |
| 377 |
} |
|
| 378 | ||
| 379 |
#' @param value (`string`)\cr new value. |
|
| 380 |
#' |
|
| 381 |
#' @export |
|
| 382 |
#' @rdname listings |
|
| 383 |
`listing_dispcols<-` <- function(df, value) {
|
|
| 384 | 69x |
if (!is.character(value)) {
|
| 385 | ! |
stop( |
| 386 | ! |
"dispcols must be a character vector of column names, got ", |
| 387 | ! |
"object of class: ", paste(class(value), collapse = ",") |
| 388 |
) |
|
| 389 |
} |
|
| 390 | 69x |
chk <- setdiff(value, names(df)) ## remember setdiff is not symmetrical |
| 391 | 69x |
if (length(chk) > 0) {
|
| 392 | ! |
stop( |
| 393 | ! |
"listing display columns must be columns in the underlying data. ", |
| 394 | ! |
"Column(s) ", paste(chk, collapse = ", "), " not present in the data." |
| 395 |
) |
|
| 396 |
} |
|
| 397 | 69x |
attr(df, "listing_dispcols") <- unique(value) |
| 398 | 69x |
df |
| 399 |
} |
|
| 400 | ||
| 401 |
#' @inheritParams formatters::fmt_config |
|
| 402 |
#' @param name (`string`)\cr name of the existing or new column to be |
|
| 403 |
#' displayed when the listing is rendered. |
|
| 404 |
#' @param fun (`function` or `NULL`)\cr a function which accepts `df` and |
|
| 405 |
#' returns the vector for a new column, which is added to `df` as |
|
| 406 |
#' `name`, or `NULL` if marking an existing column as a listing column. |
|
| 407 |
#' |
|
| 408 |
#' @return `df` with `name` created (if necessary) and marked for |
|
| 409 |
#' display during rendering. |
|
| 410 |
#' |
|
| 411 |
#' @export |
|
| 412 |
#' @rdname listings |
|
| 413 |
add_listing_col <- function(df, |
|
| 414 |
name, |
|
| 415 |
fun = NULL, |
|
| 416 |
format = NULL, |
|
| 417 |
na_str = "NA", |
|
| 418 |
align = "left") {
|
|
| 419 | 20x |
if (!is.null(fun)) {
|
| 420 | 1x |
vec <- with_label(fun(df), name) |
| 421 | 19x |
} else if (name %in% names(df)) {
|
| 422 | 19x |
vec <- df[[name]] |
| 423 |
} else {
|
|
| 424 | ! |
stop( |
| 425 | ! |
"Column '", name, "' not found. name argument must specify an existing column when ", |
| 426 | ! |
"no generating function (fun argument) is specified." |
| 427 |
) |
|
| 428 |
} |
|
| 429 | ||
| 430 | 20x |
if (!is.null(format)) {
|
| 431 | 10x |
obj_format(vec) <- format |
| 432 |
} |
|
| 433 | ||
| 434 | 20x |
obj_na_str(vec) <- na_str |
| 435 | 20x |
obj_align(vec) <- align |
| 436 | ||
| 437 |
## this works for both new and existing columns |
|
| 438 | 20x |
df[[name]] <- vec |
| 439 | 20x |
df <- add_listing_dispcol(df, name) |
| 440 | 20x |
df |
| 441 |
} |
|
| 442 | ||
| 443 |
#' Split Listing by Values of a Variable |
|
| 444 |
#' |
|
| 445 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 446 |
#' |
|
| 447 |
#' Split is performed based on unique values of the given parameter present in the listing. |
|
| 448 |
#' Each listing can only be split by variable once. If this function is applied prior to |
|
| 449 |
#' pagination, parameter values will be separated by page. |
|
| 450 |
#' |
|
| 451 |
#' @param lsting listing_df. The listing to split. |
|
| 452 |
#' @param var character. Name of the variable to split on. |
|
| 453 |
#' @param page_prefix character. Prefix to be appended with the split value (`var` level), |
|
| 454 |
#' at the end of the subtitles, corresponding to each resulting list element (listing). |
|
| 455 |
#' |
|
| 456 |
#' @return A list of `lsting_df` objects each corresponding to a unique value of `var`. |
|
| 457 |
#' |
|
| 458 |
#' @note This function should only be used after the complete listing has been created. The |
|
| 459 |
#' listing cannot be modified further after applying this function. |
|
| 460 |
#' |
|
| 461 |
#' @examples |
|
| 462 |
#' dat <- ex_adae[1:20, ] |
|
| 463 |
#' |
|
| 464 |
#' lsting <- as_listing( |
|
| 465 |
#' dat, |
|
| 466 |
#' key_cols = c("USUBJID", "AGE"),
|
|
| 467 |
#' disp_cols = "SEX", |
|
| 468 |
#' main_title = "title", |
|
| 469 |
#' main_footer = "footer" |
|
| 470 |
#' ) %>% |
|
| 471 |
#' add_listing_col("BMRKR1", format = "xx.x") %>%
|
|
| 472 |
#' split_into_pages_by_var("SEX")
|
|
| 473 |
#' |
|
| 474 |
#' lsting |
|
| 475 |
#' |
|
| 476 |
#' @export |
|
| 477 |
split_into_pages_by_var <- function(lsting, var, page_prefix = var) {
|
|
| 478 | 5x |
checkmate::assert_class(lsting, "listing_df") |
| 479 | 5x |
checkmate::assert_choice(var, names(lsting)) |
| 480 | ||
| 481 | 5x |
lsting_by_var <- list() |
| 482 | 5x |
for (lvl in unique(lsting[[var]])) {
|
| 483 | 10x |
var_desc <- paste0(page_prefix, ": ", lvl) |
| 484 | 10x |
lsting_by_var[[lvl]] <- lsting[lsting[[var]] == lvl, ] |
| 485 | 10x |
subtitles(lsting_by_var[[lvl]]) <- c(subtitles(lsting), var_desc) |
| 486 |
} |
|
| 487 | ||
| 488 | 5x |
lsting_by_var |
| 489 |
} |
| 1 | ||
| 2 |
## XXX this historically has been 1, but it actually should be 1.2!!!!! |
|
| 3 |
dflt_courier <- font_spec("Courier", 9, 1)
|
|
| 4 | ||
| 5 |
#' Methods for `listing_df` objects |
|
| 6 |
#' |
|
| 7 |
#' See core documentation in [formatters::formatters-package] for descriptions of these functions. |
|
| 8 |
#' |
|
| 9 |
#' @inheritParams formatters::toString |
|
| 10 |
#' @param x (`listing_df`)\cr the listing. |
|
| 11 |
#' @param ... additional parameters passed to [formatters::toString()]. |
|
| 12 |
#' |
|
| 13 |
#' @method print listing_df |
|
| 14 |
#' |
|
| 15 |
#' @export |
|
| 16 |
#' @name listing_methods |
|
| 17 |
print.listing_df <- function(x, widths = NULL, tf_wrap = FALSE, max_width = NULL, fontspec = NULL, col_gap = 3L, ...) {
|
|
| 18 | ! |
cat( |
| 19 | ! |
toString( |
| 20 | ! |
matrix_form(x, fontspec = fontspec, col_gap = col_gap), |
| 21 | ! |
widths = widths, |
| 22 | ! |
tf_wrap = tf_wrap, |
| 23 | ! |
max_width = max_width, |
| 24 | ! |
fontspec = fontspec, |
| 25 | ! |
col_gap = col_gap, |
| 26 |
... |
|
| 27 |
) |
|
| 28 |
) |
|
| 29 | ! |
invisible(x) |
| 30 |
} |
|
| 31 | ||
| 32 |
#' @exportMethod toString |
|
| 33 |
#' @name listing_methods |
|
| 34 |
#' @aliases toString,listing_df-method |
|
| 35 |
setMethod("toString", "listing_df", function(x, widths = NULL, fontspec = NULL, col_gap = 3L, ...) {
|
|
| 36 | 4x |
toString( |
| 37 | 4x |
matrix_form(x, fontspec = fontspec, col_gap = col_gap), |
| 38 | 4x |
fontspec = fontspec, |
| 39 | 4x |
col_gap = col_gap, |
| 40 | 4x |
widths = widths, |
| 41 |
... |
|
| 42 |
) |
|
| 43 |
}) |
|
| 44 | ||
| 45 |
## because rle in base is too much of a stickler for being atomic |
|
| 46 |
basic_run_lens <- function(x) {
|
|
| 47 | 134x |
n <- length(x) |
| 48 | 134x |
if (n == 0) {
|
| 49 | ! |
return(integer()) |
| 50 |
} |
|
| 51 | ||
| 52 | 134x |
y <- x[-1L] != x[-n] |
| 53 | 134x |
i <- c(which(y), n) |
| 54 | 134x |
diff(c(0L, i)) |
| 55 |
} |
|
| 56 | ||
| 57 |
#' @param df (`listing_df`)\cr the listing. |
|
| 58 |
#' @param colnm (`string`)\cr column name. |
|
| 59 |
#' @param colvec (`vector`)\cr column values based on `colnm`. |
|
| 60 |
#' |
|
| 61 |
#' @rdname vec_nlines |
|
| 62 |
#' @keywords internal |
|
| 63 |
format_colvector <- function(df, colnm, colvec = df[[colnm]]) {
|
|
| 64 | 600x |
if (missing(colvec) && !(colnm %in% names(df))) {
|
| 65 | ! |
stop("column ", colnm, " not found")
|
| 66 |
} |
|
| 67 | 600x |
na_str <- obj_na_str(colvec) |
| 68 | 600x |
if (is.null(na_str) || all(is.na(na_str))) {
|
| 69 | ! |
na_str <- rep("-", max(1L, length(na_str)))
|
| 70 |
} |
|
| 71 | ||
| 72 | 600x |
strvec <- vapply(colvec, format_value, "", format = obj_format(colvec), na_str = na_str) |
| 73 | 600x |
strvec |
| 74 |
} |
|
| 75 | ||
| 76 |
#' Utilities for formatting a listing column |
|
| 77 |
#' |
|
| 78 |
#' For `vec_nlines`, calculate the number of lines each element of a column vector will |
|
| 79 |
#' take to render. For `format_colvector`, |
|
| 80 |
#' |
|
| 81 |
#' @param vec (`vector`)\cr a column vector to be rendered into ASCII. |
|
| 82 |
#' @param max_width (`numeric(1)` or `NULL`)\cr the width to render the column with. |
|
| 83 |
#' @return (`numeric`)\cr a vector of the number of lines element-wise that will be |
|
| 84 |
#' needed to render the elements of `vec` to width `max_width`. |
|
| 85 |
#' |
|
| 86 |
#' @keywords internal |
|
| 87 | 600x |
setGeneric("vec_nlines", function(vec, max_width = NULL, fontspec = dflt_courier) standardGeneric("vec_nlines"))
|
| 88 | ||
| 89 |
#' @param vec (`vector`)\cr a vector. |
|
| 90 |
#' |
|
| 91 |
#' @rdname vec_nlines |
|
| 92 |
#' @keywords internal |
|
| 93 |
setMethod("vec_nlines", "ANY", function(vec, max_width = NULL, fontspec = dflt_courier) {
|
|
| 94 | 600x |
if (is.null(max_width)) {
|
| 95 | 600x |
max_width <- floor(0.9 * getOption("width")) # default of base::strwrap
|
| 96 |
# NB: flooring as it is used as <= (also in base::strwrap) |
|
| 97 |
} |
|
| 98 |
# in formatters for characters |
|
| 99 | 600x |
unlist(lapply(format_colvector(colvec = vec), nlines, max_width = max_width, fontspec = fontspec)) |
| 100 |
}) |
|
| 101 | ||
| 102 |
## setMethod("vec_nlines", "character", function(vec, max_width = NULL) {
|
|
| 103 |
## strvec <- wrap_txt(format_colvector(colvec = vec), width = max_width, collapse = "\n") |
|
| 104 |
## mtchs <- gregexpr("\n", strvec, fixed = TRUE)
|
|
| 105 |
## 1L + vapply(mtchs, function(vi) sum(vi > 0), 1L) |
|
| 106 |
## }) |
|
| 107 | ||
| 108 |
## setMethod("vec_nlines", "factor", function(vec, max_width = NULL) {
|
|
| 109 |
## lvl_nlines <- vec_nlines(levels(vec), max_width = max_width) |
|
| 110 |
## ret <- lvl_nlines[vec] |
|
| 111 |
## ret[is.na(ret)] <- format_value(NA_character |
|
| 112 |
## }) |
|
| 113 | ||
| 114 |
#' Make pagination data frame for a listing |
|
| 115 |
#' |
|
| 116 |
#' @inheritParams formatters::make_row_df |
|
| 117 |
#' @param tt (`listing_df`)\cr the listing to be rendered. |
|
| 118 |
#' @param visible_only (`flag`)\cr ignored, as listings do not have |
|
| 119 |
#' non-visible structural elements. |
|
| 120 |
#' |
|
| 121 |
#' @return a `data.frame` with pagination information. |
|
| 122 |
#' |
|
| 123 |
#' @seealso [formatters::make_row_df()] |
|
| 124 |
#' |
|
| 125 |
#' @examples |
|
| 126 |
#' lsting <- as_listing(mtcars) |
|
| 127 |
#' mf <- matrix_form(lsting) |
|
| 128 |
#' |
|
| 129 |
#' @export |
|
| 130 |
setMethod( |
|
| 131 |
"make_row_df", "listing_df", |
|
| 132 |
function(tt, colwidths = NULL, visible_only = TRUE, |
|
| 133 |
rownum = 0, |
|
| 134 |
indent = 0L, |
|
| 135 |
path = character(), |
|
| 136 |
incontent = FALSE, |
|
| 137 |
repr_ext = 0L, |
|
| 138 |
repr_inds = integer(), |
|
| 139 |
sibpos = NA_integer_, |
|
| 140 |
nsibs = NA_integer_, |
|
| 141 |
fontspec = dflt_courier) {
|
|
| 142 |
## assume sortedness by keycols |
|
| 143 | 135x |
keycols <- get_keycols(tt) |
| 144 | 135x |
dispcols <- listing_dispcols(tt) |
| 145 | 135x |
abs_rownumber <- seq_along(tt[[1]]) |
| 146 | 135x |
if (length(keycols) >= 1) {
|
| 147 | 134x |
runlens <- basic_run_lens(tt[[tail(keycols, 1)]]) |
| 148 |
} else {
|
|
| 149 | 1x |
runlens <- rep(1, NROW(tt)) |
| 150 |
} |
|
| 151 | 135x |
sibpos <- unlist(lapply(runlens, seq_len)) |
| 152 | 135x |
nsibs <- rep(runlens, times = runlens) |
| 153 | 135x |
extents <- rep(1L, nrow(tt)) |
| 154 | 135x |
if (length(colwidths) > 0 && length(colwidths) != length(dispcols)) {
|
| 155 | ! |
stop( |
| 156 | ! |
"Non-null colwidths vector must be the same length as the number of display columns.\n", |
| 157 | ! |
"Got: ", length(colwidths), "(", length(dispcols), " disp cols)."
|
| 158 |
) |
|
| 159 |
} |
|
| 160 | 135x |
if (length(colwidths) > 0) {
|
| 161 | ! |
names(colwidths) <- dispcols |
| 162 |
} |
|
| 163 |
## extents is a row-wise vector of extents, for each col, we update |
|
| 164 |
## if that column has any rows wider than the previously recorded extent. |
|
| 165 | 135x |
for (col in dispcols) {
|
| 166 |
## duplicated from matrix_form method, refactor! |
|
| 167 | 600x |
col_ext <- vec_nlines(tt[[col]], max_width = colwidths[col], fontspec = fontspec) |
| 168 | 600x |
extents <- ifelse(col_ext > extents, col_ext, extents) |
| 169 |
} |
|
| 170 | 135x |
ret <- data.frame( |
| 171 | 135x |
label = "", name = "", |
| 172 | 135x |
abs_rownumber = abs_rownumber, |
| 173 | 135x |
path = I(as.list(rep(NA_character_, NROW(tt)))), |
| 174 | 135x |
pos_in_siblings = sibpos, |
| 175 | 135x |
n_siblings = nsibs, |
| 176 | 135x |
self_extent = extents, |
| 177 | 135x |
par_extent = 0L, |
| 178 | 135x |
reprint_inds = I(replicate(NROW(tt), list(integer()))), |
| 179 | 135x |
node_class = "listing_df", |
| 180 | 135x |
indent = 0L, |
| 181 | 135x |
nrowrefs = 0L, ## XXX this doesn't support footnotes |
| 182 | 135x |
ncellrefs = 0L, ## XXX this doesn't support footnotes |
| 183 | 135x |
nreflines = 0L, ## XXX this doesn't support footnotes |
| 184 | 135x |
force_page = FALSE, |
| 185 | 135x |
page_title = NA_character_, |
| 186 | 135x |
trailing_sep = NA_character_ |
| 187 |
) |
|
| 188 | 135x |
stopifnot(identical( |
| 189 | 135x |
names(ret), |
| 190 | 135x |
names(pagdfrow( |
| 191 | 135x |
nm = "", lab = "", rnum = 1L, pth = NA_character_, extent = 1L, |
| 192 | 135x |
rclass = "" |
| 193 |
)) |
|
| 194 |
)) |
|
| 195 | 135x |
ret |
| 196 |
} |
|
| 197 |
) |
|
| 198 | ||
| 199 |
## tt$sibpos <- unlist(lapply( |
|
| 200 |
## ## don't support pathing for now |
|
| 201 |
## tt$path <- I(lapply(1:NROW(tt), |
|
| 202 |
## function(i) {
|
|
| 203 |
## retpath <- character(2*length(keycols)) |
|
| 204 |
## for(j in seq_along(keycols)) {
|
|
| 205 |
## retpath[2*j - 1] <- keycols[j] |
|
| 206 |
## retpath[2*j] <- tt[i, keycols[j], drop = TRUE] |
|
| 207 |
## } |
|
| 208 |
## retpath |
|
| 209 |
## })) |
|
| 210 |
## spl <- split(tt, tt[keycols]) |
|
| 211 |
## spl <- spl[vapply(spl, function(y) NROW(y) > 0, NA)] |
|
| 212 |
## dfs <- lapply(spl, function(df) {
|
|
| 213 |
## df <- df[order(df$abs_rownumber),] |
|
| 214 |
## ndf <- NROW(df) |
|
| 215 |
## lapply(1:ndf, function(i) {
|
|
| 216 |
## rw <- df[i,] |
|
| 217 |
## stopifnot(nrow(rw) == 1) |
|
| 218 |
## pagdfrow(nm = "", |
|
| 219 |
## lab = "", |
|
| 220 |
## rnum = rw$abs_rownumber, |
|
| 221 |
## pth = NA_character_, |
|
| 222 |
## sibpos = i, |
|
| 223 |
## nsibs = ndf, |
|
| 224 |
## extent = 1L, |
|
| 225 |
## rclass = "listing_df", |
|
| 226 |
## repind = integer()) |
|
| 227 |
## }) |
|
| 228 |
## }) |
|
| 229 |
## ret <- do.call(rbind, unlist(dfs, recursive = FALSE)) |
|
| 230 |
## ret <- ret[order(ret$abs_rownumber),] |
|
| 231 |
## ret |
|
| 232 |
## }) |
|
| 233 | ||
| 234 |
#' @inheritParams base::Extract |
|
| 235 |
#' @param x (`listing_df`)\cr the listing. |
|
| 236 |
#' @param i (`any`)\cr object passed to base `[` methods. |
|
| 237 |
#' @param j (`any`)\cr object passed to base `[` methods. |
|
| 238 |
#' |
|
| 239 |
#' @export |
|
| 240 |
#' @aliases [,listing_df-method |
|
| 241 |
#' @rdname listing_methods |
|
| 242 |
setMethod( |
|
| 243 |
"[", "listing_df", |
|
| 244 |
function(x, i, j, drop = FALSE) {
|
|
| 245 | ! |
xattr <- attributes(x) |
| 246 | ! |
xattr$names <- xattr$names[j] |
| 247 | ! |
res <- NextMethod() |
| 248 | ! |
if (!drop) {
|
| 249 | ! |
attributes(res) <- xattr |
| 250 |
} |
|
| 251 | ! |
res |
| 252 |
} |
|
| 253 |
) |
|
| 254 | ||
| 255 |
#' @rdname listing_methods |
|
| 256 |
#' @param obj (`listing_df`)\cr the listing. |
|
| 257 |
#' |
|
| 258 |
#' @return |
|
| 259 |
#' * Accessor methods return the value of the aspect of `obj`. |
|
| 260 |
#' * Setter methods return `obj` with the relevant element of the listing updated. |
|
| 261 |
#' |
|
| 262 |
#' @examples |
|
| 263 |
#' lsting <- as_listing(mtcars) |
|
| 264 |
#' main_title(lsting) <- "Hi there" |
|
| 265 |
#' |
|
| 266 |
#' main_title(lsting) |
|
| 267 |
#' |
|
| 268 |
#' @export |
|
| 269 |
setMethod( |
|
| 270 |
"main_title", "listing_df", |
|
| 271 | 137x |
function(obj) attr(obj, "main_title") %||% character() |
| 272 |
) |
|
| 273 | ||
| 274 |
#' @rdname listing_methods |
|
| 275 |
#' @export |
|
| 276 |
setMethod( |
|
| 277 |
"subtitles", "listing_df", |
|
| 278 | 147x |
function(obj) attr(obj, "subtitles") %||% character() |
| 279 |
) |
|
| 280 | ||
| 281 |
#' @rdname listing_methods |
|
| 282 |
#' @export |
|
| 283 |
setMethod( |
|
| 284 |
"main_footer", "listing_df", |
|
| 285 | 136x |
function(obj) attr(obj, "main_footer") %||% character() |
| 286 |
) |
|
| 287 | ||
| 288 |
#' @rdname listing_methods |
|
| 289 |
#' @export |
|
| 290 |
setMethod( |
|
| 291 |
"prov_footer", "listing_df", |
|
| 292 | 136x |
function(obj) attr(obj, "prov_footer") %||% character() |
| 293 |
) |
|
| 294 | ||
| 295 |
.chk_value <- function(val, fname, len_one = FALSE, null_ok = TRUE) {
|
|
| 296 | 222x |
if (null_ok && is.null(val)) {
|
| 297 | 178x |
return(TRUE) |
| 298 |
} |
|
| 299 | 44x |
if (!is.character(val)) {
|
| 300 | 4x |
stop("value for ", fname, " must be a character, got ",
|
| 301 | 4x |
"object of class: ", paste(class(val), collapse = ","), |
| 302 | 4x |
call. = FALSE |
| 303 |
) |
|
| 304 |
} |
|
| 305 | 40x |
if (len_one && length(val) > 1) {
|
| 306 | 1x |
stop( |
| 307 | 1x |
"value for ", fname, " must be length <= 1, got ", |
| 308 | 1x |
"vector of length ", length(val) |
| 309 |
) |
|
| 310 |
} |
|
| 311 | 39x |
TRUE |
| 312 |
} |
|
| 313 | ||
| 314 |
#' @rdname listing_methods |
|
| 315 |
#' @export |
|
| 316 |
setMethod( |
|
| 317 |
"main_title<-", "listing_df", |
|
| 318 |
function(obj, value) {
|
|
| 319 |
## length 1 restriction is to match rtables behavior |
|
| 320 |
## which currently enforces this (though incompletely) |
|
| 321 | 55x |
.chk_value(value, "main_title", len_one = TRUE) |
| 322 | 53x |
attr(obj, "main_title") <- value |
| 323 | 53x |
obj |
| 324 |
} |
|
| 325 |
) |
|
| 326 | ||
| 327 |
#' @rdname listing_methods |
|
| 328 |
#' @export |
|
| 329 |
setMethod( |
|
| 330 |
"subtitles<-", "listing_df", |
|
| 331 |
function(obj, value) {
|
|
| 332 | 62x |
.chk_value(value, "subtitles") |
| 333 | 61x |
attr(obj, "subtitles") <- value |
| 334 | 61x |
obj |
| 335 |
} |
|
| 336 |
) |
|
| 337 | ||
| 338 |
#' @rdname listing_methods |
|
| 339 |
#' @export |
|
| 340 |
setMethod( |
|
| 341 |
"main_footer<-", "listing_df", |
|
| 342 |
function(obj, value) {
|
|
| 343 | 53x |
.chk_value(value, "main_footer") |
| 344 | 52x |
attr(obj, "main_footer") <- value |
| 345 | 52x |
obj |
| 346 |
} |
|
| 347 |
) |
|
| 348 | ||
| 349 |
#' @rdname listing_methods |
|
| 350 |
#' @export |
|
| 351 |
setMethod( |
|
| 352 |
"prov_footer<-", "listing_df", |
|
| 353 |
function(obj, value) {
|
|
| 354 | 52x |
.chk_value(value, "prov_footer") |
| 355 | 51x |
attr(obj, "prov_footer") <- value |
| 356 | 51x |
obj |
| 357 |
} |
|
| 358 |
) |
|
| 359 | ||
| 360 |
#' @rdname listing_methods |
|
| 361 |
#' @export |
|
| 362 |
setMethod( |
|
| 363 |
"num_rep_cols", "listing_df", |
|
| 364 |
function(obj) {
|
|
| 365 | 71x |
length(get_keycols(obj)) |
| 366 |
} |
|
| 367 |
) |