I am trying to save a data frame as an image file using flextable, with the ultimate goal to plot it next to a ggplot object. Here is example of the code:
library(flextable)
ft <- flextable( head( mtcars ) )
ft <- autofit(ft)
tf <- tempfile(fileext = ".png")
save_as_image(ft, tf)
which returns the following error:
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
It seems like the file path is incorrect. I tried to change the working directory with setwd:
setwd("C:/Users/Antonis/AppData/Local/Temp/RtmpmgSSYv")
but the error persists:
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
The same happens when I try to set the path through the tempfile command:
tf <- tempfile(tmpdir = "C:/Users/Antonis/AppData/Local/Temp/RtmpmgSSYv", fileext = ".png")
The save_as_image help file also did not offer some insight.
The above code is quite generic and in fact it is drawn from a blog post, so my problem seems to be local. Any suggestions?
Not sure about flextable
, but you can use tableHTML
for this as an alternative. You can specify a file to save locally or it will be saved on a temp file automatically.
library(tableHTML)
mtcars %>%
head() %>%
tableHTML() %>%
add_theme('scientific') %>%
tableHTML_to_image()
Output: