gpt4 book ai didi

r - 如何在R中将flextable保存为png

转载 作者:行者123 更新时间:2023-12-05 03:05:52 24 4
gpt4 key购买 nike

我已经按照链接的建议进行操作:R save FlexTable as html file in script ,但似乎我面临着一个不同的问题,因为这个解决方案对我不起作用。 vanilla.table() 函数生成一个不同于 flextable() 函数的对象。

我正在使用 flextable 因为它允许理想的格式化可能性

例子:

library(flextable)
library(rtable)

# The example below work.
myft <- vanilla.table(
head(mtcars) )
myft
writeLines(as.html(myft), "MyFlexTable.html")

# The example below does not work.
myft <- regulartable(
head(mtcars),
col_keys = c("am", "carb", "gear", "mpg", "drat" ))
myft
writeLines(as.html(myft), "MyFlexTable.html")

ps:我知道可以通过单击“导出>另存为图像”手动下载照片,但我需要对其进行编程

提前致谢!

最佳答案

要将 flextable 保存为 png,您首先需要将其保存为 html 文件,然后使用 webshot 从 html 文件中获取 png。

library(flextable)
myft <- regulartable(
head(mtcars),
col_keys = c("am", "carb", "gear", "mpg", "drat" ))

# create an Rmd file ----
library(rmarkdown)
rmd_name <- tempfile(fileext = ".Rmd")
cat("```{r echo=FALSE}\nmyft\n```", file = rmd_name)

# render as an html file ----
html_name <- tempfile(fileext = ".html")
render(rmd_name, output_format = "html_document", output_file = html_name )

# get a png from the html file with webshot ----
library(webshot)
webshot(html_name, zoom = 2, file = "regulartable.png",
selector = "body > div.container-fluid.main-container > div.tabwid > table")

enter image description here

关于r - 如何在R中将flextable保存为png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50225669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com