# Installing R

R is open-source and free for all. There are multiple ways to install R

# Report generation using rmarkdown


## Code to install knitr package

install.packages('knitr')


• To generate PDF; need to install these softwares
• Windows: MiKTeX (Complete) - MikTex Window Setup (NOTE: Be sure to download the Complete rather than Basic installation)
• Mac OS X: TexLive 2013 (Full) - TexLive Mac Setup (NOTE: Download with Safari rather than Chrome strongly recommended)

RStudio Authoring R Presentation

It is time to test your report generation capability.

Open a new Rmd file in RStudio by selecting the R Markdown ... option.

On the new window, give a proper title for your file, and put your name as the author. Select pdf option

To “knit” the script into html document, click the knit HTML button

You should see a “Knitted” html file. That is how generate a “Reproducible Research” document – easy, isn’t it?

Congratulation! You have just created your first Reproducible Research Report

Now do the same to generate a PDF report

Hint: it should not work; becuase you need to install
MikTex for Window
TextLive for Mac

# Learning the Elements of a Report

## Completely Delete File Content

Now delete everything from the Rmarkdown file and copy the content of RMarkDown into the new file

Note:

## Layout: landscape or portrait

Try to uncomment classoption:landscape and render the HTML file again.

Knit the HTML or PDF in landscape mode

## Run in landscape mode
classoption: landscape

Knit the HTML or PDF in portrait mode

## Run in portrait mode
# classoption: landscape


Note:
HTML has no concept of landscape or portrait
The exercise was meant for generating PDF document.

## Layout: bigger font size

Try to change the fontsize and render the HTML or PDF file with different font size

Knit the PDF in 14 point font size

## Use font size 14 point
fontsize: 14pt


## Layout: table of content

knitr creates table of content automatically based on the Heading hierarchy of the document. We can also define the depth of the Heading hierarchy to be displayed in the table of content


## Define 3 layer of depth
output:
pdf_document:
toc: true
toc_depth: 3


## this is my first code block
First layer
===========
Second layer
------------
### Third layer
#### Fourth layer


# To Evalute or Not To Evaluate

knitr code-chunk consists of different attribute to control the behavior of the codes being evaluated. The eval attribute determines wether the codes within the code-chunk is being evalated.

## To Evaluate


## To evaluate
# {r, eval=TRUE}
# cat('Hello, I have been evaluaten\n')
# 


## Not To Evaluate


## To evaluate
# {r, eval=FALSE}
# cat('Hello, I have been evaluaten\n')
# 


# To Show the code or Not to Show the code

The analyst can decide if the code within theh code-chunk is being displayed. Sometimes, the codes might be too long and extensive, or the reader might not be interested in the code details. The attribute echo determines if the ccode is being displayed on the final report.

## Let’s show the code


# to show the code
#{r, eval=TRUE, echo=TRUE}
#cat('Hello, I have been evaluated and displayed\n')
#


## Let’s not show the code; since we are doing nothing


# this is my first code block
#{r, eval=TRUE, echo=FALSE}
#cat('Nothing will happen anyway\n')
#


# To Show Resuts or Not to Show Results

Some function displays extensive and lengthy result after completing a task. Sometimes these results are not important for the context of the reader. The attribute results determine if the analysis results are being displayed on the final report.

## Let’s show the results


## To show the results
#{r, eval=TRUE, echo=TRUE, results='markup'}
#1+1
#


## Let’s not show the results


## Not to show the results
#{r, eval=TRUE, echo=TRUE, results='hide'}
#2+2
#


# To Cache nor Not to Cache

RStudio is capable is able to store the analysis results into cache so that the code-chunk does not need to be executed again. This is very helpful if a particular code-chunk requires a long time to run. In general, it is a good practice to cache all the code-chunk to speed up the report generation process. The attribute cache determines if the analysis result of a code-chunk is store in memory to be recall on subsequent runs.

Extra: name the Code Chunk

Naming the code chunk enable one to locate it quickly in RStudio

## Let’s Not Cache


## this is my first code block
# {r NotCache, eval=TRUE, echo=TRUE, results='hide', cache=FALSE}
# ptm.uncache = proc.time()
# for(i in 1:1000){
#   x = rnorm(1000)
#   y = rnorm(100000)
#   z = t.test(x,y)
# }
# time.uncache = proc.time() - ptm.uncache
# 


Extra: Inline code

Computation can be performed outside of the code chunk area


## Compute inline code
# The un-cache code took 'r time.uncache[3]/60' minutes


## Let’s Cache


## this is my first code block
# {r LetCache, eval=TRUE, echo=TRUE, results='hide', cache=TRUE}
# ptm.cache = proc.time()
# for(i in 1:1000){
#   x = rnorm(1000)
#   y = rnorm(100000)
#   z = t.test(x,y)
# }
# time.cache = proc.time() - ptm.cache
# 


# MarkDown Document Resources

Markdown format allows user easily convert human readable document to HTML format to be serve on a website. Here are some very useful resources to learn more about Markdown.

# Next class: learn R

For the next class, we will start reviewing R by starting with R basic

## Try R

Try R is a interactive web environment to learn the basic of R programming. It shows all the basic elements of R; Syntax, Data Structure and etc. You are highly encourage to complete this mini course

## DataCamp R

DataCamp for Beginner R is another interactive environment to learn R.

# Session Information

It is always a good idea to show session information at the end. These information not only show your R version, they also show the R packages versions; which make others help debugging your code much much easier.

sessionInfo()
## R version 3.2.1 (2015-06-18)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.10.3 (Yosemite)
##
## 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
##
## loaded via a namespace (and not attached):
##  [1] magrittr_1.5    formatR_1.2     tools_3.2.1     htmltools_0.2.6
##  [5] yaml_2.1.13     stringi_0.4-1   rmarkdown_0.6.1 knitr_1.10
##  [9] stringr_1.0.0   digest_0.6.8    evaluate_0.7`