From b86f2dff980140475f8b8ab397f65231419fd734 Mon Sep 17 00:00:00 2001 From: Nan Xiao Date: Wed, 21 Feb 2024 19:39:21 -0500 Subject: [PATCH 1/2] Customize data frame and tibble printing methods --- _common.R | 22 ++++++++++++++++++++++ tlf-ae-specific.qmd | 8 ++++---- tlf-ae-summary.qmd | 4 ++-- tlf-disposition.qmd | 4 +--- tlf-efficacy-ancova.qmd | 4 +--- tlf-overview.qmd | 21 ++++++++++++--------- tlf-population.qmd | 4 +--- 7 files changed, 43 insertions(+), 24 deletions(-) diff --git a/_common.R b/_common.R index 777d5dc..a778763 100644 --- a/_common.R +++ b/_common.R @@ -13,3 +13,25 @@ rtf2pdf <- function(input) { z <- paste(x, y, sep = " && ") if (Sys.getenv("GITHUB_ACTIONS") != "") system(z) else invisible(NULL) } + +# Customize data frame and tibble printing methods. +# See for details. +knit_print.data.frame <- function(x, ...) { + paste(capture.output(base::print.data.frame(head(x, 4))), collapse = "\n") +} + +registerS3method( + "knit_print", "data.frame", knit_print.data.frame, + envir = asNamespace("knitr") +) + +options(pillar.advice = FALSE) + +knit_print.tbl_df <- function(x, ...) { + paste(capture.output(print(x, n = 4)), collapse = "\n") +} + +registerS3method( + "knit_print", "tbl_df", knit_print.tbl_df, + envir = asNamespace("knitr") +) diff --git a/tlf-ae-specific.qmd b/tlf-ae-specific.qmd index 561f451..eb06625 100644 --- a/tlf-ae-specific.qmd +++ b/tlf-ae-specific.qmd @@ -70,7 +70,7 @@ t1 <- ana %>% summarise(n = fmt_num(n_distinct(USUBJID), digits = 0)) %>% mutate(AEDECOD = AESOC, order = 0) -t1 %>% head(4) +t1 ``` In the code below, we count the number of subjects in each AE term by @@ -83,7 +83,7 @@ t2 <- ana %>% summarise(n = fmt_num(n_distinct(USUBJID), digits = 0)) %>% mutate(order = 1) -t2 %>% head(4) +t2 ``` We prepare reporting data for AE information using code below: @@ -100,7 +100,7 @@ t_ae <- bind_rows(t1, t2) %>% arrange(AESOC, order, AEDECOD) %>% select(AESOC, AEDECOD, starts_with("n")) -t_ae %>% head(4) +t_ae ``` We prepare reporting data for analysis population using code below: @@ -161,7 +161,7 @@ tbl_ae_spec <- bind_rows( AEDECOD, paste0(" ", AEDECOD) )) -tbl_ae_spec %>% head(4) +tbl_ae_spec ``` We define the format of the output as below: diff --git a/tlf-ae-summary.qmd b/tlf-ae-summary.qmd index 534688e..7edb25e 100644 --- a/tlf-ae-summary.qmd +++ b/tlf-ae-summary.qmd @@ -58,7 +58,7 @@ tidy_ae <- adae %>% select(USUBJID, TRTAN, all, drug, ser, drug_ser, die) %>% pivot_longer(cols = c(all, drug, ser, drug_ser, die)) -tidy_ae %>% head(4) +tidy_ae ``` We summarize the number and percentage of participants who meet each AE criteria. @@ -86,7 +86,7 @@ ana <- tidy_ae %>% pct = paste0("(", pct, ")") ) -ana %>% head(4) +ana ``` We prepare reporting-ready dataset for each AE group. diff --git a/tlf-disposition.qmd b/tlf-disposition.qmd index d755ace..89647c4 100644 --- a/tlf-disposition.qmd +++ b/tlf-disposition.qmd @@ -43,9 +43,7 @@ The following variables are used in the preparation of a simplified disposition - DCREASCD: discontinued from study reason coded ```{r} -adsl %>% - select(USUBJID, TRT01P, TRT01PN, DISCONFL, DCREASCD) %>% - head(4) +adsl %>% select(USUBJID, TRT01P, TRT01PN, DISCONFL, DCREASCD) ``` In the code below, we calculate the number of participants in the analysis population by treatment arms. diff --git a/tlf-efficacy-ancova.qmd b/tlf-efficacy-ancova.qmd index 3a5b0f1..e67dde6 100644 --- a/tlf-efficacy-ancova.qmd +++ b/tlf-efficacy-ancova.qmd @@ -63,9 +63,7 @@ Below is the first few records of the analysis dataset. - CHG: change from baseline ```{r} -ana %>% - select(USUBJID, TRTPN, AVISIT, AVAL, BASE, CHG) %>% - head(4) +ana %>% select(USUBJID, TRTPN, AVISIT, AVAL, BASE, CHG) ``` ## Helper functions diff --git a/tlf-overview.qmd b/tlf-overview.qmd index 807a534..3b27768 100644 --- a/tlf-overview.qmd +++ b/tlf-overview.qmd @@ -179,9 +179,7 @@ of the dataset, which can be accessed by using the command `?r2rtf_adae` in R. ::: ```{r} -r2rtf_adae |> - select(USUBJID, TRTA, AEDECOD) |> - head(4) +r2rtf_adae |> select(USUBJID, TRTA, AEDECOD) ``` To manipulate the data and create a data frame containing the necessary @@ -193,7 +191,7 @@ tbl <- r2rtf_adae %>% count(TRTA, AEDECOD) %>% pivot_wider(names_from = TRTA, values_from = n, values_fill = 0) -tbl %>% head(4) +tbl ``` Having prepared the dataset `tbl`, we can now proceed with constructing the @@ -222,7 +220,8 @@ pipes to create an RTF table. - `write_rtf()` save RTF encoding into a file with file extension `.rtf`. ```{r} -head(tbl) |> +tbl |> + head() |> rtf_body() |> rtf_encode() |> write_rtf("tlf/intro-ae1.rtf") @@ -252,7 +251,8 @@ as they maintain the same ratio. ::: ```{r} -head(tbl) |> +tbl |> + head() |> rtf_body(col_rel_width = c(3, 2, 2, 2)) |> rtf_encode() |> write_rtf("tlf/intro-ae2.rtf") @@ -276,7 +276,8 @@ In the following example, we define the column header as representing the four columns in the table: ```{r} -head(tbl) |> +tbl |> + head() |> rtf_colheader( colheader = "Adverse Events | Placebo | Xanomeline High Dose | Xanomeline Low Dose", col_rel_width = c(3, 2, 2, 2) @@ -309,7 +310,8 @@ Below is an example to make the first column left-aligned and the rest columns center-aligned. ```{r} -head(tbl) |> +tbl |> + head() |> rtf_body(text_justification = c("l", "c", "c", "c")) |> rtf_encode() |> write_rtf("tlf/intro-ae5.rtf") @@ -336,7 +338,8 @@ headers with proper border lines. ::: ```{r} -head(tbl) %>% +tbl |> + head() |> rtf_colheader( colheader = " | Treatment", col_rel_width = c(3, 6) diff --git a/tlf-population.qmd b/tlf-population.qmd index f8f8dba..e79182d 100644 --- a/tlf-population.qmd +++ b/tlf-population.qmd @@ -40,9 +40,7 @@ population table using variables below: - SAFFL: safety population flag ```{r} -adsl %>% - select(USUBJID, ITTFL, EFFFL, SAFFL) %>% - head(4) +adsl %>% select(USUBJID, ITTFL, EFFFL, SAFFL) ``` ## Helper functions From defbe12d35e746af6dc7bd0c583b547f347cc005 Mon Sep 17 00:00:00 2001 From: Nan Xiao Date: Wed, 21 Feb 2024 20:09:34 -0500 Subject: [PATCH 2/2] Add "x more rows" footer for printing data frames --- _common.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/_common.R b/_common.R index a778763..da50884 100644 --- a/_common.R +++ b/_common.R @@ -16,8 +16,15 @@ rtf2pdf <- function(input) { # Customize data frame and tibble printing methods. # See for details. +more_rows <- function(x, n) if (nrow(x) <= n) NULL else paste0("# ℹ ", nrow(x) - n, " more rows") knit_print.data.frame <- function(x, ...) { - paste(capture.output(base::print.data.frame(head(x, 4))), collapse = "\n") + paste( + c( + capture.output(base::print.data.frame(head(x, 4))), + more_rows(x, 4) + ), + collapse = "\n" + ) } registerS3method(