Agenda

  • Introduce R Markdown to create a report that combines text, code, and output
  • Use markdown syntax to add headers, formatted text, images, and more
  • Create code chunks to embed R code in your report
  • Debug common R Markdown errors
  • Add interactivity with HTML widgets

Cheatsheets

Resources

The R Markdown parts of the R4DS textbook are Chapters 27 and 29.

For a deeper dive into R Markdown, the official R Markdown text is R Markdown: The Definitive Guide. It is most useful to have as a reference.

Another great reference text for R Markdown that contains examples of many R Markdown features is R Markdown Cookbook.

I also recommend checking out RStudio website for R Markdown. The Getting Started and Gallery pages gives a visual overview of many of the things you can build with R Markdown.

Homework Exercises

Download hw4-exercise1.Rmd
Download cloudbuddy.png
  1. Recreate a report: Download the R Markdown document and image above and store them in a new R Project alongside the cloudbuddy data from the course. Use the downloaded document as a starting point and modify it so that it ends up looking like this final report. Here are some tips:

    • The report purposefully contains a few errors that prevent the document from knitting. You should start by fixing these first.

    • There are ~15 other modifications you’ll need to make. Aside from loading packages and reading in data, you should not need to make substantial edits to the existing source code within the chunks. You also should not need to delete existing code chunks. There are a couple features which you may have to look up how to do using outside resources:

      1. Add appropriate headers
      2. A floating table of contents
      3. Use links
      4. Add footnotes
      5. Add a centered image with 80% width
      6. Use inline code
      7. Make table output interactive
      8. Add white space between all sections
      9. Create tabsets (hint: see this resource)
      10. Turn one of the ggplots into an interactive plot
      11. Allow code to be toggled between shown and hidden
      12. Use code chunk options to control whether code and/or output can be shown
      13. Suppress all warnings and messages
      14. Adding the flatly theme to your report
      15. Attempt something new: An important part of learning R is teaching yourself how to do something that you haven’t explicitly been taught. At the end of the cloudbuddy report is an empty code chunk. Within this code chunk, do something new with the cloudbuddy data that you haven’t tried in previous sessions or exercises. For example, this can be creating a new table using a dplyr function, attempting to customize a ggplot in a new way. Attempts that end in failure are okay, and exploring a new function or package is encouraged!
    • Save your modified report as session-04-cloudbuddy.Rmd.

    • Here is an annotated version of the final report to help you spot some of the differences.

  2. Render a tufte document: Make sure the tufte R package is installed. Make changes the YAML of your modified report so that it knits as a tufte document. Save your tufte report as session-04-tufte.Rmd.

  3. Knit to PDF: Create one new R Markdown document from the new document icon in the IDE. Knit it to a PDF document. (You may need to install LaTeX in order to build the PDF output — RStudio will prompt you if this is necessary, or install the tinytex package and then run this code: tinytex::install_tinytex()).

    • Save your PDF as session-04-pdf.Rmd.
    • What happens if you try to knit session-04-cloudbuddy.Rmd to a PDF document? Can you get it to work?