Rmarkdown Quickstart

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

So with Markdown you can:

  • Create inline code-like highlighting by putting back-ticks around it
  • Create different headers by using the pound sign # header name
  • Create bold characters by putting double asterisks around it, e.g. **characters**, or one asterisk around it for italic characters. e.g. *characters*
  • Create blockquotes using the greater than symbol. e.g. > quote here
  • Create links. e.g. I’m an inline-style link
  • Create un-ordered bullet points by using - bullet, or use numbers to create ordered bullet points, 1. bullet
  • Write latex equations by putting dollar signs around it, e.g. typing $\mu$, will give you \(\mu\). Put two dollars sign for block equations. e.g. typing $$\mu$$

\[\mu\]

For a more comprehensive list, consider this link. Github: Markdown Here Cheatsheet

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

Next, you can embed an R code chunk. What you do is you initiate the code chunk with three backticks and a r within a curly bracket. After the code, there will be a line with just three backticks.

# we can generate code outputs
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
# or figures
plot(pressure)

Or more efficiently, there’s a insert button on the Rstudio panel above where you can simply click and it will generate the template for you.

You can give each code chunk a name, like cars above. The name is optional; if included, each code chunk needs a distinct name. One reason for doing this, say we have a .Rmd file and we only want the source code of that file. We can do this by using knitr::purl(.Rmd file path here), the purl function from the knitr package on this file, this will extract solely the source code and it will also tag it with the code chunk name if there is one. This makes it a bit easier to track for errors or make changes.

Notice that we’re using knitr::purl to call the purl function from the knitr package instead of loading the library knitr and calling the function like library(knitr) purl(.Rmd file here). The rationale is that: people might not be familiar with the purl function, by doing this, the others will know this is a function coming from the knitr package. And the other is, when loading a library, we’re loading every functions available from that package. If we’re just going to use 1 of them, consider using this method to avoid polluting the workspace.

# call knitr::purl(.Rmd file path here)
# on this .Rmd file and notice the difference
test <- 1
test
## [1] 1

A reproducible report should never manually write down numbers that are derived from the data. Instead we use inline code. e.g. To refer to the value of the test variable, instead of manually writing the test value is 1, we can write the test value is 1, created by wrapping a backtick around the r character, a space and the variable name. As shown below:

`r test`

Code Chunk Options

There are many parameters you can specify for the R code chunk. For example:

  • eval = FALSE Display the r code without evaluating it
  • echo = FALSE The code will not be shown in the final document, but the corresponding results/output would still be displayed
  • results = "hide" To hide the results/output, but the code would still be displayed
  • include = FALSE The code chunk will be evaluated, but neither the code nor its output displayed
  • For figures, you’ll want to use options like fig.width and fig.height to control the size, the best way is simply try and error (try different numbers and see which works best), or use fig.show = "hide" to hide figures. And there is fig.align='center' to center the plot (the default is left-aligned)
  • For figures, we can also specify dev = 'svg' to change the default graphics device from png to scalable vector graphics, which may look better on the web.
  • message = FALSE and warning = FALSE to suppress message and warning. Some packages will print these out when loading them, use these two options to remove them from the output file

For these options, there will be autocompletion when you start typing them in the r code chunk, which makes life easier for you.

# a plot using center, and 5 for both height and width
plot(pressure)

We can set the options globally, so it applies to all code chunk by:

# for all chode chunks all code will be displayed
knitr::opts_chunk$set(echo = TRUE)

We usually set this a the very top of the Rmarkdown file and set echo = FALSE so it doesn’t get shown. There are a bunch of other options, listed here. Blog: Chunk options and package options

Displaying Tables

One other cool thing is we can display data.frame objects in a table like manner. Consider the differences between the outputs below

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
knitr::kable( head(mtcars) )
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

We can pass the results='asis' options to the code chunk to ensure that the raw table output isn’t processed furthur by knitr. The kable function includes several options to control the maximum number of digits for numeric columns, alignment, check the help page, ?knitr::kable, for more details.

We can even create dynamic table if necessary using the datatable function from the DT package.

