Fun blogdown in R to design a personal website
Inspired by David Robinson‘s keynote talk at the RStudio conference 2019 (summary in the following tweet), I decided to write a post about how I use Yihui‘s fantastic R package blogdown to develop my own personal website.
When you’ve written the same code 3 times, write a function
— David Robinson (@drob) November 9, 2017
When you’ve given the same in-person advice 3 times, write a blog post
Well, there are a lot of useful references to check out. Here are a few of them that I would recommend. And you can always search by keyword in Yihui's book on blogdown whenever you'd like to figure out how things work.
- Yihui Xie's video tutorial
- Weicheng Zhu's slides
- Alison Hill's set of slides
- Daniel Quintana's video and written tutorial
- Amber Thomas's blog post
The whole process is actually pretty intuitive and quite straightforward if you use RStudio and are comfortable with R, git and markdown. For the following part of this post, I'll give some general guidelines and explain a few tricky steps (at least for me). Let's get ready to fire it up! 🔥
A brand new start
By default, I assume you like the Hugo theme called academic
I'm currently using and the following guidelines are trying to show you how to build a personal website with a similar style to mine.
Get the template
- Run
install.packages("blogdown")
in R if you haven't yet. - In RStudio, click Project -> New Project -> New Directory -> Website using blogdown. Configure the directory name and path, and the hugo theme is
gcushen/hugo-academic
. By default, this will download the most updated theme version for you1. - In RStudio, click Addins -> Serve Site (BLOGDOWN) or run
blogdown::serve_site()
, then you should be able to preview the example website.
If everything works well for you so far, you've made a big move! Congratulations! 👏
Personalize everything for yourself
Everyone has his own taste, and the following steps show how I modify the template for mine. 😝
- Go to the folder
content/home
which contains the files that configure the home page. Each markdown (.md) file can be regarded as one section horizontally displayed on the home page. Remove any unwanted sections by settingactive = false
in the corresponding markdown files2. The contact information at the bottom of the home page is editable in the fileconfig/_default/params.toml
. - All the personal information at the top of the home page (Name, Role, Organization, Avatar, Social Media, Biography, Interests, Education) is editable in the file
content/authors/admin/_index.md
. - You can adjust the order of each section on the home page by changing the value of
weight
in the markdown files in the foldercontent/home
. The sections with smallerweight
would go before those with largerweight
. - The menu bar on the top of the home page is also editable in the file
config/_default/menus.toml
. Hereweight
adjusts the order of the menu items and works in the same way as for the sections. And you'll see the instructions in the comments to add a link to your resume3 in the menu bar. - Great! Until now you're done with the self introduction. 👍 Now it's time to show people your achievements by editing the file
content/home/experience.md
. If you keep thegallery
section on the home page4, you can personlize the photo wall by replacing the images in the foldercontent/home/gallery/gallery
with your own. Try adding your amazing work to the folderpost
,project
,publication
andtalk
under the directorycontent
. Just follow the template format and you'll find a way to put your own stuff there. 💪
Configure the website name
- Name it in your own way! Change
title
in the fileconfig.toml
and modifydescription
inconfig/_default/params.toml
. - If you'd like, you could personalize the URL of your site by modifying the
baseurl
inconfig.toml
. But nothing comes free. 😂 I purchased my domain name (http://annielyu.com/) from Google Domains. The registration fee costs me $12 per year. And you can add Google Analytics to your site by setting thegoogle_analytics
to beUA-ACCOUNTID-1
inconfig/_default/params.toml
. Your Google Analytics account ID can be found at setting -> admin -> account settings.
By the way, if you'd like to enable comments on your site, you can sign up an account on Disqus and configure the Comments
part in the file config/_default/params.toml
.
Deploy your website
Now comes the most exciting part. ✨ Are you ready to show your profile to the whole world? The following steps will guide you how to use GitHub and Netlify to maintain and update your website in a most effortless way!
- First of all, go 👉 and get yourself a GitHub and an Netlify account you haven't yet.
- Publish the project directory as a GitHub repository under your account. If you are uncomfortable with git command lines like me, I highly recommend downloading GitHub Desktop or Source Tree for easy manipulation of your Git repo. The following steps assume you are using GitHub Desktop and have linked your GitHub account already.
- Open GitHub Desktop, click New -> Add an Existing Repository and open the project directory.
- Before you commit adding all the files, add a new file
.gitignore
to the project repository so that some files or folders such aspublic
won't be tracked5. For your reference, my.gitignore
file looks like the following:
.Rhistory
.RData
.Ruserdata
.DS_Store
public/
.Rproj.user/
.Rproj.user
- Make your GitHub repo online by clicking Publish repository. Next let's log in to Netlify and click New site from Git. Follow the instructions and pick the GitHub repo you just created. It's important in the 3rd step, you change the basic building setting by clicking show advanced -> new variable, setting
Key
asHUGO_VERSION
andValue
as the minimum hugo version you need for the current version of the academic theme6. If the site is deployed successfully, you've hit another milestone, bravo! 🎉 - You can change the site name so that the web address would be
xxxx.netlify.com
. Wait?!!! You've already purchased a custom domain name like I do? Go to Settings -> Domain management -> Add custom domain, then follow the instructions. Once you complete all the steps, you are all set! 👍
From now on, once you push your changes to the remote GitHub repo, the website would be updated accordingly as if by magic! 🌟
Some useful tips
Now you've come a long way to collect all the fundamentals to buid your site. There are still a lot of interesting features you can explore out there. Below are some tips based on my exploration. Take whichever you like to decorate your site! 🌺
- You can use emoji everywhere in your site. Check out the emoji cheatsheet. By default,
enableEmoji = true
inconfig.toml
. - Don't forget to set
hasCJKLanguage = true
inconfig.toml
if you use Chinese/Japanese/Korean languages somewhere. - Set a rule to generate permanent links of your posts using
slug
instead oftitle
by adding the following toconfig.toml
. Hereslug
can be regarded as an unique ID for your post. It turns out to be pretty useful if you can't help changing your post title after sharing the post link on social media.
# Rules to generate permanent links of your pages
[permalinks]
post = "/:year/:month/:day/:slug/"
- Check out Hugo's Build-in Shortcodes. That's how I embed the twitter and the Youtube video in this post. 😄
- You may notice there is a section called
Shiny
on my website which is not included in the template. Well, I almost adapted everything from theprojects
section. 😂 All I did is: (1) renamingprojects.md
undercontent/home
intoshiny.md
; (2) renaming the folderproject
undercontent
intoshiny
; (3) adding the following inconfig/_default/menus.toml
[[main]]
name = "Shiny"
url = "#shiny"
weight = 10
- As you may see it,
[Shiny](#shiny)
would create a hyperlink that would take site visitors to theShiny
section. The same logic applies totags
andcategories
. Below is part of the source code for my biography. And I bet you've seen how the hyperlinks work. If not, check it out! 😆
I enjoy [programming](/tags/rstats/), [photographing](#gallery) and [dessert-making](/tags/foodie/).
- It's suggested to delete any non-customized files in the folders under
content
such ascourses
andslides
so that the links to those files won't be accidentally exposed to visitors. By default, those files are searchable on the site and may appear in theRelated
section such as below a post. - Somehow the old way of making a foonote such as
^[here is a footnote]
is not working for a newer version of Hugo. Check their new manual for guidance.
Well, that's all I have to suggest for developing your personal website with R blogdown. And finally I can check ✅ write a post about blogdown in my to-do list for winter break (on the last day 😅)! Please feel free to comment below this post if you have any questions or would like to share your own website with me. Have fun blogging and exploring!
-
At the time when I write this post, it's version 4.7.0. ↩︎
-
For me, I disabled
accomplishments
,demo
,hero
,skills
,slider
andtags
. ↩︎ -
If you're having great fun using R to make HTML files, I highly recommend Yihui's another wonderful package pagedown to help you build a nicely formatted HTML file for your resume just like mine! ↩︎
-
You can also disable the
gallery
section by settingactive = false
in the filecontent/home/gallery/index.md
. ↩︎ -
You should always keep the folder
public
untouched and this folder is not needed for deployment. ↩︎ -
You can check the miminum Hugo version from the academic theme introduction page. At this time, version
0.61.0
would work. ↩︎