How to write a report using R Markdown

Bianca Peterson
6 April 2019

Company logo

Why is this useful?

  • R Markdown documents are fully reproducible
  • You can weave together narrative text and code to produce elegantly formatted output
  • You can use multiple languages including R, Python, and SQL
  • You can choose from dozens of static and dynamic output formats including HTML, PDF, MS Word, HTML5 slides, books, websites, and more…

How-to...

  • .html file can easily be converted to other file types later
  • This will create an .Rmd file

The default YAML header

  • output: since we chose html format, this will be specified here

  • There are several other optional and cosmetic items that can be added to the YAML header, such as the font, font size, spacing, margins, etc.

A customized YAML header

  • author: Specify more authors by listing names and affiliations
  • date: Retrieve date automatically from your system
  • output: PDF document.
    • Additional output-level options are specified underneath it, with a 2-space indent.
    • latex_engine: Document will be rendered with LaTeX
    • keep_tex: The raw .tex file will be rendered along with the PDF
  • toc: A table of contents will be created

Code chunks

Code chunks (continued)

  • If you want to display the output without displaying the code in the final document, assign additional rules for that code chunk

Code chunks (continued)

  • Here are some additional rules that you might want to use:
    • eval=FALSE will display the R code without running it
    • include=FALSE will run the code, but will hide both the code and the output
    • warning=FALSE will hide warning messages that may appear, for example, when you load a library
    • error=FALSE will hide error messages
    • tidy=TRUE wraps long code so it doesn't run off the page

Code chunks (continued)

  • You can even switch between different languages in the same R Markdown document

Inserting images

  • Insert figures saved on your computer:
  • Generate a figure from code and modify the dimensions:

Insert tables

  • Standard R Markdown
  • Manually creating tables using markdown syntax

The position of the colons (“:”) indicate text-alignment

Insert tables (continued)

  • kable() function from knitr package
  • pander function from pander package

Add text

This is a level 1 header

This is a level 2 header

This starts a Paragraph Block

This will create a block quote

Format text

Text can be italicized with single asterisks

Text can be emboldened with double asterisks

Although the underlining tag has been deprecated, text can be underlined with the <u> HTML tag

Create lists

  • An
  • Unordered
  • List
  1. A
  2. Numerical
  3. List

Link to a webpage in-text

This presentation was compiled from the Coding Club: Getting Started with R Markdown licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, as well as material from svmiller on GitHub with written permission.

Compile .Rmd file into a .pdf document

  • TeX is required to create PDF output:

    • Windows: MiKTeX (Complete) - http://miktex.org/2.9/setup
      • NOTE: Be sure to download the Complete rather than Basic installation
    • Mac OS X: TexLive 2013 (Full) - http://tug.org/mactex/
      • NOTE: Download with Safari rather than Chrome strongly recommended
    • Linux: Use system package manager
  • Press the Knit button in the taskbar

  • The .html file will be saved to the same folder where you saved your .Rmd file

The final product