Insights for Action

Static Websites with Hugo and R-Blogdown

Combining Hugo and R (using R-Blogdown)

The current iteration of this website is a static website based on Hugo. In my opinion, there is a lot to like about static websites. I have previously used Jekyll and once upon a time coded html manually. However, modern static website generators allow a wonderful presentation. The currently styling is based upon the Terrassa theme; for me it scored highly for accessibility and mobile friendliness. The bigger issue for me though is to do data and version controlling. With a dynamic website, you don’t necessarily know the data someone is accessing - it is drawn from a live database which by design can be updated independently of the website. With a static website, the data become version controlled along with the website. For some websites, that is a bad design choice. But I wanted to be able to demonstrate data analysis on data that I might revisit (because it has been updated) or using new methods. All this can be version controlled.

The “source” repository, and the “public” repository are hosted on Github. Github pages are great for Jekyll, so much so they will automatically build your sites from the source files. This is not the case with Hugo and currently it is necessary to split the site into a source repo containing a public sub-repo. It is this latter sub-repo which is pushed to the github pages. This public repo contains a .nojekyll file to stop Github trying to build pages.

There are a few software ecosystems which dominate professional data analytics. For modelling and analysis I prefer R along with PostgreSQL. We often need to access a number of data munging tools to get us from messy raw data to data which can be analysed, these can include Python, awk, sed. The R ecosystem is, in my opinion, much more complete in terms of critising, validating, verifying any modelling you are doing. It is also great when you need professional output. The R package blogdown is therefore used for this website. Literate programming has been around for a long time, Donald Knuth famously proposed this decades ago. Indeed Sweave has been a stable R feature for almost 20 years. Literate programming combines analysis code with report write up. Blogdown is an extension that uses (R) Markdown in the same way to produce .html files which can be included in a Hugo blog. Consequently, R/Blogdown/Hugo provides an environment for telling data stories which can copied, corrected, amended and version controlled to provide a full audit history.

One thing I have found recently (11th January 2021), I needed to add a piece to the yaml at the top of the .Rmd file. I’ve forgotten how I arranged to get the .Rmd files rendered before, but it seems it is now necessary to include this information.

output:
  blogdown::html_page:
    toc: true
    toc_depth: 1
    number_sections: true
    fig_width: 6

With this in place, the standard blogdown::serve_site() will run the code in the .Rmd file and render it using pandoc as an html file which can be included in the website.

Share on: