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.
Once you’re in R Studio, you can open up an R markdown by clicking this icon
You do not need to download anything additional if you want to knit to an HTML file.
If you would like to knit to a pdf, you need to install MiKTex for Windows or MacTex for Macs. You can download both of these here.
It is not recommended that you knit to a Word document.
Markdown is an easy way of displaying code, output, and text all contained in the same file.
{r} #example R chunk
.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
All of these commands are entered when you start the R chunk.
knitr::opts_chunk$set(echo = TRUE, fig.width = 4, fig.height = 4)
Your GSI’s and Google are great resources!
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
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.
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.
Matrix notation \(\mathbb{X}\)
Centering an Equation \[ \mathbf{\hat\beta} = (\mathbb{X}^\mathrm{T}\mathbb{X})^{-1}\mathbb{X}^\mathrm{T}\mathbf{Y} \]
Aligning Multiple Equations: \[ \begin{aligned} \mathbf{\hat\beta} = (\mathbb{X}^\mathrm{T}\mathbb{X})^{-1}\mathbb{X}^\mathrm{T} \mathbf{Y}\\ \mathbf{b} = (\mathbb{X}^\mathrm{T}\mathbb{X})^{-1}\mathbb{X}^\mathrm{T}\mathbf{y} \end{aligned} \]
Some helpful starter documents are Markdown Basics and Markdown Intro