2015-12-15

Creative CC license terms

Preface

What?

R Markdown is an authoring format that enables easy creation of dynamic documents, presentations, and reports from R. It combines the core syntax of markdown (an easy-to-write plain text format) with embedded R code chunks that are run so their output can be included in the final document. R Markdown documents are fully reproducible (they can be automatically regenerated whenever underlying R code or data changes).

logo

It happened everyday

  1. Data preparation
  2. Modeling
  3. Generating report
  4. Something wrong in your data
  5. Repeat 1 ~ 4

logo

Why?

  1. Automate
    • Periodic documents
    • Adapted documents
    • Faster to implement
  2. Reproducible
    • Less errors
  3. Diferent formats
    • Html
    • Pdf
    • Presentations

Research Pipeline

how to start?

Installing R Markdown

Overview

  • YAML Metadata: YAML document options
  • Markdown: Article text
  • R Code Chunk: Executible R code

research pipeline

Rendering Output

  • RStudio: "Knit" command (Ctrl+Shift+K)
  • Command line: rmarkdown::render function
rmarkdown::render("input.Rmd")

Create a HTML Document

output_option output_option

Markdown Basics

Markdown Quick Reference

md_quick_ref

Emphasis

*italic*   **bold**

_italic_   __bold__

I am italic

I am bold

Headers

Setext:

Header 1
=============

Header 2
-------------

atx:

# Header 1

## Header 2

### Header 3

Lists

Unordered List:

* Item 1
* Item 2
    + Item 2a
    - Item 2b

Ordered List:

1. Item 1
2. Item 2
3. Item 3
    + Item 3a
    + Item 3b




Unordered List:

  • Item 1
  • Iteivm 2
    • Item 2a
    • Item 2b

Ordered List:

  1. Item 1
  2. Item 2
  3. Item 3
    • Item 3a
    • Item 3b

Links & Images

Plain Code Blocks

  • Plain code blocks are displayed in a fixed-width font but not evaulated.

    ```
    This text is displayed verbatim / preformatted
    ```
    
  • Specify the language of the block is avaliable

    ```r
    x = rnorm(10)
    ```
    x = rnorm(10)

Embedding Equations

Tables

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell


First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

Text Color

You can color content using base color classes red, blue, green, yellow, and gray (or variations of them e.g. red2, red3, blue2, blue3, etc.).

<div class="red2">
This text is red
</div>
  • red red2 red3
  • blue blue2 blue3
  • green green2 green3
  • yellow yellow2 yellow3
  • gray gray2 gray3

R Code Chunks

Overview

R code will be evaluated and printed

```{r}
summary(cars$dist)
```
summary(cars$dist)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.00   26.00   36.00   42.98   56.00  120.00

Named R code chunk.

```{r plot}
summary(cars)
plot(cars)
```
  • Easy Navigation in RStudio

Basic Chunk Options

  • echo(TRUE): whether to include R source code in the output file
  • eval(TRUE): whether to evaluate the code chunk
  • message(TRUE): whether to preserve messages emitted by message()
  • include(TRUE): if include=FALSE, nothing will be written into the output document, but the code is still evaluated and plot files are generated
  • warning(TRUE): whether to preserve warnings in the output

Set global chunk options:

knitr::opts_chunk$set()

Plots

  • dev('png'): figure format(png, jpeg, tiff, svg, …)
  • fig.path('figure/'): figure path
  • fig.width(7): figure width
  • fig.height(7): figure height
  • dpi(72): DPI (dots per inch)
```{r dev='svg', fig.path='myplot',fig.width=5, fig.height=5}
plot(iris)
```

Inline R Code

I counted 'r 1+1' red trucks on the highway.


I counted 2 red trucks on the highway.

Other Cool Stuff

ggvis code

library(knitr)
library(ggvis)
mtcars %>%
    ggvis(x = ~wt, y = ~mpg) %>%
    layer_smooths(se=TRUE, opacity := 0.5, opacity.hover := 0.75) %>% 
    layer_points(fill = ~factor(cyl), size := 50, size.hover := 200) %>%
    set_options(hover_duration = 250)

DataTable code

library(DT)
datatable(mtcars,options=list(pageLength=3))

References