R Markdown website
While working on articles within this blog, I can utilize
blogdown::serve_site() which in turn calls
servr::httw(). This will serve my site locally and trigger a rebuild whenever a file is saved. However, this doesn’t work for a separate R Markdown website I built that happens to contain slides built with the xaringan package.
In order to watch for file changes and rebuild the entire R Markdown website, I wrote the script below taking advantage of the function watch from the testthat package. The script watches the root directory of my R Markdown website for changes and rebuilds my website with a call to
The script below makes use of docopt in order to create a script that one can simply run from the command line. I place this in the root directory of my R Markdown website. Note that I actually prepend an underscore to the script name,
_watch-render-site.R, so that it isn’t copied into the
In order for the script to work appropriately, I also needed to create the file
.Renviron within my R Markdown website root directory. It has a single environment variable set as I don’t currently have pandoc on my
With the script below, my workflow for the R Markdown website then becomes:
_watch-render-site.Rfrom a command prompt.
- Write content using R Markdown.
- Save changes, allowing the entire site to rebuild.
_site/index.htmlwithin a web browser.
_site/index.htmlas the site is rebuilt.
R Markdown file
If one would like to render just a single file rather than an entire website, the function
rmarkdown::render() should be utilized instead of
Below is an example of rendering a single