It tabulates a data.frame representing an aggregation
which is then transformed as a flextable with
as_flextable. The function
allows to define any display with the syntax of flextable in
a table whose layout is showing dimensions of the aggregation
across rows and columns.
Arguments
- x
an aggregated data.frame
- rows
column names to use in rows dimensions
- columns
column names to use in columns dimensions
- datasup_first
additional data that will be merged with table and placed after the columns presenting the row dimensions.
- datasup_last
additional data that will be merged with table and placed at the end of the table.
- hidden_data
additional data that will be merged with table, the columns are not presented but can be used with
compose()
ormk_par()
function.- row_compose
a list of call to
as_paragraph()
- these calls will be applied to the row dimensions (the name is used to target the displayed column).- ...
named arguments calling function
as_paragraph()
. The names are used as labels and the values are evaluated when the flextable is created.- object
an object returned by function
tabulator()
.
Methods (by generic)
summary(tabulator)
: callsummary()
to get a data.frame describing mappings between variables and their names in the flextable. This data.frame contains a column namedcol_keys
where are stored the names that can be used for further selections.
Examples
set_flextable_defaults(digits = 2, border.color = "gray")
library(data.table)
# example 1 ----
if (require("stats")) {
dat <- aggregate(breaks ~ wool + tension,
data = warpbreaks, mean
)
cft_1 <- tabulator(
x = dat, rows = "wool",
columns = "tension",
`mean` = as_paragraph(as_chunk(breaks)),
`(N)` = as_paragraph(as_chunk(length(breaks), formatter = fmt_int))
)
ft_1 <- as_flextable(cft_1)
ft_1
}
wool
L
M
H
mean
(N)
mean
(N)
mean
(N)
A
44.56
2
24.00
2
24.56
2
B
28.22
2
28.78
2
18.78
2
# example 2 ----
if (require("ggplot2")) {
multi_fun <- function(x) {
list(mean = mean(x), sd = sd(x))
}
dat <- as.data.table(ggplot2::diamonds)
dat <- dat[cut %in% c("Fair", "Good", "Very Good")]
dat <- dat[, unlist(lapply(.SD, multi_fun),
recursive = FALSE
),
.SDcols = c("z", "y"),
by = c("cut", "color")
]
tab_2 <- tabulator(
x = dat, rows = "color",
columns = "cut",
`z stats` = as_paragraph(as_chunk(fmt_avg_dev(z.mean, z.sd, digit2 = 2))),
`y stats` = as_paragraph(as_chunk(fmt_avg_dev(y.mean, y.sd, digit2 = 2)))
)
ft_2 <- as_flextable(tab_2)
ft_2 <- autofit(x = ft_2, add_w = .05)
ft_2
}
color
Fair
Good
Very Good
z stats
y stats
z stats
y stats
z stats
y stats
D
3.8 (0.55)
6.0 (0.81)
3.5 (0.56)
5.6 (0.92)
3.4 (0.60)
5.5 (0.97)
E
3.7 (0.57)
5.9 (0.83)
3.5 (0.59)
5.6 (0.95)
3.4 (0.85)
5.5 (1.00)
F
3.8 (0.60)
5.9 (0.88)
3.5 (0.60)
5.7 (0.95)
3.5 (0.63)
5.6 (1.01)
G
4.0 (0.66)
6.1 (0.99)
3.6 (0.65)
5.9 (1.01)
3.5 (0.65)
5.7 (1.04)
H
4.2 (0.62)
6.5 (0.94)
3.7 (0.69)
6.0 (1.13)
3.7 (0.72)
6.0 (1.16)
I
4.2 (0.61)
6.5 (0.88)
3.9 (0.73)
6.3 (1.21)
3.9 (0.72)
6.3 (1.18)
J
4.3 (0.74)
6.7 (1.09)
4.0 (0.69)
6.4 (1.12)
4.0 (0.70)
6.5 (1.15)
# example 3 ----
# data.table version
dat <- melt(as.data.table(iris),
id.vars = "Species",
variable.name = "name", value.name = "value"
)
dat <- dat[,
list(
avg = mean(value, na.rm = TRUE),
sd = sd(value, na.rm = TRUE)
),
by = c("Species", "name")
]
# dplyr version
# library(dplyr)
# dat <- iris %>%
# pivot_longer(cols = -c(Species)) %>%
# group_by(Species, name) %>%
# summarise(avg = mean(value, na.rm = TRUE),
# sd = sd(value, na.rm = TRUE),
# .groups = "drop")
tab_3 <- tabulator(
x = dat, rows = c("Species"),
columns = "name",
`mean (sd)` = as_paragraph(
as_chunk(avg),
" (", as_chunk(sd), ")"
)
)
ft_3 <- as_flextable(tab_3)
ft_3
Species
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
setosa
5.01 (0.35)
3.43 (0.38)
1.46 (0.17)
0.25 (0.11)
versicolor
5.94 (0.52)
2.77 (0.31)
4.26 (0.47)
1.33 (0.20)
virginica
6.59 (0.64)
2.97 (0.32)
5.55 (0.55)
2.03 (0.27)
init_flextable_defaults()