Quick tip: Live preview of R Markdown reports

I don’t know how you create markdown-files, but I typically do the following:

  • Create a new raw document
  • Update the YAML to my ‘standard’ settings
  • Create the rest of the document interactively, only running the chunks
  • Knit the full document only when I’m close to done with the report
  • Realize all the stupid mistakes I’ve made, and spend the rest of the time looking for and fixing errors, such as:
    • Spelling mistakes
    • Variable / model being specified in chunk further down
    • Realying on object in the global environment but not specified in the report
    • Forgetting to import some package in the report
    • Setting wrong settings for some of the chunks
    • etc

Enter {xaringan}

At an online workshop at eRum 2020, someone mentioned in the chat that with the package {xaringan} you can pretty much get a live preview of you R Markdown report.

Install {xaringan} from CRAN, open a new R Markdown document, save it and run the following command in the Console:

xaringan::infinite_moon_reader()

This will knit the Markdown file and render it in the Viewer every time you save or click somewhere outside the R-markdown file, giving an experience close a live preview.

To stop the live rendering, you can use servr::daemon_list() to see the number of your session, and servr::daemon_stop(<number>) to stop that session. Alternatively, you can restart you R-session with CTRL + SHIFT + F10.

My thoughs

I’ve used this a couple of times now, and I really like it, especially for early development. It’s perfect for getting the layout and theme right, and for smaller reports.

For larger reports it might be too time consuming to render the report often, but a possible workaround is to set knitr::opts_chunk$set(eval = FALSE) in the setup chunk, and use the live preview functionality to work on the text.

Let me know what you think, or if you have similar tricks, in the comment section below! 😄

Jan Petter Iversen
Jan Petter Iversen
Consultant

Data analytics consultant from Bergen, Norway.

comments powered by Disqus

Related