How to make a website with R blogdown, Hugo, and GitHub

R Markdown has long been a key component of my research workflow, providing a legible “lab notebook” for my daily analyses. I’ve wanted to transition my Wordpress-based website to GitHub Pages for some time, preferably using R Markdown to better integrate into my workflow. I was pretty thrilled to discover the R blogdown, Hugo, and GitHub pages combination. I was able to set it up in an afternoon, and I have high hopes that it will be a good system to allow me to share my research and analyses with a broader audience.

Update 19 October, 2017: RStudio has built in a lot of these functions into their IDE now; I’d check The Blogdown Documentation for more updated information. A few notes are added at the very bottom of this page.

This post is a “how-to”, but due to time limitations it’s more of a cheat-sheet to my future self than a full tutorial - basic Git/Github knowledge, etc., is assumed. For more complete coverage, here are a few resources I used to piece this together (note some really good information was also provided in the comment sections of these blog posts):

Disclaimer: There’s a very good chance that the route I took was not the most efficient. I also use Windows and haven’t considered other systems for this post. User beware.

General approach

Github pages expects the website to be in the master branch, but blogdown stores all of the final site elements in a “public” subfolder. There seem to be two main approaches to get them to play nicely together. In one (described in the first link above), the user creates a sub branch in the main repository to store all of the source files in, leaving the public folder materials in the main branch. In the second method (described in the second post), the user creates two repositories on Github. One is the main username.github.io pages repo, the other is for all of the source files. These are then linked using Git Submodules. I chose to use the latter approach.

Blogdown and Hugo

First thing’s first: the blogdown package and Hugo need to be installed. You can also begin choosing your theme at this point - find a theme here.

In R Studio:

# install Blogdown from Github
install.packages('devtools')
devtools::install_github('rstudio/blogdown')

# use blogdown to install Hugo
library(blogdown)
install_hugo()

Github

Set up your Github repositories. We’ll follow the “User Page” example where your website is going to use your Github username: username.github.io.

Using the + icon in the upper right corner of your github page, create the following new repositories. Don’t bother creating a README.md, as blogdown needs an empty directory. I think. At any rate, you don’t need a README.

  • username.github.io, replacing ‘username’ with your username. This will be used to host your site on the master branch, using the ‘public’ folder produced from Hugo
  • website-hugo - This repository will contain all the source files from blogdown/Hugo

Back to RStudio

Next we need to create the site in RStudio using blogdown and Hugo, and then link this with the two repositories. In my normal research life, I tend to use R Projects with the built-in git capabilities, but for some reason, that gave me a lot of problems in this case. So I created an R Project for the Hugo source files, but then manually added Git to the folder and manually linked it with Github via Git Bash (rather than just intializaing the R Project straight from the Github repository, which is what I usually do. That kept crashing for me today).

So, either create an R Project or just use a dedicated working directory for your website source files. If not using an R Project, set your working directory using setwd("path/to/your/folder"). Note that commands such as new_site() and serve_site() will load your website in the RStudio viewer, and you will need to hit the stop sign to exit viewing mode and go back to coding.

This is also where you will install your theme, found here. Being an academic type, I chose Academic.

# create your site
library(blogdown)
new_site() # then hit stop

# install theme
install_theme("gcushen/hugo-academic", theme_example = TRUE, update_config = TRUE)

# make/view website with new theme
serve_site() # then hit stop

You’ll notice there are now a bunch of folders and files in your working directory, including the ‘public’ folder. Don’t move anything manually - we’ll do it in Git Bash.

Git Bash/Terminal

This part got a little fuzzy for me, because I ran into several problems including SSH authentication, the R Project Git confusion I mentioned above, and I seemed to also have a problem using the git submodule command on an empty repo. So this could probably be tweaked over time, because I’m not sure if all the steps I took are required in sequence.

Open your terminal/Git Bash and cd to your project directory for the source files (website-hugo to match my github repo, in my case). If you’re not set up with SSH yet, do that here.

Remove the public folder

rm -r public/

Initialize a git repository

git init
git remote add origin git@github.com:username/website-hugo.git

Try linking the public folder with your username.github.io repo via a submodule now, but it didn’t work for me just yet…

git submodule add git@github.com:username/username.github.io.git public

Instead, I had to commit and push my hugo source files first, then run the submodule command

git add -A
git commit -m 'initial commit'
git push origin master

git submodule add git@github.com:username/username.github.io.git public

Leave Git Bash open, but go back to R Studio

Back to RStudio

Not really sure if this is necessary…but make a few changes to site, such as altering the config file or trying new_post("Hello World"), then “compile” the site again.

# make a test post if you're into that
new_post('Hello World')

# compile site changes: preview within R Studio
serve_site() # then hit stop

# compile site without preview:
build_site()

Back to Git Bash

Now we need to commit our new changes and push them to the “public” folder. So, if you were already in your project directory:

cd public

git add -A
git commit -m 'initial site'
git push origin master

And Voila! your website should now be available at username.github.io, and you should have a whole mess of files in your username.github.io repository now. Repeat those steps for any website changes you make:

  • work on your site in RStudio
  • run build_site()
  • open Git Bash, cd to your public folder, and add/commit/push changes

I had also made a local repository for username.github.io…but I’m not sure if that’s necessary either. I haven’t used it yet, and you can always recreate the files pretty easily with blogdown/Hugo.

Further tips on creating content

New page types

Blogdown contains wrapper functions to run Huge commands. So instead of hugo new post/my-article-name.md, you enter wrapper functions directly into the console, such as new_post(title="My article name"), plus additional arguments as needed (see code examples below.)

I’m learning this as we speak with this blog post, but the rmd = TRUE argument took me a moment.

# to make a new blog post, using Rmd instead of md:
new_post(title='How to make a website with R blogdown, Hugo, and GitHub', 
         rmd = TRUE, 
         categories = c('R', 'Hugo', 'Github','website'), 
         tags = c('R'))

# to make a new publication page:
new_content(path = 'publication/food-energy-water-nexus.md',
            kind = 'publication')

Deleting files from website

There’s no way to remove files from the website without re-generating the full public folder. There’s no problem when adding content, such as new posts, but the only (?) way to remove a post is to:

  • manually delete the contents of the public folder on your computer from the website-huge folder
  • delete the contents of jdeines.github.io using that cloned repository and commit
  • re-run build_site(), which will regenerate all of the pages
  • then add/commit/push that to Github.

New RStudio Add-ins

Update, October 19, 2017
More recent versions of RStudio (v 1.1.383) and Blogdown provide a number of built in Add-ins that provide alternate ways to navigate. Here are some tips I’ve learned:

Updating your theme

You can update your theme by forcing a new installation of the theme. Note that this will overwrite any changes you’ve made, and you will need to specifically check your config.toml and front matter on all content pages to ensure they match the settings in the new files. It’s a good idea to rename the current theme folder under the themes folder to ensure you can revert back if things go wonky.

To install the academic theme again:

# update theme by overwriting
install_theme("gcushen/hugo-academic", theme_example = FALSE, update_config = TRUE, force = TRUE)

I also made a new site with the updated theme in order to easily check for differences in the settings but not muddy my website project. To do this, I:

  • In RStudio, go to File > New Project > New Directory and select build a website
  • Fill out the form, and for the academic theme enter ‘gushen/hugo-academic’ in the theme option. Uncheck the boxes.