使用 R flextable 包时出错 - 无法加载临时图像文件

Error using R flextable package - cannot load temporary image file

我正在尝试使用 flextable 将数据框保存为图像文件,最终目标是将其绘制在 ggplot 对象旁边。这是代码示例:

library(flextable)
ft <- flextable( head( mtcars ) )
ft <- autofit(ft)
tf <- tempfile(fileext = ".png")
save_as_image(ft, tf)

其中returns出现以下错误:

Could not load  file:///D:/Antonis/Documents/file:/C:/Users/Antonis/AppData/Local/Temp/RtmpmgSSYv/file2510dc77c47.html
Error in webshot::webshot(url = sprintf("file://%s", tf), file = path,  : 
  webshot.js returned failure value: 1
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="file://C:\Users\Antonis\AppData\Local\Temp\RtmpmgSSYv\file2510dc77c47.html": The filename, directory name, or volume label syntax is incorrect

文件路径好像不对。我尝试使用 setwd:

更改工作目录
setwd("C:/Users/Antonis/AppData/Local/Temp/RtmpmgSSYv")

但错误仍然存​​在:

Could not load  file:///C:/Users/Antonis/AppData/Local/Temp/RtmpmgSSYv/file:/C:/Users/Antonis/AppData/Local/Temp/RtmpmgSSYv/file25105e4f2c8b.html
Error in webshot::webshot(url = sprintf("file://%s", tf), file = path,  : 
  webshot.js returned failure value: 1
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="file://C:\Users\Antonis\AppData\Local\Temp\RtmpmgSSYv\file25105e4f2c8b.html": The filename, directory name, or volume label syntax is incorrect

当我尝试通过 tempfile 命令设置路径时,也会发生同样的情况:

tf <- tempfile(tmpdir = "C:/Users/Antonis/AppData/Local/Temp/RtmpmgSSYv", fileext = ".png")

save_as_image 帮助文件也没有提供一些见解。

上面的代码很通用,实际上是从博客 post 中提取的,所以我的问题似乎是局部的。 有什么建议吗?

不确定 flextable,但您可以使用 tableHTML 作为替代方案。您可以指定一个文件保存在本地,或者它会自动保存在一个临时文件中。

library(tableHTML)
mtcars %>%
 head() %>%
 tableHTML() %>%
 add_theme('scientific') %>%
 tableHTML_to_image()

输出: