Learning Objectives
To full participate in today’s activites, you must have the following:
knitr
, blogdown v1.3
, distill v1.2.4
,
blogdown::install_hugo()
✏️Exercise 1: Set your intentions (Think-Pair-Share)
First watch, then do
We will be alternating a lot today where you watch me demo first, then you replicate on your own or in groups.
How you will actually learn this material:
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.
website_test_rmarkdown
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.
_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 right). You can open supported file types by clicking on the name of the file from this pane. * Open both the _site.yml
and index.Rmd
files now.
✏️ Do steps 1-3. Put up a ✔️when you are done.
_site.yml
fileThis 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 the collaborative notes.
name: "my-website"
output_dir: "."
navbar:
title: "My Website"
left:
- text: "Home"
href: index.html
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.
#
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.
There should now be a BUILD tab in the top right pane.
An HTML file should now appear in your viewer pane.
Click the ‘Show in new window’ button to see your site in a full browser window.
Make a change to your index.Rmd
, knit and refresh your browser window.
✏️ Do steps 4-6. Put up a ✔️when you are done.
Notes, Tips and Comments
.md
and .Rmd
file in your top level project folder into an HTML file.
Let’s get your test site up for the world to see.
git add <file name>
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.
After the page refreshes, you will see the URL to your website in at the top of GitHub pages section.
Let’s add some content.
project.Rmd
_site.yml
configuration file by adding a new text
line to the navbar
. - text: "My Projects"
href: project.html
⚠️ Be mindful about the spacing. Make sure the - text:
is directly in line with the home page item in the YAML header.
👥 Do steps 7-8 in your breakout rooms. Come back to the main room when you are done.
menu
---
name: "my-website"
output_dir: "."
navbar:
title: "My Website"
left:
- text: "Home"
href: index.html
- text: "Courses"
menu:
- text: "Math 1"
href: math1.html
- text: "Math 2"
href: math2.html
right:
- text: "About"
href: about.html
---
Distill based websites are a nice enhancement to the Rmarkdown website. Very similar setup/structure, but with added ability to manage a blog. You can actually create static Websites with distill as well. Learn more about the difference here.
Follow steps 1 & 2 from the section above.
distill
to turn the project into a website.In the console, type the following:
library(distill)
create_blog(dir=".", title = "My Blog")
The basic scaffolding for a blog and an initial welcome post will be created within the root (.
) sub-directory.
Open _site.yml
Looks similar eh?
distill::distill_article
instead of html_document
output_dir:
in the YAML to "docs"
render_site()
in the console to build your site instead of using the build tab._site
, but does docs
.What’s happening, is that all .md
and .Rmd
files in your root directory get knitted into .html
files and placed into the docs
folder. All content in the _posts
folder also gets directly copied into the docs
folder.
Run the following code in the console:
create_post("Hello World", draft=TRUE)
_posts
subdirectory.2021-06-15-hello-world
draft: true
section in the YAML header👥 In breakout rooms:
Come back to the main room when you are done.
.nojekyll
file using either touch .nojekyll
in the terminal, or file.create(".nojekyll")
in the R console
main
branch and the /docs
folder.This was a basic introduction to these types of websites. See the documentation at https://rstudio.github.io/distill/blog.html to see how to:
The “simple” websites that were 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.
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. Alison Hill has a ton of resources on how to work with blogdown and Hugo, including a four part Summer of blogdown series of workshop notes. What we’re going to cover here is a very watered down, one hour(ish) version.
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()
.
You got this. Fifteenth time is the charm.
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')
blogdown:::serve_site()
This is the “website compilation” part. R/Markdown are turning all the .md
and .Rmd
files into HTML, Hugo is linking them together under the direction of themes and the config file.
❓ Poke around on this site. What do you notice?
Let’s start by looking at the files that are contained in the root folder of this project.
index.Rmd
: Nothing really is there!
config.yaml
: Functions similar to the _site.yml
file, with a LOT more options.
✏️ Customize your site
❓ Were you able to see the results of each of the changes you made on the live site? This is the magic of ✨Live Reload ✨.
content
folder. Right now there is only an about.md
file. * Opening this file we see a familiar YAML header, and a body of text..md
) file format.✏️ Update content
about.md
.about.md
file, rename this as cv.md
.
config.yaml
file, and add a new menu itemOn 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 posts
✏️ Add a post
.Rmd
format for this example. What this .Rmarkdown
? 🤷. See this github issue as a place to start to learn about how these two extensions work with hugo.serve_site()
to see the live preview.serve_site()
. You will have to do this manually.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
✏️ Publish
Almost done! We need to do one more big of configuration on the Rstudio side so that when you push change to github, it will trigger a redeploy automatically.
✏️ Update deployment settings
Go back to your config.yml
file and change the baseurl to your website URL.
Now you are GTG! At least at a starting page. The devil is always in the details (of the theme). That’s where we’re going next.
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.We do not have time to go through this in detail. PLEASE follow up with Alison Hill’s Up and running with blogdown in 2021 post before you start serious webpage building. She’ll set you up with all the proper checks. We’re just fiddling around here and getting you introduced.
blogdown::new_site(theme = "wowchemy/starter-academic")
widget
.content/home
folder.
.md
files are a widget.✏️ Control your widgets
hero.md
active: false
to line 3 (under widget: name)
demo.md
the same way. This one already had a spot for the active parameter.content/authors
. You specifically are in /admin/_index.md
, and AFAIK you are not Nelson Bighetti.✏️ Make it about you Change each of these one at a time, watching the live reload to make sure you know what they are linked to.
/content
with an _index.md
file
index.md
index.md
from another project, and modify.featured
posts
until you’re more familiar with blogdown.
content/home/widget.md
for customization optionsconfig/_default/menus.yaml
In recommended order
blogdown
: Creating Websites with R Markdown https://bookdown.org/yihui/blogdown/ , https://github.com/rstudio/blogdown