Read Lab Dynamics of Complex Biochemical Systems elread @uci.edu
[email protected] Department of Computer Science University of …dock/upload/R/sweave.pdf · 2009....
Transcript of [email protected] Department of Computer Science University of …dock/upload/R/sweave.pdf · 2009....
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.1
Seminar 2
Sweave
Presented May 6, 2009Lab Meeting
Michael [email protected]
Department of Computer ScienceUniversity of California, Irvine
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.2
Motivation
What is Sweave?
I In my own words, Sweave is a way to include R code(directly or indirectly) within a LATEX document.
I Sweave was written by Friedrich Leisch, and is included as apart of R.
I This lecture will demo some of the things you can do usingSweave, and provide a template to work off of.
Flow
1. Write R code within your LATEX source, use extension .Rnw.
2. Compile your .Rnw using R CMD Sweave to generate a .tex.
3. Compile your .tex using pdflatex.
Example
> help("Sweave", package = "utils")
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.3
Motivation
Why use Sweave?
I Literate programming
I Reproducible Research
I Generating figures
I Generating tables
I Demoing R
I Generating reports
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.4
Outline of talk
Outline
Noweb
I Sexpr
I Sweave options
I Reusing code blocks
FiguresTablesExtras
I cacheSweave
I ESS
I Ryacas
I Networks
I Untangle
My scripts
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.5
Noweb
What is Noweb?
A way to include code (not just R) inside LATEX:
\def\dsum{\displaystyle\sum}Let's compute $\dsum_{i=1}^{10}{i}$ in {\R}:<<example>>=sum(1:10)@
Example
Let’s compute10∑i=1
i in R:
> sum(1:10)
[1] 55
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.6
Noweb
Sexpr
Pronounced “S expression” not “Sex PR”. These allow you toinclude smaller bits of R code into LATEX.
$\dsum_{i=1}^{10}{i} = \Sexpr{sum(1:10)}$
Example10∑i=1
i = 55
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.7
Noweb
Sweave options
There are a number of options that you can use in a chunk.The most important are:
echo print the R code as if it had been run in a sessionresults either hide, verbatim, or tex to hide all printed
output, or print as LATEXlabel the name of the code chunk
strip.white if set to FALSE, newlines will be preservedterm similar to echo, but does not print the R code,
just the outputeval a toggle to turn off the evaluation of a code chunk
fig if TRUE, will generate a LATEX figure
Example
<<label=example, echo=F, fig=T>>=
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.8
Noweb
Code chunk reuse
You can reuse a named code chunk in later code chunks, byreferencing the name.
Example
<<later, echo=F, eval=F>>=cat("Michael Zeller (c) 2009")@<<example, echo=F>>=<<later>><<later>>@
Michael Zeller (c) 2009
Michael Zeller (c) 2009
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.9
Figures
Example
\setkeys{Gin}{width=2in}<<setup, echo=F>>=n <- 20; data <- rnorm(20);@<<fig2cplot,echo=F,include=F>>=plot(data, xlab='Dim 1', ylab='Dim 2')@\begin{figure}\begin{center}<<fig2c,fig=T, echo=F>>=<<fig2cplot>>@\end{center}
\caption{Random sample ($n=\Sexpr{n}$) of our data}\label{fig:one} \end{figure}
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.10
Figures
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
5 10 15 20
−1.
5−
1.0
−0.
50.
00.
51.
01.
5
Dim 1
Dim
2
Figure: Random sample (n = 20) of our data
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.11
Tables
Generating tables
To generate tables, you can use the xtable package. It is easy toread in a file of data in R and to output it as a LATEX table.
timemethod1 20.9method2 50.9method3 19.2
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.12
Tables
Example
<<table, results=tex, echo=F>>=library(xtable)data <- read.table('examples/test.txt')xtable(data, caption='Benchmarks', label="tab2",
digits=c(0, 2), table.placement="htb")@
timemethod1 20.90method2 50.90method3 19.20
Table: Benchmarks
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.13
cacheSweave
What is cacheSweave?
cacheSweave is a package for R, or more specifically, it is a driverfor Sweave. It allows you to cache results from computationallyintensive R code within your Sweave documents.
I You must label all code blocks
I No output will be displayed for cached blocks
Example
<<cache, cache=T>>=c <- kmeans(c(rnorm(100000),
rnorm(100000, mean=2)),centers=2)$centers
@
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.14
ESS
Why use ESS?
Emacs Speaks Statistics is a package for emacs that provides aRnw-mode as well as the ability to run an interactive R sessionremotely or locally from within emacs.
Features
I Within Rnw-mode, all Noweb chunks are editted using R asthe minor-mode. Meaning that you can tab to format yourcode.
I You get to keep all of your code in one place, but uselatex-mode to edit LATEX and R-mode to edit R.
I You can test your code using a remote or local R sessionwhile creating your Sweave document.
Demo
Quick demo
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.15
ESS
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.16
Ryacas
Ryacas example
Ryacas is a package for R that is a wrapper for the open sourcecomputer algebra system, Yet Another Computer Algebra System.
Example
<<math, results=hide, echo=F>>=library(Ryacas)eq <- yacas(TeXForm((Sym('x')+1)^2 + Sym('k')^3), retclass = 'unquote')
x <- List(Sym('x1'),Sym('x2'))pc1 <- 0.8; pc2 <- 0.2mu1 <- List(1,1); mu2 <- List(4,4)icov <- Inverse(List(List(1,1),List(1,4)))ret <- yacas(TeXForm(icov*(mu1-mu2)*x-
1/2*(mu1*icov*mu1-mu2*icov*mu2)))@<<math, results=tex, echo=F>>=print(ret)@<<math, results=tex, echo=F>>=print(eq)@
”(x + 1)2 + k3”;”(−3x1 + 7.5, 0)”;
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.17
Networks
Networks
The network package is written by UCI’s very own, Carter Butts.It can be used (as an example) to demo the much more advancedplots that you can make using R, that are very hard to do usingLATEX and PSTricks.
<<setup, results=hide, echo=F>>=library(network)@<<network, fig=T, echo=F>>=#Construct a sparse graphm<-matrix(rbinom(100,1,1.5/9),10)diag(m)<-0g<-network(m)
#Plot the graphplot(g)@
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.18
Networks
Example
Figure: A simple network
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.19
Untangle
Stangle
Use R CMD Stangle to Untangle a Sweave file into a single .Rfile.
Example Output
###################################################### chunk number 1: example eval=FALSE##################################################### help("Sweave", package="utils");
###################################################### chunk number 2: example###################################################sum(1:10)
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.20
My Scripts
What are my scripts?
I wrote a Makefile and a template to make using Sweave easier onmyself.
Makefile
#!/bin/bash
SOURCE := sweave
SUFFIX := .Rnw
all: tex
tex:
cd $(SOURCE); R CMD Stangle $(SOURCE)$(SUFFIX); cd ..;
cd $(SOURCE); sed -e "s#@F@I@L@E@#$(SOURCE)$(SUFFIX)#" ../scripts/makepdf.r > /tmp/makepdf.tmp; cd ..;
cd $(SOURCE); R --file=/tmp/makepdf.tmp --vanilla; cd ..;
cd $(SOURCE); pdflatex $(SOURCE); cd ..;
clean:
ls $(SOURCE).* | grep -v $(SUFFIX) | xargs echo
makepdf.r
library(cacheSweave)
Sweave("@F@I@L@E@", driver=cacheSweaveDriver)
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.21
References
References
1. http://www.stat.umn.edu/~charlie/Sweave/
2. http://www.statistik.lmu.de/~leisch/Sweave/
3. https://sites.google.com/site/ucirseminar/
4. http://en.wikipedia.org/wiki/Noweb
5. http://en.wikipedia.org/wiki/Literate_programming
6. http://en.wikipedia.org/wiki/Reproducibility
7. http://biosun1.harvard.edu/courses/individual/bio271/lectures/L7/Sweave-manual-20021007.pdf
8. http://addictedtor.free.fr/graphiques/
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.22
Future talks
UCI R Seminar
Rscript: Shell scripting with R
snow & snowball: Parallel Computing in R
EBImage: Working with Images in R
Ra and inline: Just-in-time and inline compilation in R
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.23
Acknowledgements
Dan Gillen : Template for slidesTeaching R in Stats 211
Chris DuBois : Suggestions and referencesMatt Kayala : For introducing me to R
Seminar 2
UCI R Seminar
Front Matter
Noweb
Figures
Tables
cacheSweave
ESS
Ryacas
Networks
Untangle
My Scripts
References
2.24
Thank you for [email protected]
Figure: Obligatory cat picture