-
Notifications
You must be signed in to change notification settings - Fork 5
/
stats-html.Rmd
68 lines (52 loc) · 2.01 KB
/
stats-html.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
---
title: "gtsummary + R Markdown"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
```
```{r libraries, message=FALSE}
library(gtsummary)
library(tidyverse)
library(survival)
```
# NOTE
This RMarkdown is the built-in example from the `gtsummary` R package. You can get the same file by running the code below:
```{r, eval = FALSE}
library(gtsummary)
system.file(package = "gtsummary") %>%
file.path("rmarkdown_example/gtsummary_rmarkdown_html.Rmd") %>%
file.edit()
```
## gtsummary tables
Tables created with {gtsummary} can be integrated into R markdown documents.
The {gtsummary} package was written to be a companion to the [{gt} package](https://gt.rstudio.com/) from RStudio, and {gtsummary} tables are printed using {gt} when possible.
Currently, {gt} supports **HTML** output, with **LaTeX** and **RTF** planned for the future.
```{r example}
patient_characteristics <-
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt)
patient_characteristics
```
With HTML output, you can include complex tables with footnotes, indentation, and spanning table headers.
```{r}
# Side-by-side Regression Models
# logistic regression model
t1 <-
glm(response ~ trt + grade + age, trial, family = binomial) %>%
tbl_regression(exponentiate = TRUE)
# time to death Cox model
t2 <-
coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
tbl_regression(exponentiate = TRUE)
# printing merged table
tbl_merge(
tbls = list(t1, t2),
tab_spanner = c("**Tumor Response**", "**Time to Death**")
)
```
## inline reporting
Any number/statistic from a {gtsummary} table can be reported inline in a R markdown document using the `inline_text()` function. See example below:
> Among patients who received Drug A, `r inline_text(patient_characteristics, variable = grade, level = "III", column = "Drug A")` had grade III tumors.
For detailed examples using functions from {gtsummary}, visit the [{gtsummary} website](http://www.danieldsjoberg.com/gtsummary/).