Can you save a gt() table with a transparent background as a png

Viewed 323

Hi I am wondering if there is a way to save a table created using the gt package package and function as an image with a transparent background?

Below is some example code that creates a gt() table and saves the table as a png, but not transparent

  filter((Sepal.Length==5.1 & Species=='versicolor')) %>%
  select(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width) %>%
  gt() %>%
  tab_spanner(label = "Sepal", columns = vars(Sepal.Length, Sepal.Width)) %>%
  tab_spanner(label = "Petal", columns = vars(Petal.Length, Petal.Width)) %>% 
  tab_options(
    table.background.color = "transparent",
    column_labels.font.size = 10.5,
    table.font.size = 10,
    heading.title.font.size  = 24,
    heading.title.font.weight = 'bold',
    heading.subtitle.font.size = 11,
    table.font.color = 'white',
    table.border.top.color = "#0D2549",
    data_row.padding = px(2), 
    footnotes.font.size = 8,
    source_notes.font.size = 9,
    footnotes.padding = px(1), 
  ) %>%
  opt_table_font(
    font = list(
      google_font("Chivo"),
      default_fonts()
    )
  ) %>%
  gtsave("Image Name.png")
1 Answers

Here's what I've used in my Shiny App to make the GT table background transparent using CSS colors.

tab_options(
    table.background.color = "#FFFFFF00",
    column_labels.font.size = px(10),
    column_labels.font.weight = "bold",
    table.font.color = "white",
    table.font.size = px(12L)
)

Here's what the table looks like. Note that the Shiny App's background is black so the table shows black (although it's actually transparent).

GT Table in Shiny App

Related