This document shows how to use RMarkdown in RStudio.
After completing this module, users should be able to:
Recommended: James et al, An Introduction to Statistical Learning, Chapter 6.2.
(This can also be done in Markdown format for links.)
The four main types of code RMarkdown knows are Markdown, raw html, R code, and Latex.
Note how section titles are offset with double pound signs, and subsections with triple pound signs.
Emphasis with underscores.
More emphasis with more underscores.
Table example – I don’t know why the width defaults to 100% though. Note use of colons to set alignment of table column.
Tables | Are | Cool |
---|---|---|
col 3 is | right-aligned | $1600 |
col 2 is | centered | $12 |
zebra stripes | are neat | $1 |
See here for more on Markdown formatting:
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
\(3x+y\) Latex math inbetween dollar signs.
Math on its own line with double dollar signs.
\[\frac{2}{3x}\]
More complicated example:
\[\sum_{i=1}^n (y_i - \hat{y}_i)^2 = \sum_{i=1}^n (y_i - \beta_0 - \beta_1 x_1 - \beta_2 x_2 ...)^2\]
Note that Latex-coded equations require mathjax.js, an extra bit of javascript that does the typesetting on the fly when the html page is loaded by the user. But I think this too is automatically folded into the .html file, and shouldn’t require anything extra once the .html file has been knit.
Latex is a complex typesetting language, but RMarkdown is mainly designed for simple math. Some more info on math in Latex can be found here:
All R code goes in between the triple apostrophes, with various options:
The basic just runs the code and prints the output, in separate boxes:
2+3
## [1] 5
To remove the ugly pound signs, I always use the comment=NA
option (not also how in this sentence, the single apostrophe creates text formatted in a code-looking way).
2+3
[1] 5
If you want to suppress warnings (eg, when loading a package, or when a function is too talkative) us message=FALSE,warning=FALSE
options:
library(ggplot2)
Note that every time you knit a RMarkdown file, it starts a single instance of R running in the background, so objects created or loaded earlier in the file are still available to code chunks later in the file; however, once the file is knit, the R instance is ended and everything is erased from R’s memory.
To add R plots, just execute them as usual, but use some image options to make it look nicer. I use something like fig.width=5,fig.height=4,fig.align='center'
.
plot(cars)
To add an external image, you can just use html formatting. Local images or external images both work, and for the current template settings, both R images and external images are hard-coded into the html output without further need for resources once the Rmd has been knit.
(Note that you need to have your original image somewhere the SRC can find it, eg in a local directory shared with the Rmd file.)
You can also use Markdown format to include images.
I’ve added a little javascript to allow the simplest of interactions. That script is in css/quizzer.css
, which is in the header of this file, although there is also necessary stuff in the header.html and footer.html files.
Here’s an example, which can be easily tinkered with:
What is 3 x 5?