This document serves as a short introduction to R Markdown. R Markdown can be used to generate reports that adhere to reproducible research initiatives, capturing computer code, datasets, methods, protocols and results in a single document. The following highlights many of the basic commands.
The links below are helpful references to get started in R Markdown:
There are two basic modes for R Markdown: Pandoc Markdown and R code chunks. Pandoc Markdown is where you will write the body of the report.
Remember, always save your R Markdown file. To generate the report, click on the “Knit ____” icon in the menu bar of the Rmd window.
You can control different aspects of the text in Pandoc Markdown (size, italic, bullet points, etc.). Below are some examples.
Emphasis
italic bold
italic bold
Headers
Lists
Unordered List:
Ordered List:
The execution of R code in Markdown is accomplished with R code chunks. You can embed the code and make it visible or hide it (which is useful when the code generates a table or graph). Names can be given to each code chunk for navigation. Results can be cached to speed up processing time.
Generally, I like to make sure the code is working correctly by making an R Script in a separate file. I then copy and paste the code chunks into the R Markdown file.
The most useful and frequently used commands:
The remainder of the document runs through an example workflow.
# quick summary and plot
library(ggplot2)
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
qplot( speed, dist, data=cars)
To add an immage from a file, you can use the following code and packages. The image file must be in the working directory location. Replace the file location below with the location of an image you want to add.
install.packages(“png”)
You can cite R packages with the following code:
##
## To cite package 'png' in publications use:
##
## Simon Urbanek (2013). png: Read and write PNG images. R package
## version 0.1-7. https://CRAN.R-project.org/package=png
##
## A BibTeX entry for LaTeX users is
##
## @Manual{,
## title = {png: Read and write PNG images},
## author = {Simon Urbanek},
## year = {2013},
## note = {R package version 0.1-7},
## url = {https://CRAN.R-project.org/package=png},
## }
##
## ATTENTION: This citation information has been auto-generated from
## the package DESCRIPTION file and may need manual editing, see
## 'help("citation")'.
The session information contains the versions of R and all packages used in the analysis contained in your R Markdown file.
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.9.5 (Mavericks)
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] grid stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] png_0.1-7 ggplot2_2.1.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.7 digest_0.6.10 plyr_1.8.4 gtable_0.2.0
## [5] formatR_1.4 magrittr_1.5 evaluate_0.9 scales_0.4.0
## [9] stringi_1.1.1 rmarkdown_1.0 labeling_0.3 tools_3.3.1
## [13] stringr_1.1.0 munsell_0.4.3 yaml_2.1.13 colorspace_1.2-6
## [17] htmltools_0.3.5 knitr_1.14