Integrating R, knitr, and LaTeX via RStudio
-
Upload
aaron-baggett -
Category
Technology
-
view
5.971 -
download
2
description
Transcript of Integrating R, knitr, and LaTeX via RStudio
Department of!Psychology!
University of Mary Hardin-Baylor!
Integrating R, knitr, and LaTeX via
RStudio!!
Aaron R. Baggett!February15, 2013!
Introduction!
• R is a free, open-source statistical and data visualization software application
• Open source = large community of developers • Increasing in popularity
(Muenchen, 2012)
Aaron R. Baggett | 02/15/2013! 2!
R!
Introduction!
• Programming language • Can interface with many other programs • Object oriented language
a <- 15 b <- 15 a + b [1] 30
Aaron R. Baggett | 02/15/2013! 3!
R!
> Response <- c("Not too happy", "Pretty happy", "Very happy")> lt20k <- c(17.2, 60.5, 22.2)> lt49k <- c(13, 56.8, 30.2)> lt89k <- c(7.7, 50.3, 41.9)> gt90k <- c(5.3, 51.8, 42.9)> > (happy <- data.frame(Response, lt20k, lt49k, lt89k, gt90k)) Response lt20k lt49k lt89k gt90k1 Not too happy 17.2 13.0 7.7 5.32 Pretty happy 60.5 56.8 50.3 51.83 Very happy 22.2 30.2 41.9 42.9> happy$Response <- NULL> happy lt20k lt49k lt89k gt90k1 17.2 13.0 7.7 5.32 60.5 56.8 50.3 51.83 22.2 30.2 41.9 42.9> > (test <- chisq.test(happy))
Pearson's Chi-squared testdata: happyX-squared = 17.73, df = 6, p-value = 0.00694> test$expected lt20k lt49k lt89k gt90k[1,] 10.79 10.81 10.79 10.81[2,] 54.82 54.88 54.82 54.88[3,] 34.28 34.32 34.28 34.32
Introduction!
• Flexible and versatile • 4,336 user contributed packages
o Statistics and data analysis o Psychometrics o Graphics o Economics and finance
Aaron R. Baggett | 02/15/2013! 5!
R!
Introduction!
• Free graphical user interface (GUI) for R o http://www.rstudio.com/
o https://twitter.com/rstudioapp
• Still requires use of syntax o e.g., reg.fit <- lm(y ~ x1 + x2 + x3, data = df)
• Not limited by point and click interface
Aaron R. Baggett | 02/15/2013! 6!
RStudio!
Introduction!
• Consolidated work environment o Four window panes
1. R Console
2. Script
3. Workspace 4. Utility tabs
a. Files
b. Plots
c. Packages
d. R Help
Aaron R. Baggett | 02/15/2013! 7!
Advantages of RStudio!
Introduction!
• Templates for document preparation o knitr: http://yihui.name/knitr/ o Sweave: http://goo.gl/gMkqo o R Markdown: http://goo.gl/YHBB1
• LaTeX integration o knitr works best for me
Aaron R. Baggett | 02/15/2013! 9!
Advantages of RStudio!
Document Preparation!
• Pronounced “Lah-Tek” • Document preparation system • Sections, cross-references, bibliographies • Equation/math typesetting • Stability • Like R, large, active community
Aaron R. Baggett | 02/15/2013! 10!
LaTeX http://www.latex-project.org/
LaTeX!
\documentclass[11pt, oneside]{article}\usepackage{geometry}\geometry{letterpaper}\usepackage[parfill]{parskip}\usepackage{graphicx}\title{Brief Article}\author{The Author}\date{\today}\begin{document}\maketitle%\section{}%\subsection{}\end{document}
Aaron R. Baggett | 02/15/2013! 11!
Standard Preamble!
Brief Article
The Author February 15, 2013
1
Document Preparation!
• Comprehensive package for generating “Elegant, flexible and fast, dynamic report generation with R.”
• Easily handles embedding R code directly in LaTeX documents
• Tutorial video from knitr developers: http://www.screenr.com/qcv8
Aaron R. Baggett | 02/15/2013! 13!
knitr!
knitr!
• All inline R code begins with: <<>>=• And ends with: @
Aaron R. Baggett | 02/15/2013! 15!
knitr Code!
<<tidy = TRUE>>=library(ggplot2)names(diamonds) #Returns variable nameshead(diamonds) #Returns first six rows of datasetwith(diamonds, summary(carat)) #Summarize 'carat' variablewith(diamonds, cor(carat, price)) #Correlate 'carat' and 'price’. @
<<fig.width = 6, fig.height = 6, tidy = FALSE>>=qplot(carat, data = diamonds, fill = color, geom = ‘histogram’,
binwidth = 0.4, xlim = c(0,3),main = ‘Stacked Histogram of Diamond Carat Weight by Color’,xlab = ‘Carat Weight’, ylab = ‘Count’)
@
knitr!
Aaron R. Baggett | 02/15/2013! 16!
knitr Code!
<<box, fig.width=5.5, fig.height=5.5, fig.align='center', tidy=FALSE>>=plot(Sex, Extrov, main =
"Box-Whisker Plot of Variance\n(Extroversion by Sex)”,col = "dodgerblue3", ylab = "Extroversion")
@
\newpage\setlength\parindent{0pt}\textbf{\large{5). Test to see if there is a mean difference in Extroversion between males and females (make sure you test the assumption of homogeneity of variance and set up your t-test appropriately)}}
\vspace*{-.55in}
knitr!
Aaron R. Baggett | 02/15/2013! 18!
knitr efficiency!
Conclusion!
• Efficient document markup • Dynamic and attractive reports • Stability • Open source • Free
Aaron R. Baggett | 02/15/2013! 19!
Benefits!