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:
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?
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")
Yes, the documentation can be found here : https://davidgohel.github.io/flextable/articles/format.html#set_formatter-function
and here : https://davidgohel.github.io/flextable/reference/set_formatter.html
set_formatter(b = function(x) format(round(x, -3), big.mark = ","),
c = function(x) format(round(x, -3), big.mark = ","),
d = function(x) format(round(x, -3), big.mark = ","),
e = function(x) round(x, 2),
f = function(x) round(x, 2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With