Introduction to R Markdown

Allison Roberts

8/25/2020

Overview of R Markdown

Why R Markdown?

  • Reproducible
    • Save time generating the same report each month
  • Reduces errors (and makes it easier to fix them)
    • Easily check whether the tables or graphs are using the correct version of data
    • If it is not, easily change it
  • Flexibility
    • You can be explicit about anything, and design a product that looks EXACTLY how you want
    • Because all of your decisions are expressed in code, creating a new product with those same designs is easy

Some Drawbacks

  • Time (especially in the beginning)
    • Changing any default options will take you a lot of time
    • Weird problems can arise and be difficult to diagnose
  • Compatibility for sharing
    • Some co-authors may not want a PDF or HTML
    • Output to Microsoft products (PPT, DOC) is possible, but not optimized

Two Main Output Types:

  • HTML
    • Default for R Markdown
    • Included with initial installation
  • PDF
    • Requires additional installations

Sharing Options

  • Files
  • RPubs
  • Shiny

Rendering HTML

HTML Default Outputs

  • Web page (default)
    • Can be interactive
  • Presentation
    • Can be rendered using several programs (ioslides, Slidy, R Presentations)

Advantages of HTML Outputs

  • Very easy to create something basic, but professional looking
  • (Sort of) easy to customize
    • If you know CSS or HTML, you can do elaborate customizations
  • Best for anything interactive

Useful HTML Extensions

  • Bookdown
    • Create multi-paged HTML documents, with tabs
  • Blogdown
    • Create a full website
    • Can be hosted through GitHub
  • RevealJS
    • My personal favorite for presentations (used for this one!)
    • Provides tons of customization options
  • Flexdashboard

Rendering PDFs

Getting Started with PDFs

  • PDFs are generated using Latex
  • You need to install a distribution of Latex on your machine to use this software
  • I recommend TinyTeX, as it is easy to install and use

To install TinyTex:

# Install the R Package
install.packages("tinytex")

# Use the package to install Latex
tinytex::install_tinytex()

Default PDF Ouputs

  • PDF Documents
    • Very professional looking
    • Have a cool, Latex-exclusive font (Computer Modern)
    • Easy to include formatted tables, figures, and in-line code
  • PDF Presentations (Beamer)
    • NOT attractive

Great PDF Extension

  • Bookdown!
    • Designed to help you write an entire book in R, but also provides some useful extensions
    • Link to figures in text, automatically number figures and tables, and more

(Easy) Customized Slide Examples

Background Images

  • Display static images that automatically change size with the presentation

Background Videos

  • Display dynamic video behind text

Introductory Materials (all by Yihui Xie)

More Advanced Reading

Examples: