/ #blog #R 

Setting up your blog with RStudio and blogdown II: Workflow

This is Part III of my series of posts about how to setup you blog with RStudio and blogdown. The other parts are:

  • Part I about to setup the blog using Hugo, RStudio and blogdown
  • Part II explains my workflow of creating new posts.
  • Part III on how to modify the theme.

Workflow

In Part I of this series of post we setup our new blog using blogdown and Hugo. Once the blog is configured, this is the typical workflow I follow to write new posts and update my blog online:

  1. Open your blog project with RStudio
  2. Load the blogdown library and start the Hugo server and browser
library(blogdown)
blogdown::serve_site()
  1. Create new post: the best way is to use the RStudio Addins → New post. The Addins menu is in the top bar of RStudio, but you can also get it using
blogdown:::new_post_addin()

Then you will get a window like this

Fill in all the details. Don’t forget to choose plain markdown or R markdown. Use the latter if you want to include some R code.

A new markdown will open with the YAML header filled with the title, author, date, etc. of the posts.

Take some time here to change/add categories and tags to your blog. You can also:

  • Add a featured image which will appear at the top of the post in some themes (like the casper-two that I chose)

image: /img/posts/image_for_this_post.png

  • Since you will take some time to write your new post (even some days), I suggest to include also the variable:

draft: true

in the YAML. If you do this, the post will show up in the Rstudio/Hugo local developing environment. But it will not be rendered when we build the deploy version of the blog. Once you have finished writint it change it to draft: false so next time we build the deployment version of the blog the post will be included.

  1. After that start writing your amazing post. Everytime you save a new version of the R/markdown file, the Hugo server will render the new version of the blog and you will get in the console something like this

Editing a R/markdown is super easy. You can find some really good guides to do that here

Underneath Hugo is taking care of converting everything into HTML. This means that you can also use Hugo shortcodes in the markdown to do things like inserting a tweet or a youtube video. Shortcodes are simple shortcuts to render HTML, iframes, etc. in those cases. Shortcodes appear in your R/markdown like this in markdown

{{< myshortcode >}}

of like this in R markdown

{{ % ... %}}

For example, if we want to include this youtube video https://www.youtube.com/watch?v=2WTWx0yknQQ in your R/markdown, you simply have to include the following code in your text:

Or to include this tweet https://twitter.com/xieyihui/status/817461069014859780 simply use this code

Check the complete list of already available shortcodes here. You can even write your own ones as we will see later in Part III of this tutorial.

  1. If you are happy with your post, remove the draft: true line in the YAML and we are ready to deploy the new version of your blog to our domain. Here is the commands in RStudio I use to do that:
system("rm -r ~/best_blog_ever/public/*")
blogdown::hugo_build(local=F)
system("~/best_blog_ever/deploy.sh")

The fist command simply erases the public directory that contained the old version of the blog. This is probably not needed, but it gives me peace of mind :).

The second command builds the blog. Note the local=F flag which tells Hugo to build a deployment version of the blog, meaning that all the posts with draft: true will not be rendered.

The third command is my personal deployment script. Although most of the guides out there show you how to deploy your blog using Netlify and/or GitHub, I am using my own domain and space at Dreamhost. Then to update my blog I simply synchronize (using rsync) the public directory with the root directory of my domain. Here is my deploy.sh script

#!/bin/sh 
USER=your_user_to_your_blog_domain
HOST=your_dream_host_machine.dreamhost.com
DIR=your_blog_domain/
rsync -avz --exclude-from 'exclude-list.txt' --delete public/ ${USER}@${HOST}:~/${DIR}
exit 0

you might also notice that I have excluded some files in the synchronization which are specified in the file exclude-list.txt. In my case I only have the .htacces file there.

All done!

Author

Esteban Moro

Professor at Northeastern University. Working on Complex Systems, Social Networks and Urban Science.