如何在 R 中将 'reactable' table 导出到图像(PDF、JPG、PNG 或 HTML 页面?)?

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

我使用 'reactable' pkg/function 在 R 中创建了一个漂亮的 table。我可以将它导出(实际上是编织)为 HTML 页面,但是有没有办法将它导出为图像(如果可以,自动导出)? 不需要,但这里有一些代码:

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

我没试过,但这应该行得通。 reactable生成一个htmlwidget。所以可以使用htmlwidgets包的saveWidget功能将table保存到html文件中,然后使用webshot包进行快照。

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

这是一个使用 saveWidget 将 Reactable table 保存为 HTML 文件的示例,然后使用该文件和 webshot2 创建一个 . PNG。这有帮助吗?

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)