Is it possible to format multiple columns with one line of script using flexitable?
The example includes variables of two different types of double. I'd like to:
- round variables greater than 1000 to the nearest 1000 and add thousand comma formating, and
- round variables less than one to two decimal places
This is relatively simple using flextable, but becomes a bore with a much larger data set. I cannot see a way to do this more efficiently.
I could use dplyr to pre-process the data (although the comma formatting requires changing the variable type from double to character). I'd prefer if possible to do this in flextable. I'm aware multi-column formatting is possible with Huxtable. Might it be possible for users to create bespoke set_formatter_type functions?
MWE
set.seed(100)
tib <- tibble(a = letters[1:4],
b = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
c = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
d = runif(4)*sample(c(10000, 1000000, 10000000), 4, replace = TRUE),
e = runif(4),
f = runif(4))
regulartable(tib) %>%
set_formatter(b = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(c = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(d = function(x) format(round(x, -3), big.mark = ",")) %>%
set_formatter(e = function(x) round(x, 2)) %>%
set_formatter(f = function(x) round(x, 2)) %>%
print(preview = "docx")