如何在 R 中将“可反应”表导出到图像(PDF、JPG、PNG 或 HTML 页面?)?

问题描述 投票:0回答:3

我使用“reactable”pkg/函数在 R 中创建了一个漂亮的表。我可以将其导出(实际上,编织)为 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)
r reactable
3个回答
2
投票

下面是使用

saveWidget
将 Reactable 表保存为 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)

2
投票

我没有尝试过,但这应该可行。

reactable
生成
htmlwidget
。因此,您可以使用
saveWidget
包的
htmlwidgets
功能将表格保存在 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

2
投票

简单

reactablefmtr::save_reactable(x, "x.png")

© www.soinside.com 2019 - 2024. All rights reserved.