Tutorial: inline_text"

knitr::opts_chunk$set(
  collapse = TRUE,
  warning = FALSE,
  comment = "#>"
)
gt_compact_fun <- function(x) {
  gt::tab_options(x, 
                  table.font.size = 'small',
                  data_row.padding = gt::px(1),
                  summary_row.padding = gt::px(1),
                  grand_summary_row.padding = gt::px(1),
                  footnotes.padding = gt::px(1),
                  source_notes.padding = gt::px(1),
                  row_group.padding = gt::px(1))
}
# we do NOT want the vignette to build on CRAN...it's taking too long
if (!identical(Sys.getenv("IN_PKGDOWN"), "true") && 
    !tolower(as.list(Sys.info())$user) %in% c("sjobergd", "currym", "whitingk", "whiting")) {
  msg <- 
    paste("View this vignette on the",
          "[package website](https://www.danieldsjoberg.com/gtsummary/articles/inline_text.html).")
  cat(msg)
  knitr::knit_exit()
}

Introduction

Reproducible reports are an important part of good practices. We often need to report the results from a table in the text of an R markdown report. Inline reporting has been made simple with inline_text(). The inline_text() function reports statistics from {gtsummary} tables inline in an R markdown report.

Setup

Before going through the tutorial, install and load {gtsummary}.

# install.packages("gtsummary")
library(gtsummary)

Example data set

We'll be using the trial data set throughout this example.

For brevity in the tutorial, let's keep a subset of the variables from the trial data set.

trial2 <-
  trial %>%
  select(trt, marker, stage)

Inline results from tbl_summary() {#inline_text_tbl_summary}

First create a basic summary table using tbl_summary() (review tbl_summary() vignette for detailed overview of this function if needed).

tab1 <- tbl_summary(trial2, by = trt)
tab1

To report the median (IQR) of the marker levels in each group, use the following commands inline.

The median (IQR) marker level in the Drug A and Drug B groups are `r inline_text(tab1, variable = marker, column = "Drug A")` and `r inline_text(tab1, variable = marker, column = "Drug B")`, respectively.

Here's how the line will appear in your report.

The median (IQR) marker level in the Drug A and Drug B groups are r inline_text(tab1, variable = marker, column = "Drug A") and r inline_text(tab1, variable = marker, column = "Drug B"), respectively.

If you display a statistic from a categorical variable, include the level argument.

`r inline_text(tab1, variable = stage, level = "T1", column = "Drug B")` resolves to "r inline_text(tab1, variable = stage, level = "T1", column = "Drug B")"

Inline results from tbl_regression() {#inline_text_tbl_regression}

Similar syntax is used to report results from tbl_regression() and tbl_uvregression() tables. Refer to the tbl_regression() vignette if you need detailed guidance on using these functions.

Let's first create a regression model.

# build logistic regression model
m1 <- glm(response ~ age + stage, trial, family = binomial(link = "logit"))

Now summarize the results with tbl_regression(); exponentiate to get the odds ratios.

tbl_m1 <- tbl_regression(m1, exponentiate = TRUE)
tbl_m1

To report the result for age, use the following commands inline.

`r inline_text(tbl_m1, variable = age)`

Here's how the line will appear in your report.

r inline_text(tbl_m1, variable = age)

It is reasonable that you'll need to modify the text. To do this, use the pattern argument. The pattern argument syntax follows glue::glue() format with referenced R objects being inserted between curly brackets. The default is pattern = "{estimate} ({conf.level*100}% CI {conf.low}, {conf.high}; {p.value})". You have access the to following fields within the pattern argument.

tibble::tribble(
  ~Parameter,       ~Description,
  "`{estimate}`",   "primary estimate (e.g. model coefficient, odds ratio)",
  "`{conf.low}`",   "lower limit of confidence interval",
  "`{conf.high}`",  "upper limit of confidence interval",
  "`{p.value}`",    "p-value",
  "`{conf.level}`", "confidence level of interval",
  "`{N}`",          "number of observations"  
) %>%
  gt::gt() %>%
  gt::fmt_markdown(columns = c(Parameter)) %>%
  gt_compact_fun()

Age was not significantly associated with tumor response `r inline_text(tbl_m1, variable = age, pattern = "(OR {estimate}; 95% CI {conf.low}, {conf.high}; {p.value})")`.

Age was not significantly associated with tumor response r inline_text(tbl_m1, variable = age, pattern = "(OR {estimate}; 95% CI {conf.low}, {conf.high}; {p.value})").

If you're printing results from a categorical variable, include the level argument, e.g. inline_text(tbl_m1, variable = stage, level = "T3") resolves to "r inline_text(tbl_m1, variable = stage, level = "T3")".

The inline_text function has arguments for rounding the p-value (pvalue_fun) and the coefficients and confidence interval (estimate_fun). These default to the same rounding performed in the table, but can be modified when reporting inline.

For more details about inline code, review to the RStudio documentation page.



Try the gtsummary package in your browser

Any scripts or data that you put into this service are public.

gtsummary documentation built on June 22, 2022, 9:07 a.m.