I would like to create a workflow to create tables using {gt}, Unfortunately, I won't always know how many columns there will be or what their names will be. They will however, follow a consistent pattern as seen below. Obviously, the trailing pipe is an issue in this instance. Is there any easier way to go about this?
library(dplyr)
library(stringr)
library(tidyr)
library(glue)
library(gt)
x <- tibble(
'A: Expected' = c(1, 2, 6),
'A: Collected' = c(1, 1, 6),
'A: Percent' = c(100, 50, 100),
'B: Expected' = c(2, 4, 9),
'B: Collected' = c(1, 1, 9),
'B: Percent' = c(50, 25, 100)
)
spanners <- function(gt_data = x) {
y <- names(gt_data)
df <- y %>%
tibble() %>%
rename(label = 1) %>%
filter(str_detect(label, "Expected")) %>%
mutate(label = str_replace_all(label, ": Expected", "")) %>%
mutate(columns_out = glue("c('{label}: Collected', '{label}: Expected', '{label}: Percent')"))
add_spanners <- df %>%
mutate(spanner = glue("tab_spanner(
label = {label},
columns = {columns_out},
id = {label},
gather = TRUE
) %>% "))
return(add_spanners$spanner)
}
t <- x %>%
gt() %>%
spanners()
#> tab_spanner(
#> label = A,
#> columns = c('A: Collected', 'A: Expected', 'A: Percent'),
#> id = A,
#> gather = TRUE
#> ) %>%
#> tab_spanner(
#> label = B,
#> columns = c('B: Collected', 'B: Expected', 'B: Percent'),
#> id = B,
#> gather = TRUE
#> ) %>%
I'd like for it to look like this:
Created on 2021-08-30 by the reprex package (v2.0.1)</sup