This is an R Markdown document, which present the basis elements to work with the knitr package in the goal to create report. Here, we presente a collection of tricks to work with knitR in R studio.

Why use knitR?

Rmarkdown language.

Rmarkdown commands.

alt text

Create a new Rmarkdown document.

alt text

Include a R code in Rmarkdown document: chunk

Chunk options.

Many options are available to control the R code execution

alt text

Three ways to include a table.

with ktable.

# create a data.frame structure.
library(knitr)
mydata = data.frame(value1= 1:4, value2= 6:9)
rownames(mydata) <- paste("gene", 1:4)

kable(mydata, include.rownames=TRUE)
value1 value2
gene 1 1 6
gene 2 2 7
gene 3 3 8
gene 4 4 9

with googlevis.

library(googleVis)
## 
## Welcome to googleVis version 0.5.8
## 
## Please read the Google API Terms of Use
## before you start using the package:
## https://developers.google.com/terms/
## 
## Note, the plot method of googleVis will by default use
## the standard browser to display its output.
## 
## See the googleVis package vignettes for more details,
## or visit http://github.com/mages/googleVis.
## 
## To suppress this message use:
## suppressPackageStartupMessages(library(googleVis))
mydata = data.frame(value1= 1:100, value2= 6:105)
rownames(mydata) <- paste("gene", 1:100)

table = gvisTable(mydata,options=list(page='enable', height='automatic', width='automatic'))
table

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”> TableID2c2966e993aa

<!– Table generated in R 3.1.1 by googleVis 0.5.8 package –>

Data: mydata • Chart ID: TableID2c2966e993aagoogleVis-0.5.8

R version 3.1.1 (2014-07-10) • Google Terms of UseDocumentation and Data Policy

With DT package.

library(DT)

table = datatable(mydata, class = 'cell-border stripe')
table

<!–html_preserve–>

<!–/html_preserve–>

Include a plot in the report.

2 strategies to do that:

from a R code directly

Here, we precise no option for the R chunk {r}

mydata =rnorm(1000)
plot(mydata, cex= 0.4)

plot of chunk unnamed-chunk-4

In this case, we precise several options in the features of the plot : {r, fig.height= 14, fig.width= 14, fig.align='center'}

mydata =rnorm(1000)
plot(mydata, cex= 0.4, col= "red")

plot of chunk unnamed-chunk-5

from a file

R code to generate and save the plot

png("figure/plot2insert.png")
plot(mydata, cex= 0.4, col= "blue")
dev.off()
## png 
##   2

Include the plot with the markdown syntax.
alt text

Strategies with big Data or big Computation.

In the case, where you have a large dataset to analyze, it's important to review the way to process the data: think to process your computation on the side and save the result as a R object, which will be loaded in R chunks. Ideally, you create a R chunk with eval=F option to perform the computation: this R chunk is a component of your pipeline and is saved and available in the report ( its goal is just to generate the R objects to be visualized in the report without to be evaluated). It's a good habit to execute this part of code in Unix terminal and not in R studio server.For exemple, a function like mclapply() has a weird behaviour and batchjob usage doesn't work.

Conclusions.

The next time, to go further.

SessionInfo

sessionInfo()
## R version 3.1.1 (2014-07-10)
## Platform: x86_64-redhat-linux-gnu (64-bit)
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] DT_0.1          googleVis_0.5.8 knitr_1.10.5   
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.8     evaluate_0.7     formatR_1.2      highr_0.5       
##  [5] htmltools_0.2.6  htmlwidgets_0.4  jsonlite_0.9.16  magrittr_1.5    
##  [9] RJSONIO_1.3-0    rstudioapi_0.3.1 stringi_0.4-1    stringr_1.0.0   
## [13] tools_3.1.1      yaml_2.1.13