akang
akang

Reputation: 651

Export table created in formattable in R

I am plotting a table like below. Is there a way to save it? I can't just export it as it only exports the partial image.

test1 %>% 
  group_by(Name) %>%
  summarise("Weekly_trend" = spk_chr(Total))  %>%
  left_join(data) %>%
  formattable() %>%
  as.datatable() %>%
  spk_add_deps()

Upvotes: 0

Views: 1012

Answers (1)

neuron
neuron

Reputation: 2059

Since I answered your last question I have a bit more background. For some reason, the table you made doesn't seem to want to export with the method mentioned above.

However, you can export the table and save it as an image using the following code. First, the table gets saved as an htmlwidget. You can open the output .html file that gets output and see the table if you wanted to. webshot will then take a picture of the table and save it as an image.

library(htmlwidgets)
library(webshot)

test1 %>% 
  group_by(Name) %>%
  summarise("Weekly_trend" = spk_chr(Total)) %>%
  formattable() %>%
  as.datatable(options = list(pageLength = 30)) %>%
  spk_add_deps() -> w

htmlwidgets::saveWidget(w, "table.html", selfcontained = TRUE)

webshot::webshot(url = "table.html", file = "table.png", 
                 vwidth = 1000, vheight = 275)

Upvotes: 1

Related Questions