Migrating blog to Blogdown

· by Mikey Harper · Read in about 7 min · (1414 words) ·
Mountain View

I have been delaying it for a while, but I finally decided to move to blogdown. This post is designed to quickly capture my reasons for me changing, and highlight some of the features that may make you consider doing the same.

Note: this post does not intend to be a full tutorial on how to set up your blog. For detailed guidance on setting up I highly recommend the excellent guidance from the package author: https://bookdown.org/yihui/blogdown/

My previous workflow

When I started building websites, my preferred tool was always Wordpress. It is easy to use, and things can quickly be customised with themes and plugins to add the functionality required for the full website. However, there are some major drawbacks to this approach:

  • Websites can easily become bloated with lots of plugins. This can really start to impact on performance and make the website slow to load.
  • The blog requires a fully functioning web server to host the website, with dedicated RAM and processing power. Hosting would normally be around £30/40 a year for a small blog, and more if you have a high-traffic website.
  • Hundreds of requests can be sent to the server every time a single person visits a web page. If there is a spike in the number of visitors, the server can easily be overloadeded and the website will grind to a halt.

Last year, I heard about static site generators. The proposition of a static site is to shift the heavy load from the moment visitors request the content to the moment content actually changes i.e. when the website content is being updated by the website owner.1 Some of the key advantages are:

  • Speed: as the website is hosted as HTML files with no databases queries, the website is lightening fast.
  • Ease of Hosting: there is no need to install and maintain the server infrastructure to run a dynamic website.
  • Security: since the web server only hosts plain HTML files, there is no way for people to target your web server.

One of the most popular services for creating static websites is through GitHub pages. This service builds upon the Jekyll service to rebuild the HTML files of the website any time any changes are made to the GitHub repository. I migrated my website from Wordpress to Jekyll in January 2017, and resulting website was very quick and easy to maintain, and generally I was very happy with the outcome.2

Whilst Jekyll made the website building process much easier, it was still slightly awkward to share any data analysis within my blog. The main purpose I write articles is to share the outcomes of my work, which I normally write my work in R Markdown. Within the GitHub Pages workflow, I would first have to save the R Markdown file to a Markdown file, then push the changes to the server. This admittedly was not a major problem, but it was always a barrier in the workflow and I found it quite frustrating to work with.

Moving to Blogdown

Blogdown is an R package which is designed to simplify the building of websites directly from R Markdown documents. As described by the package author3:

“In a nutshell, blogdown is an effort to integrate R Markdown with static website generators, so that you can generate web pages dynamically. For example, you can use R code chunks (or other languages that knitr supports) to generate tables and graphics automatically on any web page.”

Blogdown brings together several different different services. Firstly, the content is written in R Markdown. The blogdown package then uses Hugo (an alternative static site generator) to build the website as a local preview, making it very easy to quickly review any changes you have made. Once you are happy with the changes, you can push the full website to GitHub, and the website is updated by Netlify.

At first, the amount of different components required to get this working can appear intimidating. However, the book provided by Yihui provides an excellent explanation of all the different parts: https://bookdown.org/yihui/blogdown/

## Warning: package 'DiagrammeR' was built under R version 3.5.3

Figure 1: The blogdown workflow

Setting up a Website

The process of using blogdown is made very easy within the latest release of RStudio. A basic website can be generated within minutes, and content quickly added. There are hundreds of themes available for use, and if you are not fussy about the exact details of what you want, you can find a good looking website template here.

If you are like me and are quite picky about all the aspects of the theme, you may take a bit longer to find the right theme. Hugo provides a lot of documentation on how themes can be edited. However, it helps to have a basic understanding of HTML and CSS if you want to make any changes to a theme.

Migrating old content across was relatively painless, and Chapter 4 of the Blogdown book was very useful in assisting with this.

Example Features

My favourite feature is definitely how easy it is to embed interactive graphics directly within the blog post using R. For inspiration, you can check ou the htmlwidgets web page, but as an example, we can create an interactive map in only a few lines of code:

library(leaflet)
## Warning: package 'leaflet' was built under R version 3.5.3
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=-1.39595, lat=50.93463, popup="Southampton University")
m

Figure 2: A leaflet map, with an example point

You can also use one of many different packages to plot interactive HTML charts. The following plot is quickly made with the plotly library:

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.3
library(plotly)
## Warning: package 'plotly' was built under R version 3.5.3
p <- ggplot(data = diamonds, aes(x = cut, fill = clarity)) +
            geom_bar(position = "dodge")
ggplotly(p)

Figure 3: An interatice bar chart

There are loads more amazing HTML widgets which can be used, and you should definitely check out some of the interactive content you can quickly create. I plan on writing a full post in the future with some more cool features of blogdown, so watch this space!

Conclusion

If you regularly use RMarkdown for your analysis, blogdown is the perfect option for building your blog. This article aimed to provide a short overview of my experience in using blogdown, and highlight the reasons which made me finally shift over to blogdown. If you have any comments or questions.


  1. A great explanation of the difference between dynamic and static sites is avaialble here: https://davidwalsh.name/introduction-static-site-generators

  2. You can still see a copy of the old website here: https://mikey-harper.github.io/

  3. https://blog.rstudio.com/2017/09/11/announcing-blogdown/