I’m trying to build an rtable with the following requirements:
- Overall counts at the top
- Counts of rows by a group and subgroup
- Unindent groups/subgroups by one level
- Use section dividers (blank lines) in between each group
- Use a section divider after the overall counts line to visually separate it from the rest of the table
It’s the last requirement that I’m having trouble figuring out the correct solution for.
As an example, with the following data:
iris2 <- iris
iris2$group <- rep(letters[1:3], 50)
I want to create a table that looks like this:
all obs
—————————————————————————
Overall 150 (100.0%)
setosa 50 (33.3%)
a 17 (11.3%)
b 17 (11.3%)
c 16 (10.7%)
versicolor 50 (33.3%)
a 17 (11.3%)
b 16 (10.7%)
c 17 (11.3%)
virginica 50 (33.3%)
a 16 (10.7%)
b 17 (11.3%)
c 17 (11.3%)
The only way I have been able to achieve this is with the following but it seems like a clunky way to do it:
library(rtables)
cfn <- function(df, labelstr, .N_col) {
n <- nrow(df)
in_rows(
.names = c("Overall", " "),
.list = list(rcell(c(n, n / .N_col)), " "),
.formats = c("xx (xx.x%)", "xx")
)
}
basic_table() |>
summarize_row_groups(cfun = cfn) |>
split_rows_by("Species", section_div = "", indent_mod = -1) |>
summarize_row_groups() |>
split_rows_by("group") |>
summarize_row_groups() |>
build_table(iris2)
Aside from extra code that’s difficult to reason about what it’s doing, another reason this isn’t great is if I do something like trim_rows()
, the extra row I forced under “Overall” is removed while the other section divs aren’t.
Is there a more elegant way to create a section div under the first “Overall” row? (And as an aside, a better way to provide the “Overall” label for that row outside of using a tabulation function?)