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:

  1. Error: Functions that produce HTML output found in document
  2. targeting X output. Please change the output type of this
  3. document to HTML. Alternatively, you can allow HTML output in
  4. non-HTML formats by adding this option to the YAML front-matter
  5. of your rmarkdown file:
  6. always_allow_html: yes
  7. Note however that the HTML output will not be visible in
  8. 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 2023) in R and also install PhantomJS:

  1. install.packages("webshot")
  2. 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. Section 2.10 of the bookdown book contains more information on finer control over the screenshots.

References

Chang, Winston. 2023. Webshot: Take Screenshots of Web Pages. https://wch.github.io/webshot/.