If I want to efficiently format rows of a gt table, is there a better method than what I have shown below.
Some rows are character, and so need no formatting, some are numbers where one decimal place is required, some are numbers where two decimal places is required, and some are percentages where two decimal places is required. Whatever is done, should ideally generalise to other possible formats.
I create a data frame that creates the specification for formatting, but each format requires a separate command in the pipe.
library(dplyr)
library(gt)
#create small dataset
gtcars_8 <-
gtcars %>%
dplyr::group_by(ctry_origin) %>%
dplyr::top_n(2) %>%
dplyr::ungroup() %>%
dplyr::filter(ctry_origin != "United Kingdom")
#transpose data
row_labels <- colnames(gtcars_8)
gtcars_8_t <- as.data.frame(t(as.matrix(gtcars_8)))
gtcars_8_t$row_labels <- row_labels
my_column_names <- colnames(gtcars_8_t)[1:8]
#format data
format_specs <- as.data.frame(row_labels[1:10])
format_specs$type <- c("c","c","n","c","c","n","n","n","n","p")
format_specs$decimals <- c( 0 , 0 , 0 , 0 , 0 , 1 , 2 , 2 , 1 , 2 )
format_specs
#make basic gt table
gtcars_8_t %>%
slice(1:10) %>%
gt()
#make gt table with formats hardcoded (desired output)
gtcars_8_t %>%
slice(1:10) %>%
gt() %>%
cols_move_to_start("row_labels") %>%
#format for rows where: type = n, and decimals = 1
fmt(columns = vars(my_column_names),
rows = which(format_specs$type == "n" & format_specs$decimals == 1 ),
fns = function(x) {
formatC(as.numeric(x), digits = 1, format = "f")
} ) %>%
#format for rows where: type = n, and decimals = 2
fmt(columns = vars(my_column_names),
rows = which(format_specs$type == "n" & format_specs$decimals == 2 ),
fns = function(x) {
formatC(as.numeric(x), digits = 2, format = "f")
} ) %>%
#format for rows where: type = p, and decimals = 2
fmt(columns = vars(my_column_names),
rows = which(format_specs$type == "p" & format_specs$decimals == 2 ),
fns = function(x) {
paste0(formatC(as.numeric(x), digits = 2, format = "f"),"%")
} )
While not quite the same, applying formatting in gt appears to be a bit trickier than one might first expect ( eg).