Yes, this is essentially what I did. I used pivot_wider and some clever renaming of columns to make it work with gt.
t = all_specs %>%
select(-term, -statistic, -p.value) %>%
rename(
Beta = "estimate",
SE = "std.error"
) %>%
pivot_wider(values_from = c("Beta", "SE"), names_from = "fe")
reverse_names = function(x, delim){
out = c()
for(xi in x){
parts = str_split(xi, delim)[[1]]
newname = paste(rev(parts), collapse = delim)
out = c(out, newname)
}
return(out)
}
names(t) = reverse_names(names(t), "_")
t = t[,order(names(t))]
g = t %>% gt(
rowname_col = "type",
groupname_col = "outcome",
) %>%
tab_header(title = "Outcome: Ratio by post-subway") %>%
fmt_number(
columns = matches("Beta"),
decimals = 4
) %>%
fmt_number(
columns = matches("SE"),
decimals = 4
) %>%
tab_spanner_delim(delim = "_", gather = TRUE)
Note that the pivot_wider call has the names in an order that I dont want, since gt's tab_spanner_delim has the first term in the separated name be the upper column spanner. This was a bit annoying.
Second, I don't think the gather option is working properly, notice that I have to sort the names in the data frame t before calling gt. I filed an issue about this here.
Overally I liked using gt but the way you assign column spanners based off of the names seems "hacky" to me. I would love if it could use grammar-of-graphics conventions for columns in addition to rows.
Second, have some quibbles with the latex output. It would be great if it were a short table by default and had the option of being a fragment, i.e. starting at \begin{tabular} instead of \begin{table} (well, \begin{longtable}). It's nice to be able to write your custom caption and label in the latex document and \input the table itself inside of your \begin{table} environment.
Let me know if I should file issues for these feature requests or if they are already being worked on.