DT::datatable(mtcars)

Reproducible Report

Session Information

Include “session info” in your document, preferably at the bottom: this lists the version of R that you’re using, plus all of the packages you’ve loaded. So people that wishes to reproduce your work will know which version of R, packages you’re using and when you’re running the report.

# or use sessionInfo() that's included with R
# see which format do you prefer
devtools::session_info()
## Session info --------------------------------------------------------------
##  setting  value                       
##  version  R version 3.2.4 (2016-03-10)
##  system   x86_64, darwin13.4.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       America/Chicago             
##  date     2017-01-13
## Packages ------------------------------------------------------------------
##  package     * version date       source        
##  assertthat    0.1     2013-12-06 CRAN (R 3.2.0)
##  bookdown      0.1     2016-07-13 CRAN (R 3.2.5)
##  devtools      1.12.0  2016-06-24 CRAN (R 3.2.5)
##  digest        0.6.9   2016-01-08 CRAN (R 3.2.3)
##  DT            0.2     2016-08-09 CRAN (R 3.2.5)
##  evaluate      0.9     2016-04-29 cran (@0.9)   
##  formatR       1.4     2016-05-09 cran (@1.4)   
##  highr         0.6     2016-05-09 cran (@0.6)   
##  htmltools     0.3.5   2016-03-21 CRAN (R 3.2.4)
##  htmlwidgets   0.6     2016-02-25 CRAN (R 3.2.3)
##  httpuv        1.3.3   2015-08-04 CRAN (R 3.2.0)
##  jsonlite      1.0     2016-07-01 cran (@1.0)   
##  knitr         1.14    2016-08-13 CRAN (R 3.2.4)
##  magrittr      1.5     2014-11-22 CRAN (R 3.2.0)
##  memoise       1.0.0   2016-01-29 CRAN (R 3.2.3)
##  mime          0.4     2015-09-03 CRAN (R 3.2.0)
##  miniUI        0.1.1   2016-01-15 CRAN (R 3.2.3)
##  questionr     0.5     2016-03-15 CRAN (R 3.2.4)
##  R6            2.1.2   2016-01-26 CRAN (R 3.2.3)
##  Rcpp          0.12.5  2016-05-14 cran (@0.12.5)
##  rmarkdown     1.1     2016-10-16 CRAN (R 3.2.4)
##  rmdformats    0.3     2016-09-05 CRAN (R 3.2.5)
##  rstudioapi    0.6     2016-06-27 CRAN (R 3.2.5)
##  shiny         0.14.2  2016-11-01 CRAN (R 3.2.5)
##  stringi       1.0-1   2015-10-22 CRAN (R 3.2.0)
##  stringr       1.0.0   2015-04-30 CRAN (R 3.2.0)
##  tibble        1.2     2016-08-26 CRAN (R 3.2.5)
##  withr         1.0.2   2016-06-20 CRAN (R 3.2.5)
##  xtable        1.8-2   2016-02-05 CRAN (R 3.2.3)
##  yaml          2.1.13  2014-06-12 CRAN (R 3.2.0)

Avoid Absolute Path

Try to avoid using path like /Users/ethen/Desktop/ across your documents. If you must set the working directory, set it at the beginning of the report, and consider using file.path so they are independent of the system that you’re working on (Windows, Mac, Linux). Or simply use normalizePath.

file_path <- file.path('/Users', 'ethen', 'Desktop')

# alternative
file_path <- normalizePath('/Users/ethen/Desktop')
setwd(file_path)

Bonus: Rmarkdown Themes

People have created different themes for Rmarkdown. e.g. Github: prettydoc. We can change the theme by adding extra information in the header. For example, we can change the header info into:

---
title: "Week 1"
author: "Ethen Liu"
date: "2017-01-13"
output:
  prettydoc::html_pretty:
    theme: leonids
    highlight: github
---

For the prettydoc package, the theme option can take the following values cayman, tactile, architect, leonids, hpstr. The code highlight option takes value of github and vignette. More may be added in the future.

Ethen Liu

2017-01-13