Skip to content

Commit

Permalink
Merge pull request #124 from elong0527/tbl-print
Browse files Browse the repository at this point in the history
Customize data frame and tibble printing methods
  • Loading branch information
elong0527 authored Feb 23, 2024
2 parents 0690acc + defbe12 commit 9369d2b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 24 deletions.
29 changes: 29 additions & 0 deletions _common.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,32 @@ 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 <https://github.com/elong0527/r4csr/issues/115> 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(
c(
capture.output(base::print.data.frame(head(x, 4))),
more_rows(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")
)
8 changes: 4 additions & 4 deletions tlf-ae-specific.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tlf-ae-summary.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -86,7 +86,7 @@ ana <- tidy_ae %>%
pct = paste0("(", pct, ")")
)
ana %>% head(4)
ana
```

We prepare reporting-ready dataset for each AE group.
Expand Down
4 changes: 1 addition & 3 deletions tlf-disposition.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions tlf-efficacy-ancova.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions tlf-overview.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -336,7 +338,8 @@ headers with proper border lines.
:::

```{r}
head(tbl) %>%
tbl |>
head() |>
rtf_colheader(
colheader = " | Treatment",
col_rel_width = c(3, 6)
Expand Down
4 changes: 1 addition & 3 deletions tlf-population.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9369d2b

Please sign in to comment.