【问题标题】:how to save flextable as png in R如何在R中将flextable保存为png
【发布时间】:2018-05-08 04:05:16
【问题描述】:

我已经按照链接的建议: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:我知道可以通过单击“导出>另存为图像”手动下载照片,但是我需要对其进行编程

提前致谢!

【问题讨论】:

    标签: r png viewer flextable


    【解决方案1】:

    要将 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")
    

    【讨论】:

    • 直到最近,当文本为 bold 时它无法捕获此方法时,它一直运行良好。使用 MacOS 10.14.4、webshot 0.5.1 和 R 3.5.0。
    • 如果遇到错误,请使用flextable::save_as_image 并在 GH 页面中打开问题 :)
    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2019-09-23
    • 2020-10-26
    • 2017-09-25
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多