简体   繁体   English

如何在R中将flextable保存为png

[英]how to save flextable as png in R

I already followed the recommendation of the link: R save FlexTable as html file in script , but it seems that I am facing a different problem, because this solution did not work for me. 我已经按照链接的建议进行操作: R将FlexTable保存为script中的html文件 ,但是似乎我遇到了一个不同的问题,因为此解决方案不适用于我。 The vanilla.table () function generates an object other than the flextable () function. vanilla.table ()函数生成除flextable ()函数之外的对象。

I'm using flextable because it allows for desirable formatting possibilities 我正在使用flextable因为它允许理想的格式化可能性

Example: 例:

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: I know it is possible to download the photo manually by clicking on "Export> Save as Image", however I need it programmed ps:我知道可以通过单击“导出>另存为图像”来手动下载照片,但是我需要对其进行编程

thanks in advance! 提前致谢!

To save a flextable as a png, you will first need to save it as an html file then to use webshot to get a png from the html file. 要将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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM