Skip to content

Latest commit

 

History

History
144 lines (90 loc) · 3.99 KB

2-Rmarkdown_demo.md

File metadata and controls

144 lines (90 loc) · 3.99 KB

R Markdown Examples

author: Melissa Monk date: October 2016 autosize: true

R Markdown

In this section we're going to learn the R Markdown basics

  • R Markdown renders easiest to a HTML, but we're interested in creating a PDF (unfortunately they're virtually mutually exclusive)
  • The assessment template is designed to only render to a PDF
  • Includes flavors of R, LaTeX, and HTML
  • It can be quirky at times
    • Ex. R code will still run within an HTML comment unless also commented out

How it works

Workflow

When you render, R Markdown feeds the .Rmd file to knitr, which executes all of the code chunks and creates a new markdown (.md) document which includes the code and it's output.

The markdown file generated by knitr is then processed by pandoc which is responsible for making the TeX file and, creating the finished format.

Slide content from rmarkdown.rstudio.com

Create an R Markdown document

Open Rmarkdown_workshop2016.Rproj

Using the generalized R markdown template, we'll walk through some examples

Do this: File > New File > R Markdown ...

  • Give the document a title. This is not the filename of the document and you can include spaces and punctuation.
  • You will have to Save the document and assign a file name, without spaces
    • Save this in your working directory of the R project
  • Accept the default Author (you can change this later)
  • Change the default output to PDF
  • Click OK

Knit, Commit, Push

  • Click on the "Knit to PDF" in the RStudio top menu.

  • If all goes well, a preview version will pop-up.

  • On the top right panel in RStudio, navigate to the "Git" window

  • Click Commit

  • Check all of the boxes of the files you want to commit.

  • Writing a commit message is required. Once you do this, click Commit.

  • You can now Push these changes to GitHub by clicking "Push"

You can go to your GitHub account and see the changes

I am only going to go through examples with PDF output, since that's what the Assessment Template uses

On to examples

Open 4-Workshop_examples.Rmd

The YAML

This may be the most confusing part of an R Markdown document

  • The YAML is the frontmatter controlling the document and is at the very top of the file
  • The YAML starts and ends with ---
  • You cannot add comments or extraneous text to the YAML

Your YAML should look something like this:

yaml
---
title: "Workshop Examples"
author: "Melissa Monk"
date: "September 21, 2016"
output: pdf_document
---

See Section 4.3 of the ReadMe for details

R markdown basics

Basics are laid out here

You can often use either Rmarkdown or LaTeX syntax

R code chunks

R code chunks begin and end with three accent characters (next to the 1)

R code chunk options in the repository are set global at the beginning of the Assessment_template.Rmd

{r global_options, include=FALSE} #sets global options
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)

This is telling knitr:

  • echo=FALSE: Don't print R code
  • warning=FALSE: Don't print warning messages
  • message=FALSE: don't print R messages

The default is to evaluate the code, so we don't change it here

R code chunks

Example Chunk:

  ```r
      1+1
  ```
  
  ```
  [1] 2
  ```

If you don't set any R code chunk options, this is the default

Now we'll work through the Workshop-examples.Rmd file See the ReadMe for specifics!