-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
110 lines (74 loc) · 3.86 KB
/
README.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(tidyverse)
library(gtsummary)
```
# Coding Workshop: Tables in RMarkdown \n2021-11-18
## Pros and Cons of Table Packages
### {gt}
- Pro: Looks really good by default (best looking html in my opinion!)
- Pro: Footnotes fully featured
- Pro: Can use tidyselect to select data rows/cols. Generally adheres to tidyverse style guide
- Pro: Great documentation
- Con: Word not yet supported, PDF/RTF under development BUT gtsummary objects will automatically default to other print engines so you can knit to other outputs. Non-gtsummary objects will just not print in final document.
- Con: Can't have more than 2 spanning headers - although gt will support this soon
### {huxtable}
- Pro: Works well with html, pdf, rtf and word outputs
- Pro: You can save the latex code with the `as_latex()` directly- helpful when you have to write docs with latex
- Con: I find the documentation confusing
- Con: Default settings are not as nice looking as flextable and gt (although once you add compact theme they look better)
- Con: For gtsummary tables, you may lose the footnote numbering. Footnote still appears but without numbers
- Con: Hux table doesn’t let you see it in the viewer like gt. Prints as markdown, making it harder to preview
- Con: Footnotes have limited support
- Con: While gtsummary does a lot of the leg work customizing tables for gtsummary objects, non-gtsummary tables can be cumbesome to try to customize
### {flextable}
- Pro: Footnotes fully featured
- Pro: Works well with html and pdf. Best word output in my opinion
- Pro: Great documentation
- Con: Can't use tidyselect to index rows/cols
### {kable} + {kableExtra}
- Formatting options more limited, but this can be useful for dashboards (it sizes tables better than gt and offers some limited reactivate like scroll over)
# Summary:
- gt is best for HTML, and is currently under construction for PDF and RTF (aka Word). gt is already pretty great for PDF and RTF, but not all formatting is available for the outputs. They will release it soon however
- flextable works well for Word and PowerPoint. While PDF is technically supported, flextable just makes an image and puts it into a PDF document.
- huxtable is best for the LaTeX output (which includes PDF). Has a lot of output options but doesn't look as good right out of the box.
# Resources
- gt documention: https://gt.rstudio.com/
- flextable documentation: https://ardata-fr.github.io/flextable-book/
- huxtable documentation: https://hughjonesd.github.io/huxtable/reference/index.html
- gtsummary + RMarkdown: https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html
- flextable complex table: https://stackoverflow.com/questions/70059022/reproduce-a-complex-table-with-double-headesrs/70072503#70072503
- `bstfun::style_tbl_compact()`
## Some Formatting Functions for Word/PDF Outputs
- huxtable: `width(table)` <- 0.8
- flextable: set table properties to autofit! This helps automatically pick a width
```{r, eval = FALSE}
trial %>%
tbl_summary() %>%
as_flex_table() %>%
flextable::set_table_properties(layout = "autofit")
```
## Other tips
- Load biostatR instead of gtsummary- then you won't have trouble with renv
- Code to manipulate gtsummary tables and omit estimates above/below a certain threshold (e.g. unstable OR estimates):
```{r, eval = FALSE}
x <- trial %>%
gtsummary::tbl_uvregression(y = age,
method = lm)
x
```
Adjust `x$table_body` internals:
```{r, eval = FALSE}
x$table_body <- x$table_body %>%
mutate(estimate = dplyr::case_when(
estimate < 0 ~ NA_real_,
TRUE ~ estimate))
x
```