3

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. The vanilla.table () function generates an object other than the flextable () function.

I'm using flextable because it allows for desirable formatting possibilities

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

thanks in advance!

Fellipe Gomes
  • 75
  • 2
  • 7

1 Answers1

4

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.

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

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34