Integrating R, knitr, and LaTeX via RStudio

11,025 views

Published on

Brief presentation to faculty on integrating R and knitr in LaTeX for dynamic document/manuscript generation.

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
11,025
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
200
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Integrating R, knitr, and LaTeX via RStudio

  1. 1. Department of!Psychology!University of Mary Hardin-Baylor!Integrating R, knitr,and LaTeX viaRStudio!!Aaron R. Baggett!February15, 2013!
  2. 2. Introduction!•  R is a free, open-source statistical and data visualizationsoftware application•  Open source = large community of developers•  Increasing in popularity(Muenchen, 2012)Aaron R. Baggett | 02/15/2013! 2!R!
  3. 3. Introduction!•  Programming language•  Can interface with many other programs•  Object oriented languagea <- 15b <- 15a + b[1] 30 Aaron R. Baggett | 02/15/2013! 3!R!
  4. 4. > 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 gt90k 1 Not too happy 17.2 13.0 7.7 5.3 2 Pretty happy 60.5 56.8 50.3 51.8 3 Very happy 22.2 30.2 41.9 42.9 > happy$Response <- NULL > happy lt20k lt49k lt89k gt90k 1 17.2 13.0 7.7 5.3 2 60.5 56.8 50.3 51.8 3 22.2 30.2 41.9 42.9 > > (test <- chisq.test(happy)) Pearsons Chi-squared test data: happy X-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
  5. 5. Introduction!•  Flexible and versatile•  4,336 user contributed packageso  Statistics and data analysiso  Psychometricso  Graphicso  Economics and finance Aaron R. Baggett | 02/15/2013! 5!R!
  6. 6. Introduction!•  Free graphical user interface (GUI) for Ro  http://www.rstudio.com/o  https://twitter.com/rstudioapp•  Still requires use of syntaxo  e.g., reg.fit <- lm(y ~ x1 + x2 + x3, data = df) •  Not limited by point and click interfaceAaron R. Baggett | 02/15/2013! 6!RStudio!
  7. 7. Introduction!•  Consolidated work environmento  Four window panes1.  R Console2.  Script3.  Workspace4.  Utility tabsa.  Filesb.  Plotsc.  Packagesd.  R HelpAaron R. Baggett | 02/15/2013! 7!Advantages of RStudio!
  8. 8. Introduction!•  Templates for document preparationo  knitr: http://yihui.name/knitr/o  Sweave: http://goo.gl/gMkqoo  R Markdown: http://goo.gl/YHBB1•  LaTeX integrationo  knitr works best for meAaron R. Baggett | 02/15/2013! 9!Advantages of RStudio!
  9. 9. Document Preparation!•  Pronounced “Lah-Tek”•  Document preparation system•  Sections, cross-references, bibliographies•  Equation/math typesetting•  Stability•  Like R, large, active communityAaron R. Baggett | 02/15/2013! 10!LaTeX http://www.latex-project.org/
  10. 10. 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!
  11. 11. Brief ArticleThe AuthorFebruary 15, 20131
  12. 12. Document Preparation!•  Comprehensive package for generating “Elegant,flexible and fast, dynamic report generation with R.”•  Easily handles embedding R code directly in LaTeXdocuments•  Tutorial video from knitr developers:http://www.screenr.com/qcv8Aaron R. Baggett | 02/15/2013! 13!knitr!
  13. 13. 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 names head(diamonds) #Returns first six rows of dataset with(diamonds, summary(carat)) #Summarize carat variable with(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’) @
  14. 14. 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 Variancen(Extroversion by Sex)”, col = "dodgerblue3", ylab = "Extroversion") @ newpage setlengthparindent{0pt}textbf{large{5). Test to see if there is amean difference in Extroversion between males and females (make sureyou test the assumption of homogeneity of variance and set up your t-test appropriately)}} vspace*{-.55in}
  15. 15. knitr!Aaron R. Baggett | 02/15/2013! 18!knitr efficiency!
  16. 16. Conclusion!•  Efficient document markup•  Dynamic and attractive reports•  Stability•  Open source•  FreeAaron R. Baggett | 02/15/2013! 19!Benefits!
  17. 17. Aaron Baggett!abaggett@umhb.eduAaron R. Baggett | 02/15/2013! 20!Questions?!

×