How to export a 'reactable' table to an image (PDF,JPG,PNG, or HTML page?) in R?

Viewed 2213

I have created a beautiful table in R using the 'reactable' pkg/function. I can export (in effect, knit) it as an HTML page, but is there a way to export it as an image (and if so, automatically)? Not necessary, but here is some code:

x<-as.data.frame(list(a=c("why","can't","I","figure","this","out"),b=c("it","is","probably","something","really","simple"))) 
reactable(x)
2 Answers

I have not tried but this should work. reactable generate a htmlwidget. So you can use the saveWidget function of the htmlwidgets package to save the table in a html file, then use the webshot package to take a snapshot.

library(reactable)
library(htmlwidgets)
library(webshot)

rtable <- reactable(iris[1:8,])
html <- "rtable.html"
saveWidget(rtable, html)
webshot(html, "rtableSnapshot.png") # you can also export to pdf

Here's an example of saving a Reactable table as an HTML file using saveWidget then using that file and webshot2 to create a .png. Does this help?

library("reactable")
library("htmlwidgets")
library("webshot2")

html_file <- "table.html"
img_file <- "img.png"

df <- mtcars[1:3, 1:3]

table <- reactable(df)
saveWidget(widget = table, file = html_file, selfcontained = TRUE)
webshot(url = html_file, file = img_file, delay = 0.1, vwidth = 1245)
Related