# Tutorial: inline_text

#### Last Updated: September 13, 2020

## 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.

- This set contains data from 200 patients who received one of two types of chemotherapy (Drug A or Drug B). The outcomes are tumor response and death.

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()

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

Characteristic | Drug A, N = 981 | Drug B, N = 1021
--- | --- | ---
Marker Level (ng/mL) | 0.84 (0.24, 1.57) | 0.52 (0.19, 1.20)
Unknown | 6 | 4
T Stage |  |
T1 | 28 (29%) | 25 (25%)
T2 | 25 (26%) | 29 (28%)
T3 | 22 (22%) | 21 (21%)
T4 | 23 (23%) | 27 (26%)
1 Median (IQR); n (%)

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 0.84 (0.24, 1.57) and 0.52 (0.19, 1.20), 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 “25 (25%)”

## Inline results from 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

Characteristic | OR1 | 95% CI1 | p-value
--- | --- | --- | ---
Age | 1.02 | 1.00, 1.04 | 0.091
T Stage |  |  |
T1 | — | — |
T2 | 0.58 | 0.24, 1.37 | 0.2
T3 | 0.94 | 0.39, 2.28 | 0.9
T4 | 0.79 | 0.33, 1.90 | 0.6
1 OR = Odds Ratio, CI = Confidence Interval

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.

> 1.02 (95% CI 1.00, 1.04; p=0.091)

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.

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

> 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 (OR 1.02; 95% CI 1.00, 1.04; p=0.091).

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 “0.94 (95% CI 0.39, 2.28; p=0.9)”.

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.