Search code examples
rflextable

flextable::save_as_image() includes path to image when Rmarkdown knit to Word


When I knit an Rmarkdown to Word and include a table saved as an image using flextable::save_as_image(), the function seems to also paste the path to my image above the table. How do I prevent the path from being included in the Word doc? Here's a MWE:

---
title: "MWE"
output: word_document
---

```{r include=TRUE, message=FALSE, warning=FALSE, echo=FALSE, results='asis'}
library(flextable, quietly = TRUE)
library(webshot, quietly = TRUE)
ft <- flextable( head( mtcars ) )
ft <- autofit(ft)
save_as_image(x = ft, path = "myimage.png")
knitr::include_graphics("myimage.png")
```

Here's the output I get:

enter image description here


Solution

  • save_as_image returns the filename that you saved the image to. You can hide it with invisible().

    invisible(save_as_image(x = ft, path = "myimage.png"))