It seems like html output in huxtable
has some kind of automatic greedy column width setting which creates an output that is appealing to me. The rtf output makes all columns equal width, which I find less appealing. In the example below the comparison of myhux.html
and myhux.rtf
shows what I mean.
library(huxtable)
long <- rep("LONGLONGLONGLONG", 5)
short <- rep("SHORT", 5)
df <- data.frame(LNGH = long, SHRTH = short)
myhux <- as_hux(df)
quick_rtf(myhux, file = "myhux.rtf", open = FALSE)
quick_html(myhux, file = "myhux.html", open = FALSE)
I'm aware that I can tailor a custom solution via set_col_width()
.
library(magrittr)
myhux_custom <- myhux %>%
set_col_width(c(0.7, 0.3))
quick_rtf(myhux_custom,
file = "myhux_custom.rtf",
open = FALSE)
However, since I am looking to do this for many tables in an automatic fashion I need something scalable that works for rtfs out of the box pretty much how it works for htmls.
It is possible to implement a greedy setting manually by retrieving the length of the longest string for each column and setting relative widths accordingly.
library(huxtable)
library(purrr)
library(magrittr)
long <- rep("LONGLONGLONGLONG", 5)
short <- rep("SHORT", 5)
df <- data.frame(LNGH = long, SHRTH = short)
myhux <- as_hux(df)
max_lens <- as.vector(unlist(map(myhux, ~max(nchar(.x)))))
sum_lens <- sum(max_lens)
len_fracs <- max_lens / sum_lens
myhux_edit <- myhux %>%
set_col_width(len_fracs)
quick_rtf(myhux_edit, file = "myhux_edit.rtf", open = FALSE)
quick_html(myhux_edit, file = "myhux_edit.html", open = FALSE)