Is there anyway to export coloured dataframes in R?

Viewed 31

I have a tukey results table that i would like to export while preserving the colourings :

enter image description here

I've tried :

png("test.png", height = 50*nrow(stat.test), width = 200*ncol(stat.test))
grid.table(stat.test)
dev.off()

but the table is saved like so: enter image description here

1 Answers

Here's one option. This gives negative numbers a red background and positive numbers a green background instead of the font itself.

library(gt)
library(scales)

df <- data.frame(
  group1 = c("Y", "1", "1", "2"),
  estimate = c(-5.8,-7, 1, 2),
  conf.low = c(-1,-10, 2, 3)
)


df |>
  gt::gt() |>
  data_color(
    columns = c(estimate, conf.low),
    colors = scales::col_bin(
      bins = c(-Inf, 0, Inf),
      palette = c("red", "green"),
    )
  ) |>
  gt::gtsave(filename = "test.png")

Related