Presentation is loading. Please wait.

Presentation is loading. Please wait.

Brief Intro to Knitr Package

Similar presentations


Presentation on theme: "Brief Intro to Knitr Package"— Presentation transcript:

1 Brief Intro to Knitr Package
What does Knitr do R markdown file More control over the output document - Chunk options - Global options - Tables in knitr - Knit vs. spin

2 What does Knitr do Allows users to conduct reproducible research in R using Literate Programming Versatility: any language as input(Python, Ruby, SAS, Scala…) any markup language as output(Latex, Markdown, HTML…) R Markdown Create a R Markdown file with mixture of texts and codes Process the R Markdown file through Knitr final deliverables in form of Slides, PDF, HTML, etc. with evaluated R code output written inside No more manually saving graphs as PNGs and pasting them to Doc file Useful for reproducible reporting knowledge of Latex or HTML is not necessary to make your reports look nice

3 R markdown file mixture of texts and codes exist in form of chunks
A R Markdown file example some markdown syntax R codes exist in R Markdown files in form of code chunk or inline code Inline code: The creation date of this report is `r Sys.Date()` Code chunk: ```{r simulate_data} x <- rnorm(100) y <- 2*x + rnorm(100) ``` Render your .Rmd file

4 What would you like in your sandwich
Options Chunk options Echo = FALSE results=“hide” fig.width=8 fig.height=6 eval=TRUE cache=FALSE Global options ```{r global_options, include=FALSE} knitr::opts_chunk$set(fig.width=12, fig.height=8, echo=FALSE, warning=FALSE, message=FALSE) ```

5 What would you like in your sandwich
Mathematical Notations Equations can be formatted as inline notation or displayed formulas inline equation : part of text wrap by single $ $\sum_{i=1}^n X_i$ displayed formulas: displayed in a separate paragraph wrap by a pair of $ $$\sum_{i=1}^n X_i$$ LATEX typesetting language can be directly inserted in R Markdown file to generate mathematical equation and notations Some examples

6 What would you like in your sandwich
Tables in knitr kable function in the knitr package pandoc.table function in the pander package xtable function in the xtable package

7 If you like what knit does for you ,
you might also want to checkout Spin Generate draft .Rmd for you directly from R file Using knit, your workflow might be: With spin, your workflow looks like this: Have your analysis ready in a R file Create a Rmd file copy and paste codes from R file into chunks in .Rmd Render to knit Have your analysis ready in a R file Render to spin and get a draft .Rmd file Add some markdown text and identify potential chunks in R file Only need to add #+ before code to make it a chunk(instead of  ```{r} before the chunk and ``` after) Still work as a normal R file so it’s easier to run codes cross chunks or source it

8 Reference R Markdown Cheat sheet knitr in a knutshell
Knitr documentaion by the author Yihui Xie


Download ppt "Brief Intro to Knitr Package"

Similar presentations


Ads by Google