htmlrr-markdownknitrwebshot

How to include html (`flextable`) inside an md/github document using `webshot` and `phantomjs`?


I never had problems using the webshot package and phantomjs to automatically take a screenshot of an html table and include the screenshot in the rendered document, such as on an GitHub README document. For reference, see: https://bookdown.org/yihui/rmarkdown-cookbook/html-widgets.html

HTML widgets (https://www.htmlwidgets.org) are typically interactive JavaScript applications, which only work in HTML output. If you knit an Rmd document containing HTML widgets to a non-HTML format such as PDF or Word, you may get an error message like this:

Error: Functions that produce HTML output found in document targeting X output. Please change the output type of this document to HTML. Alternatively, you can allow HTML output in non-HTML formats by adding this option to the YAML front-matter of your rmarkdown file:

always_allow_html: yes

Note however that the HTML output will not be visible in non-HTML formats.

There is actually a better solution than the one mentioned in the above error message, but it involves extra packages. You can install the webshot package (Chang 2022) in R and also install PhantomJS:

install.packages("webshot")

webshot::install_phantomjs()

Then if you knit an Rmd document with HTML widgets to a non-HTML format, the HTML widgets will be displayed as static screenshots. The screenshots are automatically taken in knitr.

However, a couple months ago, it stopped working for objects from the flextable package, whereas, it still works for other html objects like DT tables. The flextable author is not sure of the cause of this change in behaviour, and it might have nothing to do with flextable package. I was wondering if anybody else can reproduce, and perhaps, point out a solution. Thanks.

Here is my attempt at a reprex:

---
output:
  github_document
---

```{r versions}
packageVersion("webshot")
## [1] '0.5.4'

packageVersion("webshot2")
## [1] '0.1.0'

packageVersion("flextable")
## [1] '0.9.2.2'

webshot::is_phantomjs_installed()
## [1] TRUE

webshot::install_phantomjs()
## It seems that the version of `phantomjs` installed is greater than or equal to the 
requested version.To install the requested version or downgrade to another version, use 
`force = TRUE`.

webshot::install_phantomjs(force = TRUE)
## Warning in utils::download.file(url, method = method, ...): the 'wininet'
## method is deprecated for http:// and https:// URLs

## phantomjs has been installed to C:\Users\rempsyc\AppData\Roaming\PhantomJS
```

```{r DT}
DT::datatable(head(iris))
```

enter image description here

```{r flextable, eval = TRUE}
flextable::flextable(head(iris))

Error: Functions that produce HTML output found in document targeting 
gfm+tex_math_dollars-yaml_metadata_block output.
Please change the output type of this document to HTML.
If your aiming to have some HTML widgets shown in non-HTML format as a screenshot,
please install webshot or webshot2 R package for knitr to do the screenshot.
Alternatively, you can allow HTML output in non-HTML formats
by adding this option to the YAML front-matter of
your rmarkdown file:

  always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.
```

Solution

  • Solution is to regress to knitr version 1.40.


    knitr version 1.41 and above now breaks the natural html flextable to webshot image integration for GitHub/md documents. I have opened a GitHub issue about this bug: https://github.com/yihui/knitr/issues/2265