-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown_gt.Rmd
97 lines (81 loc) · 2.18 KB
/
markdown_gt.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
---
title: "gt Table"
author: "Matt Bixley"
date: "22/09/2020"
output:
html_document:
code_folding: show
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(palmerpenguins)
```
# Tables
There are several packages that help for making beautiful tables with Markdown.
[kableExtra](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html)
[Huxtable](https://cran.r-project.org/web/packages/huxtable/vignettes/huxtable.html)
[gt](https://themockup.blog/posts/2020-05-16-gt-a-grammer-of-tables/#gt-package---a-grammar-of-tables) - Grammar of Tables
## gt
[Exploring the gt (Grammar Of Tables) Package in R](https://towardsdatascience.com/exploring-the-gt-grammar-of-tables-package-in-r-7fff9d0b40cd)
```{r gt, echo = F}
if(!require("gt")) install.packages("gt")
library(gt)
```
Make a starter table
```{r table1}
t1 <- penguins %>%
tibble() %>%
filter(species == "Adelie") %>%
group_by(island, year) %>%
summarise_if(is.numeric, sum) %>%
mutate(island = case_when(
island == "Biscoe" ~ "Biscoe Island",
island == "Dream" ~ "Dream Island",
island == "Torgersen" ~ "Torgersen Island"
)) %>%
gt(rowname_col = "year")
t1
```
```{r colvars}
colvars <- c("bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g")
```
Add a summary row
```{r table2}
t2 <- t1 %>%
summary_rows(
groups = T,
columns = vars(colvars),
fns = list(AVERAGE = "mean"),
formatter = fmt_number,
use_seps = T,
decimals = 0
)
t2
```
Headers and Footers
```{r table3}
t3 <- t2 %>%
cols_label(
bill_length_mm = "Bill Length",
bill_depth_mm = "Bill Depth",
flipper_length_mm = "Flipper Length",
body_mass_g = "Body Mass"
) %>%
tab_header(title = "All About Adelie Penguins",
subtitle = "Yearly measurements of penguin things") %>%
cols_align(align = "right", columns = T) %>%
tab_footnote(
footnote = "In grams",
location = cells_column_labels(
columns = vars(body_mass_g)
)
) %>%
tab_footnote(
footnote = "In mm",
location = cells_column_labels(
columns = vars(bill_length_mm, bill_depth_mm, flipper_length_mm)
)
)
t3
```