Long and Short of R in an Hour

Tasneem Zaihra
Monday, Feb 27, 2017

Introduction to R

  • R is a language and environment for statistical computing and graphics. It includes

    • an effective data handling and storage facility,
    • a suite of operators for calculations on arrays, in particular matrices,
    • a large collection of intermediate tools for data analysis,
    • graphical facilities for data analysis and display either on-screen or on hardcopy,
    • a well-developed programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.

Introduction to RStudio

  • RStudio is an integrated development environment (IDE) for R.
  • It includes a console, syntax-highlighting editor that supports direct code execution, as well as tools for plotting, history, debugging and workspace management.

Creating Dynamic Reports

  • R includes a powerful and flexible system (Sweave) for creating dynamic reports and reproducible research using LaTeX.

  • Sweave enables the embedding of R code within LaTeX documents to generate a PDF file that includes narrative and analysis, graphics, code, and the results of computations.

For more details on authoring R presentations please visit https://support.rstudio.com/hc/en-us/articles/200486468.

Package knitr

  • knitr is an R package that adds many new capabilities to Sweave and is also fully supported by RStudio.

-You can use knitr rather than classic Sweave for weaving Rnw files.

  • To use Sweave and knitr to create PDF reports, you will need to have LaTeX installed on your system. LaTeX can be installed following the directions on the LaTeX project page.

Difference between Markdown and Rnw

  • Rnw stands for R NoWeb files. They have to be processed by Sweave i.e. code chunks are processed with R and results inluded into a LaTeX file that itself has to be processed by LaTeX oder PDFLaTeX.

  • Workflow R Markdown is a format for writing reproducible, dynamic reports with R. Use it to embed R code and results into slideshows, pdfs, html documents.

  • They use different syntax to wrap up R code in the input document, and the results returned from R are marked up according to the syntax of the output document.

Why would you want to use one versus the other?

  • You'll use substantially the same structure to write a document using markdown or \( \LaTeX \), that is, with a .Rmd or .Rnw file type.t

    • \( \LaTeX \) compiles nicely to PDF, Rmd to HTML.
    • Rmd is easier to write and compile, but offers less control over formatting.
    • If you already know \( \LaTeX \), Rnw isn't really any harder.
  • Cheatsheet link. https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf

Sample Slide with R Code

     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  

Sample Slide With Plot

plot of chunk unnamed-chunk-2