blogdown Part I: Launching a New Site
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Purpose
- A basic workflow to set up an R blog using blogdown.
- Create using the Hugo theme Tranquilpeak (v0.4.3).
- Host static website on Netlify
- Source files stored on github
- Configured so that RSS feed can be shared with R-bloggers
Disclaimer
Instructions here will rarely be explicit. These workflows are essentially notes for experienced users to help remind them of the minimal steps to complete the task at hand.
Prerequisites
- Strongish familiarity with RStudio, R packages, and R projects
- Medium familiarity with git and github
- Some familiarity with
blogdown
- Installation of
R
,R-Studio
,blogdown
, and all associated dependencies - A Netlify account linked to Github
Workflow
- Start project
- RStudio toolbar:
File -> New Project -> New Directory -> blogdown website
- Copy
kakawait/hugo-tranquilpeak-theme
into theHugo theme
field
- RStudio toolbar:
- Version control
- RStudio toolbar:
Tools -> Project Options -> Version Control -> select git
- Add the line
public
to.gitignore
Track all project files:
git add --all git commit -am "First commit"
- Create a new repository on github
Link local repo to remote repo:
git remote add origin [email protected]:username/blogname.git git push -u origin master
- RStudio toolbar:
- Nelify
- Add following line to
config.toml
file:ignoreFiles = ["\\.Rmd$", "\\.Rmarkdown$", "_files$", "_cache$"]
- Go to Netlify and select
New site from Git
- Set the following options:
- Build command:
hugo
- Publish directory:
public
- Build command:
- Change build variables:
- Find local hugo version
blogdown::hugo_version()
Settings -> Build and Deploy -> Build Environmental Variables
Set key -> HUGO_VERSION and value -> 0.42.1
(or the appropriate version)
- Find local hugo version
- Change to desired site name:
- On Netlify:
Settings -> Site details -> Site information -> Site name
- On Netlify:
- Add following line to
config.toml
- Update
baseURL
to netlify address - Make other changes as desired
- Update
- RSS feed
- Note: By default, the RSS feed for Tranquilpeak is set up to display article summaries, which will not allow automatic uploading to R-bloggers. A solution (reproduced here) can be found on the blog: cool but useless
- Create the file
rss.xml
in the following directory: themes/hugo-tranquilpeak-theme/layouts
Copy and paste the following text into this file (unmodified version here):
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> <channel> <title>{{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title> <link>{{ .Permalink }}</link> <description>Recent content {{ if ne .Title .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }}</description> <generator>Hugo -- gohugo.io</generator>{{ with .Site.LanguageCode }} <language>{{.}}</language>{{end}}{{ with .Site.Author.email }} <managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }} <webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}{{ with .Site.Copyright }} <copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }} <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }} {{ with .OutputFormats.Get "RSS" }} {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }} {{ end }} {{ range .Data.Pages }} <item> <title>{{ .Title }}</title> <link>{{ .Permalink }}</link> <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate> {{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}} <guid>{{ .Permalink }}</guid> <description>{{ .Content | html }}</description> </item> {{ end }} </channel> </rss>
Potential alternative solution by Thug R Life
Add description to .Rmd posts
- Note: posts created using .Rmd documents will be formatted awkwardly on the homepage (by default, show the first 70 lines all combine into one paragraph). github user Joseph Chou (jhchou), posted a solution that works for Tranquilpeak v0.4.3-BETA.
- Navigate to
themes\hugo-tranquilpeak-theme\layouts\_default\summary.html
- Find line 43 –
{{ .Summary }}
Replace with:
{{if .Description }} {{ printf "%s" .Description }} {{ else }} {{ printf "%s" .Summary | markdownify }} {{ end }}
This will allow you to add summaries via the .Rmd post YAML header that will display the first 70 lines
--- title: "Test post 2" author: '' date: '2018-06-19' slug: test-post-2 categories: [] tags: [] description: "a description" ---
- Deploy website
- Netlify will build website in public upon push to github.
Other themes
This workflow was written with the Tranquilpeak theme in mind. However, other suitable themes may be found here:
To implement another theme, when starting a new blogdown
website, identify the user name and rep of the desired theme, and use this as prompted when starting a new website using the File
menu in RStudio.
Additional resources
R-bloggers.com 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.