Now that you’re hopefully becoming more confident in your R skills, we can start thinking about how to integrate code and text to make a more coherent document.

Opening up an R Markdown Script

Setting up your document

Markdown is an easy way of displaying code, output, and text all contained in the same file.

There are several useful commands that help you be flexible in displaying your output. Some include: - include: tells R whether to include the R code and the corresponding output - echo: tells R whether to include the R code - eval: tells R whether to evaluate the code in the chunk

knitr::opts_chunk$set(echo = TRUE, fig.width = 4, fig.height = 4)

Your GSI’s and Google are great resources!

R Markdown

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. Here is an example of outputing the summary statistics of the cars dataset contained in base R:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Including Math Symbols

Install the mat2tex package using install.packages('mat2tex')

Note: This package is not available for R 3.5.1. To check the version of R that you are using type version into the console.

Some Helpful Documents

Some helpful starter documents are Markdown Basics and Markdown Intro