Dependencies

This document depends on the following packages:

library(devtools)

To install these packages you can use the code (or if you are compiling the document, remove the eval=FALSE from the chunk.)

install.packages("devtools")

Make the code pretty

Set the color scheme and pch.

## Color scheme inspired by the RSkittleBrewer package
## https://github.com/alyssafrazee/RSkittleBrewer
tropical=  c('darkorange', 'dodgerblue', 'hotpink', 'limegreen', 'yellow')
palette(tropical)
par(pch=19)

Unfortunately for R markdown documents the pch won’t propagate unless you do something a little more messy. We’ll hide this later, but for now this is what you need to do to set the default.

## see ch. 10 Hooks of Xie's knitr book
library(knitr)
knit_hooks$set(setPch = function(before, options, envir) {
  if(before) par(pch = 19)
})
opts_chunk$set(setPch = TRUE)

We will again hide this in future documents, but this shows how to set a default figure width and height, as well as setting plot margians.

knitr::opts_chunk$set(fig.width=5, fig.height=5, size="footnotesize",
                      warning=FALSE, message=FALSE)
knitr::knit_hooks$set(small.mar = function(before, options, envir) {
  if (before) graphics::par(mar = c(5,5,1.5,1))
})

Compiling documents

  1. Try compiling this document using the “Knit HTML” button. What files are produced?
  2. Edit the output to be “pdf_document” and recompile. What files are produced?
  3. Edit the output to be “word_document” and recompile. What files are produced?

Naming code chunks

The label “chunk1” tells you which part of the code was running in case you have errors. If you compile this document you’ll see it under the “R markdown” window on R studio.

x = rnorm(100)
plot(x,col=3)

Headers

This is a primary header.

This a secondary header

This is a tertiary header

Lists

You can create bulleted and numbered lists in the following way.

  • Bullet item 1
  • Bullet item 2
  • Bullet item 3
  1. Numbered item 1
  2. Numbered item 2
  3. Numbered item 3

Figures

The main arguments you might want to change are centering and figure height.

x = rnorm(100)
plot(x,col=3,pch=19)

Other chunk arguments

Add a chunk option of echo=FALSE to hide the code.

Setting cache=TRUE makes it so that the code won’t take as long to compile the second time. To see this, uncomment the code below and then run the document twice.

Sys.sleep(10)

Session information

It is very useful to record the session information when performing analyses so you can collaborate with yourself in the future.

sessionInfo()
## R version 3.2.1 (2015-06-18)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
## Running under: OS X 10.8.5 (Mountain Lion)
## 
## 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] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] knitr_1.11     devtools_1.8.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.0     digest_0.6.8    git2r_0.11.0    formatR_1.2    
##  [5] magrittr_1.5    evaluate_0.7.2  stringi_0.5-5   curl_0.9.2     
##  [9] xml2_0.1.2      rmarkdown_0.7   tools_3.2.1     stringr_1.0.0  
## [13] yaml_2.1.13     rversions_1.0.2 memoise_0.2.1   htmltools_0.2.6
devtools::session_info()
##  setting  value                       
##  version  R version 3.2.1 (2015-06-18)
##  system   x86_64, darwin10.8.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       America/New_York            
## 
##  package   * version date       source        
##  curl        0.9.2   2015-08-08 CRAN (R 3.2.1)
##  devtools  * 1.8.0   2015-05-09 CRAN (R 3.2.0)
##  digest      0.6.8   2014-12-31 CRAN (R 3.2.0)
##  evaluate    0.7.2   2015-08-13 CRAN (R 3.2.2)
##  formatR     1.2     2015-04-21 CRAN (R 3.2.0)
##  git2r       0.11.0  2015-08-12 CRAN (R 3.2.2)
##  htmltools   0.2.6   2014-09-08 CRAN (R 3.2.0)
##  knitr     * 1.11    2015-08-14 CRAN (R 3.2.2)
##  magrittr    1.5     2014-11-22 CRAN (R 3.2.0)
##  memoise     0.2.1   2014-04-22 CRAN (R 3.2.0)
##  Rcpp        0.12.0  2015-07-25 CRAN (R 3.2.1)
##  rmarkdown   0.7     2015-06-13 CRAN (R 3.2.1)
##  rversions   1.0.2   2015-07-13 CRAN (R 3.2.1)
##  stringi     0.5-5   2015-06-29 CRAN (R 3.2.1)
##  stringr     1.0.0   2015-04-30 CRAN (R 3.2.0)
##  xml2        0.1.2   2015-09-01 CRAN (R 3.2.2)
##  yaml        2.1.13  2014-06-12 CRAN (R 3.2.0)

It is also useful to compile the time the document was processed. This document was processed on: 2015-09-06.