RMarkdown workflow

  1. In your project, create two directories: scripts and reports

  2. Create a script prepare.R in the scripts directory

  3. Create an RMarkdown document from template, save it as final.Rmd in the reports directory.

  4. Add code to prepare.R to transform the flights data in some way. (Use subsetting, add new variables, or compute a summary. Anything works.) Assign the result to a global variable using <-. Save this variable to a file using the saveRDS() function.

    Hint: Use library(here) and here() to control where the file is saved.

  5. Leave only the header and the “setup” chunk in final.Rmd. In the “setup” chunk, insert a call to the readRDS() function for the file you saved in the previous step.

    Hint: Use here again, you should be using the same file name that you have used for saveRDS().

  6. Knit the document. This should work without failure (but show an empty document).

  7. Create a second code chunk that shows a plot of your transformed data. Do not refer to flights. Knit again.

Create site from project documents

A convenient way to create a website/an html document from several Rmd-files within a project is provided by the function rmarkdown::render_site(). We will give an overview of how it works in this section, but you can always consult the help page of the function with ?rmarkdown::render_site().

An example of the workings of this function and the resulting html file can be found under the following link: https://github.com/krlmlr/vistransrep/tree/master/render-site-example. Just call render_site() in the associated project (render-site-example.Rproj) and the html-files will be generated. You can see the result when opening index.html in your browser.

There are important ingredients that need to be present for this to work:

  1. A file "_site.yml" needs to be present in the main directory of the project. It should minimally contain the output directory for the html-files (best practice when working on github is the setting; “output_dir: docs”).
  2. A file “index.Rmd”, from which the homepage of the website will be constructed. It should link to the other html-files (Rmd-files with replaced extension “Rmd -> html”)

It is best to have all the Rmd-documents you want to include in the website in the main directory of the repository. This way render_site() will detect the files by default and create html files from them in the directory given in "_site.yml".

Further possibilities:

  1. You can setup any github-repo to automatically create and update a website from a subdirectory of the main repo directory called “doc”, containing the html file. This website will then be updated, whenever the html file changes in the master branch of the repo.

  2. This here is a quick and easy way of binding together a few Rmd-Files as a website. For the creation of larger html-documents we strongly recommend using bookdown instead. For more information see https://bookdown.org/.

Slides and reports

  1. Choose File / New File / R Markdown… from the RStudio menu. In the dialog, select “From template” in the pane on the left-hand side and then “Ninja presentation” to create an example slide deck. Knit it.

  2. What templates are available from the “rticles” package? Are any of them useful for your work?

  3. Navigate to https://github.com/rstudio/bookdown-demo (or search for “bookdown demo” in a search engine). Extract the demo to a directory of your choice, and follow the instructions in the README.md file.

Copyright © 2019 Kirill Müller. Licensed under CC BY-NC 4.0.