Search code examples
rpngviewerflextable

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. 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!


Solution

  • To save a flextable as a png, you will need to use flextable::save_as_image().

    library(flextable)
    myft <- flextable(head(mtcars), col_keys = c("am", "carb", "gear", "mpg", "drat" ))
    
    save_as_image(myft, path = "flextable.png")
    

    enter image description here