r - 如何在R中将flextable保存为png
问题描述
我已经遵循了链接的建议: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")
推荐阅读
- javascript - 如何使用数组按列填充表格?
- python-3.x - 有什么方法可以从浏览器获取链接以及何时使用 python 打开浏览器?
- kudu - Kudu的MaintenanceMgr线程磁盘IO很高,读写几乎一样
- apache - 无法在 cpanel 共享主机上的 Laravel5.8 站点中使用 .htaccess 访问子域
- r - 如何将 read_html 的输出保存和读取为 RDS 文件?
- python - 两个排序数组的中位数 - 下面的代码有什么问题?
- java - 如何检查数组中的确切元素是否包含在存储在 ArrayList 中的数组中?
- search - 如何在这个 Flutter 应用中实现搜索
- python - 小规模实时应用程序中 Python 中的 OOP 与非 OOP 实现
- java - Java多维初始化和循环添加