The goal of tidytableR is to is to reshape data from a tidy format into one that is suitable for presentation. Specifically, it works best when used to make complex, publication-quality tables from output created by dplyr::summarise
or tidyr::gather
. It allows users to map column names from an input data frame to elements of an output table.
The package was developed from code meant to act as a wrapper script for the htmlTable
function in the htmlTable package. In fact, its first release was as the tidyHtmlTable
function within the htmlTable
package. Therefore, many of the variable names and symantics used within are related to those used in the htmlTable
package.
You can install tidytableR from github with:
# install.packages("devtools")
devtools::install_github("graggsd/tidytableR")
library(tidytableR)
library(magrittr)
library(dplyr)
library(tidyr)
library(tibble)
Turn mtcars
data into a tidy dataset.
td <- mtcars %>%
rownames_to_column %>%
select(rowname, cyl, gear, hp, mpg, qsec) %>%
gather(per_metric, value, hp, mpg, qsec)
Compute 4 summary statistics for each of the 3 performance metrics, grouped number of cylinders and gears.
tidy_summary <- td %>%
group_by(cyl, gear, per_metric) %>%
summarise(Mean = round(mean(value), 1),
SD = round(sd(value), 1),
Min = round(min(value), 1),
Max = round(max(value), 1)) %>%
gather(summary_stat, value, Mean, SD, Min, Max) %>%
ungroup %>%
mutate(gear = paste(gear, "Gears"),
cyl = paste(cyl, "Cylinders"))
tidy_summary %>%
tidy_htmlTable(header = "gear",
cgroup1 = "cyl",
cell_value = "value",
rnames = "summary_stat",
rgroup = "per_metric")
4 Cylinders
6 Cylinders
8 Cylinders
3 Gears
4 Gears
5 Gears
3 Gears
4 Gears
5 Gears
3 Gears
5 Gears
hp
Max
97
109
113
110
123
175
245
335
Mean
97
76
102
107.5
116.5
175
194.2
299.5
Min
97
52
91
105
110
175
150
264
SD
NaN
20.1
15.6
3.5
7.5
NaN
33.4
50.2
mpg
Max
21.5
33.9
30.4
21.4
21
19.7
19.2
15.8
Mean
21.5
26.9
28.2
19.8
19.8
19.7
15.1
15.4
Min
21.5
21.4
26
18.1
17.8
19.7
10.4
15
SD
NaN
4.8
3.1
2.3
1.6
NaN
2.8
0.6
qsec
Max
20
22.9
16.9
20.2
18.9
15.5
18
14.6
Mean
20
19.6
16.8
19.8
17.7
15.5
17.1
14.6
Min
20
18.5
16.7
19.4
16.5
15.5
15.4
14.5
SD
NaN
1.5
0.1
0.6
1.1
NaN
0.8
0.1
tidy_summary %>%
tidy_htmlTable(header = "summary_stat",
cgroup1 = "per_metric",
cell_value = "value",
rnames = "gear",
rgroup = "cyl")
hp
mpg
qsec
Max
Mean
Min
SD
Max
Mean
Min
SD
Max
Mean
Min
SD
4 Cylinders
3 Gears
97
97
97
NaN
21.5
21.5
21.5
NaN
20
20
20
NaN
4 Gears
109
76
52
20.1
33.9
26.9
21.4
4.8
22.9
19.6
18.5
1.5
5 Gears
113
102
91
15.6
30.4
28.2
26
3.1
16.9
16.8
16.7
0.1
6 Cylinders
3 Gears
110
107.5
105
3.5
21.4
19.8
18.1
2.3
20.2
19.8
19.4
0.6
4 Gears
123
116.5
110
7.5
21
19.8
17.8
1.6
18.9
17.7
16.5
1.1
5 Gears
175
175
175
NaN
19.7
19.7
19.7
NaN
15.5
15.5
15.5
NaN
8 Cylinders
3 Gears
245
194.2
150
33.4
19.2
15.1
10.4
2.8
18
17.1
15.4
0.8
5 Gears
335
299.5
264
50.2
15.8
15.4
15
0.6
14.6
14.6
14.5
0.1
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.