Here, there’s no need to add hashtags before comments. Let’s use an example dataset: toothgrow. It’s a study observing effects of vit C on guinea pig tooth growth
In r markdown, actual code are called “code chunks” To denote code chunks, use 3 ` & curly brackets containing ‘r’ Use the 3 ticks to end the code chunk as well
Toothdata <- ToothGrowth
head(Toothdata)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
Hit play button to print results of a chunk in-line
fit <- lm(len ~ dose, data = Toothdata)
b <- fit$coefficients
plot(len ~ dose, data = Toothdata)
abline(fit)
Figure 1: The toothgrowth of guinea pigs
Markdown can pull values from code/data to display in the prose:
The intercept of the regression line is 7.4225
The slope of the regression line is 9.7635714
Don’t forget to ‘knit on save’
Hit enter to display text on next line
Hastags allow to add section headers in the markdown file. But there should always be space after a hashtag, otherwise it won’t work
Use the ‘>’ to add quotes
“This is a quote”
— Author
Use 2 dollar signs to add formulae
\[ p^2 + 2pq + q^2 = 1\]
Use the LaTeX cheat sheet to know the words for symbols
\[\Theta = \begin{pmatrix}\alpha & \beta\\ \gamma & \delta \end{pmatrix}\]
Following are some options for markdown file on how knitr interprets code chunks
Eval (T or F): Decides whether result of a code chunk will be displayed or not Echo (T or F): Decides whether the code chunk itself will be displayed or not
Cache: Tells knitr to run the code chunk only once & then ignore it for subsequent knits This is great when there’s a very long section of code you don’t want to run everytime you knit, to save computing power
But remember, values pulled from cached chunks won’t update automatically if any changes have been made to the chunk
fig.width or fig.height: the (graphical device) size of the R plots in inches. Remeber, this only changes the size in externally saved plots (like jpegs) and not in the markdown file itself
out.width or out.height: output size of the R plots IN THE R FILE
fig.cap: words for the figure caption
Add a table of contents by altering the YAML code (the weird code at the very top of the document)
You can also add tabs. To do this, you need to specify each section that you want to become a tab by placing “{.tabset}” after the line. Every subsequent header will be a new tab. There needs to be lower level headers below it, for it to put them into tabs
Themes change highlighting colour, hyperlink colour, etc. This can be changed from the YAML code. Some of the themes available:
cerulean spacelab journal flatly readable united cosmo lumen paper sandstone simplex yeti null (no theme)
You can also change colour by specifying highlight:
default tango payments kate monochrome zenburn haddox textmate espresso
You can only pick either theme or highlights
Allows the reader to toggle between displaying or not displaying the code. This is done in YAML by:
code_folding: hide
To end the tabset, simply add a section header of a higher level & add .unlisted & .unnumbered
There are a TON of options & ways to customize your R code using HTML format. This is also a great way to display your portfolio