This page was last updated Mon Jan 06.
[Download this page as a PDF]
blogdown
R package can be used to create responsive websitesCollaborative notes: https://hackmd.io/@norcalbiostat/ichps2020_website_notes
R Markdown websites like this one can be built with a minimum of two files: a _site.yml
and an index.Rmd
.
The _site.yml
file controls the overall structure of the website, such as the order of the navigation bar and the color theme.
When these files are rendered into HTML files, they can be hosted on GitHub for free. Then after turning on a few settings in your GitHub repository (repo) these files can be viewed as a magnificent webpage.
We are going to employ the “GitHub first, R Studio second” approach.
Do together
ichps_website
so you know it’s a playground.Click ‘Create Repository’ to finish, but keep this page open. We’ll come back to it a few times.
Do together
Do together
In the Terminal window (tab to the right of the Console), create the _site.yml
and index.Rmd
files using the touch
command. Type the following commands in the Terminal one at a time, pressing Return or Enter after each line to execute.
touch _site.yml
touch index.rmd
You can view these files in the File pane (lower left). You can open supported file types by clicking on the name of the file from this pane.
Do together
_site.yml
and index.Rmd
files now._site.yml
file.This is the file that controls your overall site’s configuration. If you are familiar with R Markdown files, this is the same type of ‘language’ that is used in the header area to control the document structure.
YAML headers, and this file, are very particular about spacing and tabs. To avoid unnecessary grief we are going to copy/paste code from Chapter 10.5.1 of the bookdown website.
Do together
_site.yml
file.2
(between name
and navbar
).
output_dir: "."
This is your landing page. That is, the first page people see when they go to your website. You can add a title to this page using a YAML header as is shown in the bookdown website, or you can use a pound sign #
as a first level header to make this title.
Do together
#
sign. Using both is redundant.Since we created the .yml
file manually after creating the R Project, your project doesn’t quite know yet that it has a website to build.
Do together
There should now be a BUILD tab in the top right pane.
An HTML file should now appear in your viewer pane.
.md
and .Rmd
file in your top level project folder into an HTML file.
output:
html_document:
theme: readable
highlight: pygments
Do together
index.Rmd
, knit and refresh your browser window.Let’s get your test site up for the world to see.
Do together
git add -A
This will stage all files that have been changed and/or added. Staging is the process of adding a file to be tracked under version control.
git commit -m "first commit"
Every commit needs a message. Try to make it informative, yet brief.
git push
This will push your changes up to GitHub’s servers online. You may have to enter your github username and password here. See happy git with R for help storing your github credentials. Do together
public
folder)After the page refreshes, you will see the URL to your website in at the top of GitHub pages section.
Let’s add an example analysis project to showcase on our website.
Do together
project.Rmd
_site.yml
configuration file by adding a new text
line. Be mindful about the spacing here. - text: "My Projects"
href: project.html
git add -A
git commit -m "add project"
git push
The “simple” website that was built above can be extended and enhanced well enough with some CSS and Rmarkdown wizardry. However, sometimes you may want a little more of a ‘modern’, ‘dynamic’ or ‘responsive’ feel, or to include a blog in your website. Here are some examples:
This is when we enter the realm of [Hugo], - the self proclaimed “most popular open-source static site generator”.
Hugo is it’s own language, and for advanced usage of Hugo-based websites, some understanding of how Hugo works and reading those docs may be required.
For now, we are just going to show you how to get started in this realm of fancy-ness using the R package blogdown
. Then you can go break it later. The super official blogdown documentation is linked in the References section at the bottom of this page.
If you did not do so beforehand, install the blogdown
package, and use it to install Hugo using blogdown::install_hugo()
.
This is step 1 and 2 from the first part of this workshop.
The blogdown
package will setup the necessary files, folder structures and theme files from a pre-specified them that is available on github. The example we will start with is a very simple site containing a few pages and a blog.
blogdown::new_site(theme='yihui/hugo-lithium')
This could take up to a minute to download all necessary files.
When it is done, your website will be rendered and appear in the bottom right viewer pane. Click the ‘Show in new window’ to open it in your full browser.
Let’s start by looking at the files that are contained in the root folder of this project.
index.Rmd
: Nothing really is there! This is one of the primary differences between this type of site building and the one we just finished. Content is handled in a very different manner. Hugo + Blogdown aim to help you focus on content creation and these programs put the pieces together for you.
config.toml
: Functions similar to the _site.yml
file, you can think of TOML as having the same functionality as the YAML, but with different syntax. TOML uses a key-value pair of options, such as
title = "A Hugo website"
Areas of the website, like the menu (navbar), are controlled in sections that start with brackets and look like the following:
[[menu.main]]
name = "About"
url = "/about/"
Do together
❓ Were you able to see the results of each of the changes you made on the live site?
Open the content
folder. Right now there is only an about.md
file. Open this file.
We see a familiar YAML header, and a body of text.
Do together
This folder is where we can add more static pages, such as a CV. Since we are not using any R code here, we can stick with the simpler markdown only file format.
Do together
about.md
file, rename this as cv.md
.config.toml
file, and add a new `[[menu.main]]
section as shown below so that this link shows up in the navbar.[[menu.main]]
name = "CV"
url = "/cv/"
On your newly updated website, clicking on the icon in the top left. This will take you back to the landing page. For this theme, the landing page is a blog (instead of a static page such as About
). Clicking on one of these posts takes you to the page that contains the post itself.
The content files for posts are stored under content/post/
folder. If you look there now you’ll see some markdown (.md
), some R Markdown (.Rmd
) files, and their associated .html
output files.
Creating new blog posts
Do together
fastmap
, miniUI
, htmltools
, shiny
..Rmd
format for this example.Let’s do a quick analysis of the cars
data set to add some simple content for display.
(Instructors note: Intro, summary, pander, sentence, plot, sentence, inline R, hide code)
We’ve been “cheating” a little so far in that our site has already been “Served”. When we started the new site in step 2, not only did blogdown
download the theme files and exmaple site, it also ran blogdown::serve_site()
. This is what allows for the live preview.
At a later point when you want to come back to working on your website, opening the project file itself will not initiate a serve_site()
. You will have to do this manually. Again, there is an Addin for this.
Go ahead and save your work, add, commit, and push your content to your github account.
If you have your own server, and you are familiar with website deployment then you can roll your own as you are used to. For those of us that have absolutely no idea how to host a website, a very good option that is very user friendly (and free) is Netlify.
You could use GitHub Pages as we did previously to host a Hugo based website, but there are some tweaks that you have to do to get GitHub to work with/around Jekyll in the way Blogdown/Hugo builds the sites. If you want to learn more about the why’s, go read the docs at https://bookdown.org/yihui/blogdown/deployment.html
Do together (Time pending)
blogdown
, and not all are easy to work with. Here is a blog post by Peter Baumgartner on important thoughts on choosing a theme to work with.blogdown
: Creating Websites with R Markdown https://bookdown.org/yihui/blogdown/ , https://github.com/rstudio/blogdown