Main Heading of the report
Plain text can be included as part of the report. This can be used to give introduction and explanations to your R code and results. Level 1 heading is added using ‘#’.
Blank line generates a new paragraph
Including Text and Formatting
Level 2 heading added using ‘##’, deeper heading using ‘###’ and so on.
Bulleted list using ’*’
- item 1
- item 2
- item 3
Numbered list using ‘1.’
- item 1
- item 2
- item 3
Formatting parts of text by enclosing within the symbols as below:
italic, italic, bold, bold, monospace
Horizontal line using ‘- - -’
Including Code
Code chunks start with {r name, option=value} and end with
. Instead of typing them manually, they can be created in RStudio using Ctrl+Alt+i command (command+option+i in mac). Optional parameters can be specified to customize the display of code chunks in the report.Some commonly used options below:
- include=TRUE (default) displays all code and results in the report, and include=FALSE runs the code but doesnot display it in the report
- echo=TRUE displays the code in the report, but not the results, and echo=FALSE hides the code in the report
- warning=TRUE/ FALSE to display warnings generated
- cache=TRUE to use knitr for caching to improve performance for time-consuming computations
Inline code can be added by enclosing in single quotes E.g. “Two plus two equals 4” displays “Two plus two equals 4”
knitr::opts_chunk$set(options) sets options globally to all code chunks unless overwritten by local options
Code chunk calculating and displaying results
Using a sample data set to demonstrate code usage:
data("cars")
head(cars)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
cars$time <- round(cars$dist/cars$speed,2)
cars$time
## [1] 0.50 2.50 0.57 3.14 2.00 1.11 1.80 2.60 3.40 1.55 2.55 1.17 1.67 2.00
## [15] 2.33 2.00 2.62 2.62 3.54 1.86 2.57 4.29 5.71 1.33 1.73 3.60 2.00 2.50
## [29] 1.88 2.35 2.94 2.33 3.11 4.22 4.67 1.89 2.42 3.58 1.60 2.40 2.60 2.80
## [43] 3.20 3.00 2.35 2.92 3.83 3.88 5.00 3.40
Including Figures
Any figure produced by the code will be automatically included in the report when the code is run. You can hide accompanying code using echo=FALSE in the code chunk. Figure display options can be set in the code chunk as well as follows:
Barplot of Distance vs Speed
Including Tables
kable()
function in the knitr
package can be used to produce a table in the report with a matrix or data frame as input. To further customize the table with advanced fomatting options, use pander package.
speed | dist | time |
---|---|---|
4 | 2 | 0.50 |
4 | 10 | 2.50 |
7 | 4 | 0.57 |
7 | 22 | 3.14 |
8 | 16 | 2.00 |
9 | 10 | 1.11 |