| 1 |
setOldClass(c("listing_df", "tbl_df", "tbl", "data.frame"))
|
|
| 2 |
setOldClass(c("MatrixPrintForm", "list"))
|
|
| 3 | ||
| 4 |
#' @rdname listings |
|
| 5 |
#' @title Create a Listing from a `data.frame` or `tibble` |
|
| 6 |
#' |
|
| 7 |
#' @description `r lifecycle::badge("experimental")`
|
|
| 8 |
#' |
|
| 9 |
#' Creates listings by using `cols` and `key_cols` to produce a compact and |
|
| 10 |
#' elegant representation of the `data.frame` or `tibble` in input. |
|
| 11 |
#' |
|
| 12 |
#' @param df data.frame or listing_df. The (non-listing) data.frame to be converted to a listing or |
|
| 13 |
#' the listing_df to be modified. |
|
| 14 |
#' @param key_cols character. Names of columns which should be treated as *key columns* |
|
| 15 |
#' when rendering the listing. Key columns allow you to group repeat occurrences. |
|
| 16 |
#' @param disp_cols character or NULL. Names of non-key columns which should be displayed when |
|
| 17 |
#' the listing is rendered. Defaults to all columns of `df` not named in `key_cols` or |
|
| 18 |
#' `non_disp_cols`. |
|
| 19 |
#' @param non_disp_cols character or NULL. Names of non-key columns to be excluded as display |
|
| 20 |
#' columns. All other non-key columns are then treated as display columns. Invalid if |
|
| 21 |
#' `disp_cols` is non-NULL. |
|
| 22 |
#' @param unique_rows logical(1). Should only unique rows be included in the listing. Defaults to `FALSE`. |
|
| 23 |
#' @param default_formatting list. A named list of default column format configurations to apply when rendering the |
|
| 24 |
#' listing. Each name-value pair consists of a name corresponding to a data class (or "numeric" for all unspecified |
|
| 25 |
#' numeric classes) and a value of type `fmt_config` with the format configuration that should be implemented for |
|
| 26 |
#' columns of that class. If named element "all" is included in the list, this configuration will be used for all |
|
| 27 |
#' data classes not specified. Objects of type `fmt_config` can take 3 arguments: `format`, `na_str`, and `align`. |
|
| 28 |
#' @param col_formatting list. A named list of custom column formatting configurations to apply to specific columns |
|
| 29 |
#' when rendering the listing. Each name-value pair consists of a name corresponding to a column name and a value of |
|
| 30 |
#' type `fmt_config` with the formatting configuration that should be implemented for that column. Objects of type |
|
| 31 |
#' `fmt_config` can take 3 arguments: `format`, `na_str`, and `align`. Defaults to `NULL`. |
|
| 32 |
#' @param main_title character(1) or NULL. The main title for the listing, or |
|
| 33 |
#' `NULL` (the default). Must be length 1 non-NULL. |
|
| 34 |
#' @param subtitles character or NULL. A vector of subtitle(s) for the |
|
| 35 |
#' listing, or `NULL` (the default). |
|
| 36 |
#' @param main_footer character or NULL. A vector of main footer lines |
|
| 37 |
#' for the listing, or `NULL` (the default). |
|
| 38 |
#' @param prov_footer character or NULL. A vector of provenance strings |
|
| 39 |
#' for the listing, or `NULL` (the default). Each string element is placed on a new line. |
|
| 40 |
#' @param vec any. A column vector from a `listing_df` to be annotated as a key column. |
|
| 41 |
#' |
|
| 42 |
#' @return A `listing_df` object, sorted by the key columns. |
|
| 43 |
#' |
|
| 44 |
#' @details 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 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 |
#' |
|
| 74 |
#' @examples |
|
| 75 |
#' dat <- ex_adae |
|
| 76 |
#' |
|
| 77 |
#' # This example demonstrates the listing with key_cols (values are grouped by USUBJID) and |
|
| 78 |
#' # multiple lines in prov_footer |
|
| 79 |
#' lsting <- as_listing(dat[1:25, ], |
|
| 80 |
#' key_cols = c("USUBJID", "AESOC"),
|
|
| 81 |
#' main_title = "Example Title for Listing", |
|
| 82 |
#' subtitles = "This is the subtitle for this Adverse Events Table", |
|
| 83 |
#' main_footer = "Main footer for the listing", |
|
| 84 |
#' prov_footer = c( |
|
| 85 |
#' "You can even add a subfooter", "Second element is place on a new line", |
|
| 86 |
#' "Third string" |
|
| 87 |
#' ) |
|
| 88 |
#' ) %>% |
|
| 89 |
#' add_listing_col("AETOXGR") %>%
|
|
| 90 |
#' add_listing_col("BMRKR1", format = "xx.x") %>%
|
|
| 91 |
#' add_listing_col("AESER / AREL", fun = function(df) paste(df$AESER, df$AREL, sep = " / "))
|
|
| 92 |
#' |
|
| 93 |
#' mat <- matrix_form(lsting) |
|
| 94 |
#' |
|
| 95 |
#' cat(toString(mat)) |
|
| 96 |
#' |
|
| 97 |
#' # This example demonstrates the listing table without key_cols |
|
| 98 |
#' # and specifying the cols with disp_cols. |
|
| 99 |
#' dat <- ex_adae |
|
| 100 |
#' lsting <- as_listing(dat[1:25, ], |
|
| 101 |
#' disp_cols = c("USUBJID", "AESOC", "RACE", "AETOXGR", "BMRKR1")
|
|
| 102 |
#' ) |
|
| 103 |
#' |
|
| 104 |
#' mat <- matrix_form(lsting) |
|
| 105 |
#' |
|
| 106 |
#' cat(toString(mat)) |
|
| 107 |
#' |
|
| 108 |
#' # This example demonstrates a listing with format configurations specified |
|
| 109 |
#' # via the default_formatting and col_formatting arguments |
|
| 110 |
#' dat <- ex_adae |
|
| 111 |
#' dat$AENDY[3:6] <- NA |
|
| 112 |
#' lsting <- as_listing(dat[1:25, ], |
|
| 113 |
#' key_cols = c("USUBJID", "AESOC"),
|
|
| 114 |
#' disp_cols = c("STUDYID", "SEX", "ASEQ", "RANDDT", "ASTDY", "AENDY"),
|
|
| 115 |
#' default_formatting = list( |
|
| 116 |
#' all = fmt_config(align = "left"), |
|
| 117 |
#' numeric = fmt_config( |
|
| 118 |
#' format = "xx.xx", |
|
| 119 |
#' na_str = "<No data>", |
|
| 120 |
#' align = "right" |
|
| 121 |
#' ) |
|
| 122 |
#' ) |
|
| 123 |
#' ) %>% |
|
| 124 |
#' add_listing_col("BMRKR1", format = "xx.x", align = "center")
|
|
| 125 |
#' |
|
| 126 |
#' mat <- matrix_form(lsting) |
|
| 127 |
#' |
|
| 128 |
#' cat(toString(mat)) |
|
| 129 |
#' |
|
| 130 |
#' @export |
|
| 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 | 36x |
if (length(non_disp_cols) > 0 && length(intersect(key_cols, non_disp_cols)) > 0) {
|
| 143 | 1x |
stop("Key column also listed in non_disp_cols. All key columns are by",
|
| 144 | 1x |
" definition display columns.") |
| 145 |
} |
|
| 146 | 35x |
if (!is.null(disp_cols) && !is.null(non_disp_cols)) {
|
| 147 | 1x |
stop("Got non-null values for both disp_cols and non_disp_cols. This is not supported.")
|
| 148 | 34x |
} else if (is.null(disp_cols)) {
|
| 149 |
## non_disp_cols NULL is ok here |
|
| 150 | 11x |
cols <- setdiff(names(df), c(key_cols, non_disp_cols)) |
| 151 |
} else {
|
|
| 152 |
## disp_cols non-null, non_disp_cols NULL |
|
| 153 | 23x |
cols <- disp_cols |
| 154 |
} |
|
| 155 | 34x |
if (!all(sapply(default_formatting, is, class2 = "fmt_config"))) {
|
| 156 | ! |
stop("All format configurations supplied in `default_formatting`",
|
| 157 | ! |
" must be of type `fmt_config`.") |
| 158 |
} |
|
| 159 | 34x |
if (!(is.null(col_formatting) || all(sapply(col_formatting, is, class2 = "fmt_config")))) {
|
| 160 | ! |
stop("All format configurations supplied in `col_formatting`",
|
| 161 | ! |
" must be of type `fmt_config`.") |
| 162 |
} |
|
| 163 | ||
| 164 | 34x |
df <- as_tibble(df) |
| 165 | 34x |
varlabs <- var_labels(df, fill = TRUE) |
| 166 | 34x |
o <- do.call(order, df[key_cols]) |
| 167 | 34x |
if (is.unsorted(o)) {
|
| 168 | 16x |
message("sorting incoming data by key columns")
|
| 169 | 16x |
df <- df[o, ] |
| 170 |
} |
|
| 171 | ||
| 172 |
## reorder the full set of cols to ensure key columns are first |
|
| 173 | 34x |
ordercols <- c(key_cols, setdiff(names(df), key_cols)) |
| 174 | 34x |
df <- df[, ordercols] |
| 175 | 34x |
var_labels(df) <- varlabs[ordercols] |
| 176 | ||
| 177 | 34x |
for (cnm in key_cols) {
|
| 178 | 51x |
df[[cnm]] <- as_keycol(df[[cnm]]) |
| 179 |
} |
|
| 180 | ||
| 181 |
## key cols must be leftmost cols |
|
| 182 | 34x |
cols <- c(key_cols, setdiff(cols, key_cols)) |
| 183 | ||
| 184 | 34x |
row_all_na <- apply(df[cols], 1, function(x) all(is.na(x))) |
| 185 | 34x |
if (any(row_all_na)) {
|
| 186 | 1x |
message("rows that only contain NA values have been trimmed")
|
| 187 | 1x |
df <- df[!row_all_na, ] |
| 188 |
} |
|
| 189 | ||
| 190 |
# set col format configs |
|
| 191 | 34x |
df[cols] <- lapply(cols, function(col) {
|
| 192 | 214x |
col_class <- tail(class(df[[col]]), 1) |
| 193 | 214x |
col_fmt_class <- if (!col_class %in% names(default_formatting) && is.numeric(df[[col]])) "numeric" else col_class |
| 194 | 214x |
col_fmt <- if (col %in% names(col_formatting)) {
|
| 195 | 2x |
col_formatting[[col]] |
| 196 | 214x |
} else if (col_fmt_class %in% names(default_formatting)) {
|
| 197 | 6x |
default_formatting[[col_fmt_class]] |
| 198 |
} else {
|
|
| 199 | 206x |
if (!"all" %in% names(default_formatting)) {
|
| 200 | ! |
stop("Format configurations must be supplied for all listing columns. ",
|
| 201 | ! |
"To cover all remaining columns please add an 'all' configuration", |
| 202 | ! |
" to `default_formatting`.") |
| 203 |
} |
|
| 204 | 206x |
default_formatting[["all"]] |
| 205 |
} |
|
| 206 |
# ANY attr <- fmt_config slot |
|
| 207 | 214x |
obj_format(df[[col]]) <- obj_format(col_fmt) |
| 208 | 214x |
obj_na_str(df[[col]]) <- if (is.null(obj_na_str(col_fmt))) "NA" else obj_na_str(col_fmt) |
| 209 | 214x |
obj_align(df[[col]]) <- if (is.null(obj_align(col_fmt))) "left" else obj_align(col_fmt) |
| 210 | 214x |
df[[col]] |
| 211 |
}) |
|
| 212 | ||
| 213 | 2x |
if (unique_rows) df <- df[!duplicated(df[, cols]), ] |
| 214 | ||
| 215 | 34x |
class(df) <- c("listing_df", class(df))
|
| 216 |
## these all work even when the value is NULL |
|
| 217 | 34x |
main_title(df) <- main_title |
| 218 | 34x |
main_footer(df) <- main_footer |
| 219 | 34x |
subtitles(df) <- subtitles |
| 220 | 34x |
prov_footer(df) <- prov_footer |
| 221 | 34x |
listing_dispcols(df) <- cols |
| 222 | 34x |
df |
| 223 |
} |
|
| 224 | ||
| 225 | ||
| 226 |
#' @export |
|
| 227 |
#' @rdname listings |
|
| 228 |
as_keycol <- function(vec) {
|
|
| 229 | 51x |
if (is.factor(vec)) {
|
| 230 | 11x |
lab <- obj_label(vec) |
| 231 | 11x |
vec <- as.character(vec) |
| 232 | 11x |
obj_label(vec) <- lab |
| 233 |
} |
|
| 234 | 51x |
class(vec) <- c("listing_keycol", class(vec))
|
| 235 | 51x |
vec |
| 236 |
} |
|
| 237 | ||
| 238 | ||
| 239 |
#' @export |
|
| 240 |
#' @rdname listings |
|
| 241 |
is_keycol <- function(vec) {
|
|
| 242 | 3538x |
inherits(vec, "listing_keycol") |
| 243 |
} |
|
| 244 | ||
| 245 | ||
| 246 | ||
| 247 |
#' @export |
|
| 248 |
#' @rdname listings |
|
| 249 |
get_keycols <- function(df) {
|
|
| 250 | 176x |
names(which(sapply(df, is_keycol))) |
| 251 |
} |
|
| 252 | ||
| 253 |
#' @export |
|
| 254 |
#' @inherit formatters::matrix_form |
|
| 255 |
#' @seealso [formatters::matrix_form()] |
|
| 256 |
#' @param indent_rownames logical(1). Silently ignored, as listings do not have row names |
|
| 257 |
#' nor indenting structure. |
|
| 258 |
#' |
|
| 259 |
#' @examples |
|
| 260 |
#' |
|
| 261 |
#' lsting <- as_listing(mtcars) |
|
| 262 |
#' mf <- matrix_form(lsting) |
|
| 263 |
#' |
|
| 264 |
#' @return a `MatrixPrintForm` object |
|
| 265 |
setMethod( |
|
| 266 |
"matrix_form", "listing_df", |
|
| 267 |
rix_form <- function(obj, indent_rownames = FALSE) {
|
|
| 268 |
## we intentionally silently ignore indent_rownames because listings have |
|
| 269 |
## no rownames, but formatters::vert_pag_indices calls matrix_form(obj, TRUE) |
|
| 270 |
## unconditionally. |
|
| 271 | 76x |
cols <- attr(obj, "listing_dispcols") |
| 272 | 76x |
listing <- obj[, cols] |
| 273 | 76x |
atts <- attributes(obj) |
| 274 | 76x |
atts$names <- cols |
| 275 | 76x |
attributes(listing) <- atts |
| 276 | ||
| 277 | 76x |
keycols <- get_keycols(listing) |
| 278 | ||
| 279 | ||
| 280 | 76x |
bodymat <- matrix("",
|
| 281 | 76x |
nrow = nrow(listing), |
| 282 | 76x |
ncol = ncol(listing) |
| 283 |
) |
|
| 284 | ||
| 285 | 76x |
colnames(bodymat) <- names(listing) |
| 286 | ||
| 287 | ||
| 288 | 76x |
curkey <- "" |
| 289 | 76x |
for (i in seq_along(keycols)) {
|
| 290 | 103x |
kcol <- keycols[i] |
| 291 | 103x |
kcolvec <- listing[[kcol]] |
| 292 | 103x |
kcolvec <- vapply(kcolvec, format_value, "", format = obj_format(kcolvec), na_str = obj_na_str(kcolvec)) |
| 293 | 103x |
curkey <- paste0(curkey, kcolvec) |
| 294 | 103x |
disp <- c(TRUE, tail(curkey, -1) != head(curkey, -1)) |
| 295 | 103x |
bodymat[disp, kcol] <- kcolvec[disp] |
| 296 |
} |
|
| 297 | ||
| 298 | 76x |
nonkeycols <- setdiff(names(listing), keycols) |
| 299 | 76x |
if (length(nonkeycols) > 0) {
|
| 300 | 75x |
for (nonk in nonkeycols) {
|
| 301 | 229x |
vec <- listing[[nonk]] |
| 302 | 229x |
vec <- vapply(vec, format_value, "", format = obj_format(vec), na_str = obj_na_str(vec)) |
| 303 | 229x |
bodymat[, nonk] <- vec |
| 304 |
} |
|
| 305 |
} |
|
| 306 | ||
| 307 | ||
| 308 | 76x |
fullmat <- rbind( |
| 309 | 76x |
var_labels(listing, fill = TRUE), |
| 310 | 76x |
bodymat |
| 311 |
) |
|
| 312 | ||
| 313 | 76x |
colaligns <- rbind( |
| 314 | 76x |
rep("center", length(cols)),
|
| 315 | 76x |
matrix(sapply(listing, obj_align), |
| 316 | 76x |
ncol = length(cols), |
| 317 | 76x |
nrow = nrow(fullmat) - 1, |
| 318 | 76x |
byrow = TRUE |
| 319 |
) |
|
| 320 |
) |
|
| 321 | 76x |
MatrixPrintForm( |
| 322 | 76x |
strings = fullmat, |
| 323 | 76x |
spans = matrix(1, |
| 324 | 76x |
nrow = nrow(fullmat), |
| 325 | 76x |
ncol = ncol(fullmat) |
| 326 |
), |
|
| 327 | 76x |
ref_fnotes = list(), |
| 328 | 76x |
aligns = colaligns, |
| 329 | 76x |
formats = matrix(1, |
| 330 | 76x |
nrow = nrow(fullmat), |
| 331 | 76x |
ncol = ncol(fullmat) |
| 332 |
), |
|
| 333 | 76x |
row_info = make_row_df(obj), |
| 334 | 76x |
nlines_header = 1, ## XXX this is probably wrong!!! |
| 335 | 76x |
nrow_header = 1, |
| 336 | 76x |
has_topleft = FALSE, |
| 337 | 76x |
has_rowlabs = FALSE, |
| 338 | 76x |
expand_newlines = TRUE, |
| 339 | 76x |
main_title = main_title(obj), |
| 340 | 76x |
subtitles = subtitles(obj), |
| 341 | 76x |
page_titles = page_titles(obj), |
| 342 | 76x |
main_footer = main_footer(obj), |
| 343 | 76x |
prov_footer = prov_footer(obj) |
| 344 |
) |
|
| 345 |
} |
|
| 346 |
) |
|
| 347 | ||
| 348 | ||
| 349 |
#' @export |
|
| 350 |
#' @rdname listings |
|
| 351 | 135x |
listing_dispcols <- function(df) attr(df, "listing_dispcols") %||% character() |
| 352 | ||
| 353 |
#' @export |
|
| 354 |
#' @param new character. Names of columns to be added to |
|
| 355 |
#' the set of display columns. |
|
| 356 |
#' @rdname listings |
|
| 357 |
add_listing_dispcol <- function(df, new) {
|
|
| 358 | 17x |
listing_dispcols(df) <- c(listing_dispcols(df), new) |
| 359 | 17x |
df |
| 360 |
} |
|
| 361 |
#' @export |
|
| 362 |
#' @param value character. New value. |
|
| 363 |
#' @rdname listings |
|
| 364 |
`listing_dispcols<-` <- function(df, value) {
|
|
| 365 | 127x |
if (!is.character(value)) {
|
| 366 | ! |
stop( |
| 367 | ! |
"dispcols must be a character vector of column names, got ", |
| 368 | ! |
"object of class: ", paste(class(value), collapse = ",") |
| 369 |
) |
|
| 370 |
} |
|
| 371 | 127x |
chk <- setdiff(value, names(df)) ## remember setdiff is not symmetrical |
| 372 | 127x |
if (length(chk) > 0) {
|
| 373 | ! |
stop( |
| 374 | ! |
"listing display columns must be columns in the underlying data. ", |
| 375 | ! |
"Column(s) ", paste(chk, collapse = ", "), " not present in the data." |
| 376 |
) |
|
| 377 |
} |
|
| 378 | 127x |
attr(df, "listing_dispcols") <- unique(value) |
| 379 | 127x |
df |
| 380 |
} |
|
| 381 | ||
| 382 | ||
| 383 | ||
| 384 |
#' @rdname listings |
|
| 385 |
#' |
|
| 386 |
#' @param name character(1). Name of the existing or new column to be |
|
| 387 |
#' displayed when the listing is rendered. |
|
| 388 |
#' @param fun function or NULL. A function which accepts \code{df} and
|
|
| 389 |
#' returns the vector for a new column, which is added to \code{df} as
|
|
| 390 |
#' \code{name}, or NULL if marking an existing column as
|
|
| 391 |
#' a listing column. |
|
| 392 |
#' @inheritParams formatters::fmt_config |
|
| 393 |
#' |
|
| 394 |
#' @return `df`, with `name` created (if necessary) and marked for |
|
| 395 |
#' display during rendering. |
|
| 396 |
#' |
|
| 397 |
#' @export |
|
| 398 |
add_listing_col <- function(df, |
|
| 399 |
name, |
|
| 400 |
fun = NULL, |
|
| 401 |
format = NULL, |
|
| 402 |
na_str = "NA", |
|
| 403 |
align = "left") {
|
|
| 404 | 17x |
if (!is.null(fun)) {
|
| 405 | ! |
vec <- fun(df) |
| 406 | 17x |
} else if (name %in% names(df)) {
|
| 407 | 17x |
vec <- df[[name]] |
| 408 |
} else {
|
|
| 409 | ! |
stop( |
| 410 | ! |
"Column '", name, "' not found. name argument must specify an existing column when ", |
| 411 | ! |
"no generating function (fun argument) is specified." |
| 412 |
) |
|
| 413 |
} |
|
| 414 | ||
| 415 | 17x |
if (!is.null(format)) {
|
| 416 | 7x |
vec <- df[[name]] |
| 417 | 7x |
obj_format(vec) <- format |
| 418 |
} |
|
| 419 | ||
| 420 | 17x |
obj_na_str(vec) <- na_str |
| 421 | 17x |
obj_align(vec) <- align |
| 422 | ||
| 423 |
## this works for both new and existing columns |
|
| 424 | 17x |
df[[name]] <- vec |
| 425 | 17x |
df <- add_listing_dispcol(df, name) |
| 426 | 17x |
df |
| 427 |
} |
| 1 |
## #' Print a listing to the terminal |
|
| 2 |
## #' @param x listing_df. the listing |
|
| 3 |
## #' @param ... ANY. unused |
|
| 4 |
## #' @return prints the listing object to the screen and silently returns the object |
|
| 5 |
## #' @export |
|
| 6 |
## setMethod("print", "listing_df",
|
|
| 7 |
## function(x, ...) {
|
|
| 8 |
## cat(toString(listing_matrix_form(x))) |
|
| 9 |
## invisible(x) |
|
| 10 |
## }) |
|
| 11 | ||
| 12 |
#' Methods for `listing_df` objects |
|
| 13 |
#' |
|
| 14 |
#' See core documentation in \code{formatters} for descriptions
|
|
| 15 |
#' of these functions. |
|
| 16 |
#' |
|
| 17 |
#' @export |
|
| 18 |
#' @inheritParams formatters::toString |
|
| 19 |
#' @param x listing_df. The listing. |
|
| 20 |
#' @param ... dots. See `toString` method in \code{formatters} for all parameters.
|
|
| 21 |
#' @method print listing_df |
|
| 22 |
#' @name listing_methods |
|
| 23 |
print.listing_df <- function(x, widths = NULL, tf_wrap = FALSE, max_width = NULL, ...) {
|
|
| 24 | 2x |
cat(toString(matrix_form(x), widths = widths, tf_wrap = tf_wrap, max_width = max_width, ...)) |
| 25 | 2x |
invisible(x) |
| 26 |
} |
|
| 27 | ||
| 28 |
#' @exportMethod toString |
|
| 29 |
#' @name listing_methods |
|
| 30 |
#' @aliases toString,listing_df-method |
|
| 31 |
setMethod("toString", "listing_df", function(x, ...) {
|
|
| 32 | 2x |
toString(matrix_form(x), ...) |
| 33 |
}) |
|
| 34 | ||
| 35 |
## because rle in base is too much of a stickler for being atomic |
|
| 36 |
basic_run_lens <- function(x) {
|
|
| 37 | 76x |
n <- length(x) |
| 38 | 76x |
if (n == 0) {
|
| 39 | ! |
return(integer()) |
| 40 |
} |
|
| 41 | ||
| 42 | 76x |
y <- x[-1L] != x[-n] |
| 43 | 76x |
i <- c(which(y), n) |
| 44 | 76x |
diff(c(0L, i)) |
| 45 |
} |
|
| 46 | ||
| 47 | ||
| 48 |
#' @rdname vec_nlines |
|
| 49 |
#' @param df listing_df. The listing. |
|
| 50 |
#' @param colnm Column name |
|
| 51 |
#' @param colvec Column values based on colnm |
|
| 52 |
format_colvector <- function(df, colnm, colvec = df[[colnm]]) {
|
|
| 53 | 334x |
if (missing(colvec) && !(colnm %in% names(df))) {
|
| 54 | ! |
stop("column ", colnm, " not found")
|
| 55 |
} |
|
| 56 | 334x |
na_str <- obj_na_str(colvec) |
| 57 | 334x |
if (is.null(na_str) || all(is.na(na_str))) {
|
| 58 | ! |
na_str <- rep("-", max(1L, length(na_str)))
|
| 59 |
} |
|
| 60 | ||
| 61 | 334x |
strvec <- vapply(colvec, format_value, "", format = obj_format(colvec), na_str = na_str) |
| 62 | 334x |
strvec |
| 63 |
} |
|
| 64 | ||
| 65 |
#' Utilities for formatting a listing column |
|
| 66 |
#' |
|
| 67 |
#' For `vec_nlines`, calculate the number of lines each element of a column vector will |
|
| 68 |
#' take to render. For `format_colvector`, |
|
| 69 |
#' |
|
| 70 |
#' @param vec any vector. A column vector to be rendered into ASCII. |
|
| 71 |
#' @param max_width numeric (or NULL). The width the column will be |
|
| 72 |
#' rendered in. |
|
| 73 |
#' @return a numeric vector of the number of lines elementwise that |
|
| 74 |
#' will be needed to render the elements of \code{vec} to width
|
|
| 75 |
#' \code{max_width}.
|
|
| 76 |
#' @keywords internal |
|
| 77 | 334x |
setGeneric("vec_nlines", function(vec, max_width = NULL) standardGeneric("vec_nlines"))
|
| 78 | ||
| 79 |
#' @rdname vec_nlines |
|
| 80 |
#' @param vec A vector. |
|
| 81 |
#' @keywords internal |
|
| 82 |
setMethod("vec_nlines", "ANY", function(vec, max_width = NULL) {
|
|
| 83 | 334x |
strvec <- wrap_txt(format_colvector(colvec = vec), max_width = max_width, hard = TRUE) |
| 84 | 334x |
mtchs <- gregexpr("\n", strvec, fixed = TRUE)
|
| 85 | 334x |
1L + vapply(mtchs, function(vi) sum(vi > 0), 1L) |
| 86 |
}) |
|
| 87 | ||
| 88 |
## setMethod("vec_nlines", "character", function(vec, max_width = NULL) {
|
|
| 89 |
## strvec <- wrap_txt(format_colvector(colvec = vec), max_width = max_width, hard = TRUE) |
|
| 90 |
## mtchs <- gregexpr("\n", strvec, fixed = TRUE)
|
|
| 91 |
## 1L + vapply(mtchs, function(vi) sum(vi > 0), 1L) |
|
| 92 |
## }) |
|
| 93 | ||
| 94 |
## setMethod("vec_nlines", "factor", function(vec, max_width = NULL) {
|
|
| 95 |
## lvl_nlines <- vec_nlines(levels(vec), max_width = max_width) |
|
| 96 |
## ret <- lvl_nlines[vec] |
|
| 97 |
## ret[is.na(ret)] <- format_value(NA_character |
|
| 98 |
## }) |
|
| 99 | ||
| 100 |
#' Make pagination dataframe for a listing |
|
| 101 |
#' @export |
|
| 102 |
#' @inheritParams formatters::make_row_df |
|
| 103 |
#' @param tt listing_df. The listing to be rendered |
|
| 104 |
#' @param visible_only logical(1). Ignored, as listings |
|
| 105 |
#' do not have non-visible structural elements. |
|
| 106 |
#' |
|
| 107 |
#' @examples |
|
| 108 |
#' lsting <- as_listing(mtcars) |
|
| 109 |
#' mf <- matrix_form(lsting) |
|
| 110 |
#' |
|
| 111 |
#' @return a data.frame with pagination information. |
|
| 112 |
#' @seealso \code{\link[formatters]{make_row_df}}
|
|
| 113 |
setMethod( |
|
| 114 |
"make_row_df", "listing_df", |
|
| 115 |
function(tt, colwidths = NULL, visible_only = TRUE, |
|
| 116 |
rownum = 0, |
|
| 117 |
indent = 0L, |
|
| 118 |
path = character(), |
|
| 119 |
incontent = FALSE, |
|
| 120 |
repr_ext = 0L, |
|
| 121 |
repr_inds = integer(), |
|
| 122 |
sibpos = NA_integer_, |
|
| 123 |
nsibs = NA_integer_) {
|
|
| 124 |
## assume sortedness by keycols |
|
| 125 | 77x |
keycols <- get_keycols(tt) |
| 126 | 77x |
dispcols <- listing_dispcols(tt) |
| 127 | 77x |
abs_rownumber <- seq_along(tt[[1]]) |
| 128 | 77x |
if (length(keycols) >= 1) {
|
| 129 | 76x |
runlens <- basic_run_lens(tt[[tail(keycols, 1)]]) |
| 130 |
} else {
|
|
| 131 | 1x |
runlens <- rep(1, NROW(tt)) |
| 132 |
} |
|
| 133 | 77x |
sibpos <- unlist(lapply(runlens, seq_len)) |
| 134 | 77x |
nsibs <- rep(runlens, times = runlens) |
| 135 | 77x |
extents <- rep(1L, nrow(tt)) |
| 136 | 77x |
if (length(colwidths) > 0 && length(colwidths) != length(dispcols)) {
|
| 137 | ! |
stop( |
| 138 | ! |
"Non-null colwidths vector must be the same length as the number of display columns.\n", |
| 139 | ! |
"Got: ", length(colwidths), "(", length(dispcols), " disp cols)."
|
| 140 |
) |
|
| 141 |
} |
|
| 142 | 77x |
if (length(colwidths) > 0) {
|
| 143 | ! |
names(colwidths) <- dispcols |
| 144 |
} |
|
| 145 |
## extents is a row-wise vector of extents, for each col, we update |
|
| 146 |
## if that column has any rows wider than the previously recorded extent. |
|
| 147 | 77x |
for (col in dispcols) {
|
| 148 |
## duplicated from matrix_form method, refactor! |
|
| 149 | 334x |
col_ext <- vec_nlines(tt[[col]], max_width = colwidths[col]) |
| 150 | 334x |
extents <- ifelse(col_ext > extents, col_ext, extents) |
| 151 |
} |
|
| 152 | 77x |
ret <- data.frame( |
| 153 | 77x |
label = "", name = "", |
| 154 | 77x |
abs_rownumber = abs_rownumber, |
| 155 | 77x |
path = I(as.list(rep(NA_character_, NROW(tt)))), |
| 156 | 77x |
pos_in_siblings = sibpos, |
| 157 | 77x |
n_siblings = nsibs, |
| 158 | 77x |
self_extent = extents, |
| 159 | 77x |
par_extent = 0L, |
| 160 | 77x |
reprint_inds = I(replicate(NROW(tt), list(integer()))), |
| 161 | 77x |
node_class = "listing_df", |
| 162 | 77x |
indent = 0L, |
| 163 | 77x |
nrowrefs = 0L, ## XXX this doesn't support footnotes |
| 164 | 77x |
ncellrefs = 0L, ## XXX this doesn't support footnotes |
| 165 | 77x |
nreflines = 0L, ## XXX this doesn't support footnotes |
| 166 | 77x |
force_page = FALSE, |
| 167 | 77x |
page_title = NA_character_, |
| 168 | 77x |
trailing_sep = NA_character_ |
| 169 |
) |
|
| 170 | 77x |
stopifnot(identical( |
| 171 | 77x |
names(ret), |
| 172 | 77x |
names(pagdfrow( |
| 173 | 77x |
nm = "", lab = "", rnum = 1L, pth = NA_character_, extent = 1L, |
| 174 | 77x |
rclass = "" |
| 175 |
)) |
|
| 176 |
)) |
|
| 177 | 77x |
ret |
| 178 |
} |
|
| 179 |
) |
|
| 180 | ||
| 181 |
## tt$sibpos <- unlist(lapply( |
|
| 182 |
## ## don't support pathing for now |
|
| 183 |
## tt$path <- I(lapply(1:NROW(tt), |
|
| 184 |
## function(i) {
|
|
| 185 |
## retpath <- character(2*length(keycols)) |
|
| 186 |
## for(j in seq_along(keycols)) {
|
|
| 187 |
## retpath[2*j - 1] <- keycols[j] |
|
| 188 |
## retpath[2*j] <- tt[i, keycols[j], drop = TRUE] |
|
| 189 |
## } |
|
| 190 |
## retpath |
|
| 191 |
## })) |
|
| 192 |
## spl <- split(tt, tt[keycols]) |
|
| 193 |
## spl <- spl[vapply(spl, function(y) NROW(y) > 0, NA)] |
|
| 194 |
## dfs <- lapply(spl, function(df) {
|
|
| 195 |
## df <- df[order(df$abs_rownumber),] |
|
| 196 |
## ndf <- NROW(df) |
|
| 197 |
## lapply(1:ndf, function(i) {
|
|
| 198 |
## rw <- df[i,] |
|
| 199 |
## stopifnot(nrow(rw) == 1) |
|
| 200 |
## pagdfrow(nm = "", |
|
| 201 |
## lab = "", |
|
| 202 |
## rnum = rw$abs_rownumber, |
|
| 203 |
## pth = NA_character_, |
|
| 204 |
## sibpos = i, |
|
| 205 |
## nsibs = ndf, |
|
| 206 |
## extent = 1L, |
|
| 207 |
## rclass = "listing_df", |
|
| 208 |
## repind = integer()) |
|
| 209 |
## }) |
|
| 210 |
## }) |
|
| 211 |
## ret <- do.call(rbind, unlist(dfs, recursive = FALSE)) |
|
| 212 |
## ret <- ret[order(ret$abs_rownumber),] |
|
| 213 |
## ret |
|
| 214 |
## }) |
|
| 215 | ||
| 216 |
#' @export |
|
| 217 |
#' @param x listing_df. The listing. |
|
| 218 |
#' @inheritParams base::Extract |
|
| 219 |
#' @param i ANY. Passed to base `[` methods. |
|
| 220 |
#' @param j ANY. Passed to base `[` methods. |
|
| 221 |
#' @aliases [,listing_df-method |
|
| 222 |
#' @rdname listing_methods |
|
| 223 |
#' @keywords internal |
|
| 224 |
setMethod( |
|
| 225 |
"[", "listing_df", |
|
| 226 |
function(x, i, j, drop = FALSE) {
|
|
| 227 | ! |
xattr <- attributes(x) |
| 228 | ! |
xattr$names <- xattr$names[j] |
| 229 | ! |
res <- NextMethod() |
| 230 | ! |
if (!drop) {
|
| 231 | ! |
attributes(res) <- xattr |
| 232 |
} |
|
| 233 | ! |
res |
| 234 |
} |
|
| 235 |
) |
|
| 236 | ||
| 237 |
#' @rdname listing_methods |
|
| 238 |
#' @param obj The object. |
|
| 239 |
#' @export |
|
| 240 |
#' @return for getter methods, the value of the aspect of |
|
| 241 |
#' \code{obj}; for setter methods, \code{obj} with
|
|
| 242 |
#' the relevant element of the listing updated. |
|
| 243 |
#' |
|
| 244 |
#' @examples |
|
| 245 |
#' |
|
| 246 |
#' lsting <- as_listing(mtcars) |
|
| 247 |
#' main_title(lsting) <- "Hi there" |
|
| 248 |
#' |
|
| 249 |
#' main_title(lsting) |
|
| 250 |
setMethod( |
|
| 251 |
"main_title", "listing_df", |
|
| 252 | 79x |
function(obj) attr(obj, "main_title") %||% character() |
| 253 |
) |
|
| 254 | ||
| 255 |
#' @rdname listing_methods |
|
| 256 |
#' @export |
|
| 257 |
setMethod( |
|
| 258 |
"subtitles", "listing_df", |
|
| 259 | 78x |
function(obj) attr(obj, "subtitles") %||% character() |
| 260 |
) |
|
| 261 |
#' @rdname listing_methods |
|
| 262 |
#' @export |
|
| 263 |
setMethod( |
|
| 264 |
"main_footer", "listing_df", |
|
| 265 | 78x |
function(obj) attr(obj, "main_footer") %||% character() |
| 266 |
) |
|
| 267 |
#' @rdname listing_methods |
|
| 268 |
#' @export |
|
| 269 |
setMethod( |
|
| 270 |
"prov_footer", "listing_df", |
|
| 271 | 78x |
function(obj) attr(obj, "prov_footer") %||% character() |
| 272 |
) |
|
| 273 | ||
| 274 |
.chk_value <- function(val, fname, len_one = FALSE, null_ok = TRUE) {
|
|
| 275 | 148x |
if (null_ok && is.null(val)) {
|
| 276 | 136x |
return(TRUE) |
| 277 |
} |
|
| 278 | 12x |
if (!is.character(val)) {
|
| 279 | 4x |
stop("value for ", fname, " must be a character, got ",
|
| 280 | 4x |
"object of class: ", paste(class(val), collapse = ","), |
| 281 | 4x |
call. = FALSE |
| 282 |
) |
|
| 283 |
} |
|
| 284 | 8x |
if (len_one && length(val) > 1) {
|
| 285 | 1x |
stop( |
| 286 | 1x |
"value for ", fname, " must be length <= 1, got ", |
| 287 | 1x |
"vector of length ", length(val) |
| 288 |
) |
|
| 289 |
} |
|
| 290 | 7x |
TRUE |
| 291 |
} |
|
| 292 | ||
| 293 |
#' @rdname listing_methods |
|
| 294 |
#' @export |
|
| 295 |
setMethod( |
|
| 296 |
"main_title<-", "listing_df", |
|
| 297 |
function(obj, value) {
|
|
| 298 |
## length 1 restriction is to match rtables behavior |
|
| 299 |
## which currently enforces this (though incompletely) |
|
| 300 | 39x |
.chk_value(value, "main_title", len_one = TRUE) |
| 301 | 37x |
attr(obj, "main_title") <- value |
| 302 | 37x |
obj |
| 303 |
} |
|
| 304 |
) |
|
| 305 | ||
| 306 |
#' @rdname listing_methods |
|
| 307 |
#' @export |
|
| 308 |
setMethod( |
|
| 309 |
"subtitles<-", "listing_df", |
|
| 310 |
function(obj, value) {
|
|
| 311 | 36x |
.chk_value(value, "subtitles") |
| 312 | 35x |
attr(obj, "subtitles") <- value |
| 313 | 35x |
obj |
| 314 |
} |
|
| 315 |
) |
|
| 316 | ||
| 317 |
#' @rdname listing_methods |
|
| 318 |
#' @export |
|
| 319 |
setMethod( |
|
| 320 |
"main_footer<-", "listing_df", |
|
| 321 |
function(obj, value) {
|
|
| 322 | 37x |
.chk_value(value, "main_footer") |
| 323 | 36x |
attr(obj, "main_footer") <- value |
| 324 | 36x |
obj |
| 325 |
} |
|
| 326 |
) |
|
| 327 | ||
| 328 |
#' @rdname listing_methods |
|
| 329 |
#' @export |
|
| 330 |
setMethod( |
|
| 331 |
"prov_footer<-", "listing_df", |
|
| 332 |
function(obj, value) {
|
|
| 333 | 36x |
.chk_value(value, "prov_footer") |
| 334 | 35x |
attr(obj, "prov_footer") <- value |
| 335 | 35x |
obj |
| 336 |
} |
|
| 337 |
) |
|
| 338 | ||
| 339 |
#' @rdname listing_methods |
|
| 340 |
#' @export |
|
| 341 |
setMethod( |
|
| 342 |
"num_rep_cols", "listing_df", |
|
| 343 |
function(obj) {
|
|
| 344 | 3x |
length(get_keycols(obj)) |
| 345 |
} |
|
| 346 |
) |
| 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 or horizontal if there are many columns. |
|
| 7 |
#' |
|
| 8 |
#' @param lsting listing_df. The listing to paginate. |
|
| 9 |
#' @param lpp numeric(1) or NULL. Number of row lines (not counting titles and |
|
| 10 |
#' footers) to have per page. Standard is `70` while `NULL` disables vertical |
|
| 11 |
#' pagination. |
|
| 12 |
#' @param cpp numeric(1) or NULL. Width (in characters) of the pages for |
|
| 13 |
#' horizontal pagination. `NULL` (the default) indicates no horizontal |
|
| 14 |
#' pagination should be done. |
|
| 15 |
#' @inheritParams formatters::pag_indices_inner |
|
| 16 |
#' @inheritParams formatters::vert_pag_indices |
|
| 17 |
#' @inheritParams formatters::page_lcpp |
|
| 18 |
#' @inheritParams formatters::toString |
|
| 19 |
#' |
|
| 20 |
#' @returns A list of listings' objects that are meant to be on separated pages. |
|
| 21 |
#' For `pag_tt_indices` a list of paginated-groups of row-indices of `lsting`. |
|
| 22 |
#' |
|
| 23 |
#' @rdname paginate |
|
| 24 |
#' |
|
| 25 |
#' @examples |
|
| 26 |
#' dat <- ex_adae |
|
| 27 |
#' lsting <- as_listing(dat[1:25, ], disp_cols = c("USUBJID", "AESOC", "RACE", "AETOXGR", "BMRKR1"))
|
|
| 28 |
#' |
|
| 29 |
#' mat <- matrix_form(lsting) |
|
| 30 |
#' |
|
| 31 |
#' cat(toString(mat)) |
|
| 32 |
#' |
|
| 33 |
#' paginate_listing(lsting, lpp = 10) |
|
| 34 |
#' |
|
| 35 |
#' paginate_listing(lsting, cpp = 100, lpp = 40) |
|
| 36 |
#' |
|
| 37 |
#' paginate_listing(lsting, cpp = 80, lpp = 40, verbose = TRUE) |
|
| 38 |
#' @export |
|
| 39 |
#' |
|
| 40 |
#' @return for `paginate_listing` a list containing separate |
|
| 41 |
#' `listing_df` objects for each page, for `pag_listing_indices`, |
|
| 42 |
#' a list of indices in the direction being paginated corresponding |
|
| 43 |
#' to the individual pages in that dimension. |
|
| 44 |
paginate_listing <- function(lsting, |
|
| 45 |
page_type = "letter", |
|
| 46 |
font_family = "Courier", |
|
| 47 |
font_size = 8, |
|
| 48 |
lineheight = 1, |
|
| 49 |
landscape = FALSE, |
|
| 50 |
pg_width = NULL, |
|
| 51 |
pg_height = NULL, |
|
| 52 |
margins = c(top = .5, bottom = .5, left = .75, right = .75), |
|
| 53 |
lpp = NA_integer_, |
|
| 54 |
cpp = NA_integer_, |
|
| 55 |
colwidths = propose_column_widths(lsting), |
|
| 56 |
tf_wrap = !is.null(max_width), |
|
| 57 |
max_width = NULL, |
|
| 58 |
verbose = FALSE) {
|
|
| 59 | 20x |
checkmate::assert_class(lsting, "listing_df") |
| 60 | 20x |
checkmate::assert_numeric(colwidths, lower = 0, len = length(listing_dispcols(lsting)), null.ok = TRUE) |
| 61 | 19x |
checkmate::assert_flag(tf_wrap) |
| 62 | 19x |
checkmate::assert_count(max_width, null.ok = TRUE) |
| 63 | 19x |
checkmate::assert_flag(verbose) |
| 64 | ||
| 65 | ||
| 66 | ||
| 67 | ||
| 68 | 19x |
indx <- paginate_indices(lsting, |
| 69 | 19x |
page_type = page_type, |
| 70 | 19x |
font_family = font_family, |
| 71 | 19x |
font_size = font_size, |
| 72 | 19x |
lineheight = lineheight, |
| 73 | 19x |
landscape = landscape, |
| 74 | 19x |
pg_width = pg_width, |
| 75 | 19x |
pg_height = pg_height, |
| 76 | 19x |
margins = margins, |
| 77 | 19x |
lpp = lpp, |
| 78 | 19x |
cpp = cpp, |
| 79 | 19x |
colwidths = colwidths, |
| 80 | 19x |
tf_wrap = tf_wrap, |
| 81 | 19x |
max_width = max_width, |
| 82 | 19x |
rep_cols = length(get_keycols(lsting)), |
| 83 | 19x |
verbose = verbose) |
| 84 | ||
| 85 | ||
| 86 | 19x |
vert_pags <- lapply(indx$pag_row_indices, |
| 87 | 19x |
function(ii) lsting[ii, ]) |
| 88 | 19x |
dispnames <- listing_dispcols(lsting) |
| 89 | 19x |
full_pag <- lapply(vert_pags, |
| 90 | 19x |
function(onepag) {
|
| 91 | 27x |
if (!is.null(indx$pag_col_indices)) {
|
| 92 | 27x |
lapply(indx$pag_col_indices, |
| 93 | 27x |
function(jj) {
|
| 94 | 76x |
res <- onepag[, dispnames[jj], drop = FALSE] |
| 95 | 76x |
listing_dispcols(res) <- intersect(dispnames, names(res)) |
| 96 | 76x |
res |
| 97 |
}) |
|
| 98 |
} else {
|
|
| 99 | ! |
list(onepag) |
| 100 |
} |
|
| 101 |
}) |
|
| 102 | ||
| 103 | 19x |
ret <- unlist(full_pag, recursive = FALSE) |
| 104 | 19x |
ret |
| 105 |
} |
|
| 106 | ||
| 107 |
#' @title Defunct functions |
|
| 108 |
#' |
|
| 109 |
#' @description |
|
| 110 |
#' These functions are defunct and their symbols will be removed entirely |
|
| 111 |
#' in a future release. |
|
| 112 |
#' @rdname defunct |
|
| 113 |
#' @inheritParams paginate_listing |
|
| 114 |
#' @export |
|
| 115 |
pag_listing_indices <- function(lsting, |
|
| 116 |
lpp = 15, |
|
| 117 |
colwidths = NULL, |
|
| 118 |
max_width = NULL, |
|
| 119 |
verbose = FALSE) {
|
|
| 120 | 1x |
.Defunct("paginate_indices", package = "formatters")
|
| 121 |
} |