Setting up your blog with RStudio and blogdown II: Workflow

[This article was first published on R on , and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


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
  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

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 the new post (even some days), I suggest to include also the variable

draft: true

If you include this, the post will show up in the Rstudio/Hugo local developing environment. It will not be rendered when we build the updated version of the blog to deploy. Once you have finished it make draft: false so next time we build the deployment version of the blog it 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

{{< ... >}}

of like this in R markdown

{{ % ... %}}

For example, if we want to include this youtube video in your R/markdown you simply have to include the following code in your text

{{< youtube 2WTWx0yknQQ >}}

Or this tweet with this code

{{< tweet 817461069014859780 >}}

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 ~/blog/casper-two/public/*")

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 script

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!

To leave a comment for the author, please follow the link and comment on their blog: R on . offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)