...

Sample Sweave Document 1 Introduction 2

by user

on
Category: Documents
14

views

Report

Comments

Transcript

Sample Sweave Document 1 Introduction 2
Sample Sweave Document
Duncan Murdoch
March 8, 2011
1
Introduction
This is a sample document.
It contains R code to print the value of π:
> pi
[1] 3.141593
2
Graphs
It also includes a graph:
>
>
>
>
set.seed(123)
x <- rnorm(1000)
y <- rnorm(1000)
plot(x, y)
1
●
●
3
●
●
●
●
●
●
●
●
●
●
● ●
●
●●
●
●
●●●●
● ●
●
●
●
●
● ●●
● ● ●●
●
●
●
●
●
●● ●
● ●●
● ●
●
●● ● ●●●
●
●
●
● ●
● ●●●●
● ●●
●
●●
●
● ● ●
● ●
●
● ●●
●● ●
● ●● ●
●
●●●●
●
●
●●● ●
●
●
● ●● ●
●
●● ● ●●
●●●
● ●●
●
●
●●● ●
●
●● ●
●● ● ● ●
●●
●
● ●●
●
●
●●●
●●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
● ●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●●
● ●●● ● ● ● ●
●●●●
●
●
●●
●
●
●●
●
●
●●
● ● ●●
●●●
●●● ●
●●
●
●
● ●● ●
●●
●
●
●●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●●● ●
●
● ●
●●● ●
●
●
● ●● ●
●●●
● ●●●
●
●
●
● ● ● ●●
●●
●●
●
●●●
●
●
●● ●
●●
●●
●●●
● ●
●●●
●
●
●●●●
●
●●●
● ● ●●
●
●
●●●●●●●
●
● ●
●●●●
●●
●
●●
●●●
●● ●
● ●●● ● ● ● ●
●
●
●●
●
●●●
●●
●●
●
●
●●● ●
●
●●
● ●
●●●●
●●●
●●
●
●
●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●●●
●●●
●
●
● ●●●
●●
● ● ●●
●●
●
●●
● ●●
●●
●●●
●●
●●
● ●
●
●●●
●●●
●●
●●●
●
●●
●●
●●
●
●
●
●
●
●●● ●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●● ● ●●
●● ●
●● ● ●
●●●●
●●
●● ●●
●
●●●● ●●
●
●●
●
● ● ●● ●● ●●
●
●
●
●●
●●
●●
●●
●
● ●
●● ●●
●
●
●
●●
●
●● ● ●● ● ●●●● ●●
●
● ●●●●
●●
●● ●
●●●
●
●●
● ●●
●
●●●●●
● ●● ● ●● ●
●
●
●
●
● ●●
●
● ●
●●●
●
●● ● ● ●
●●
●
●
● ●●●●●
●●●
●● ●
● ●
●
●
●●●●
●●
● ●
●
●● ●
●●
●● ● ●●●●●
●●●
●
●
●
●●● ●●
●● ●●
● ●●●● ●
●●●
●●● ●
●
●
●● ●
●●
●
●
●
●●●
●
●●
●
● ●●
● ●●
●● ● ●
●● ● ● ●
●
●
●
●●●
●
●
●●
●
●●
●● ●
● ●●
●●
●
● ● ●●
● ●●
● ●●
●●
●● ●
●
● ●●
●●● ● ●● ●
● ●●●● ●●● ●
●
● ●●
●● ●
● ●● ●
●
●
● ●● ●
●
●
●
● ● ●● ●
●
●
●●
● ●
●
●●
●
●
● ● ●
● ●● ●
●
●
●
●
●
●
●
● ● ●
●
●
● ● ●
●
●
●●
● ●
● ● ●
● ●
●
● ●
●
●●
●
●
● ●
●
●
●●
●
● ●
●●
●
●
●
●
●
●
●
●
●
−3
−2
−1
0
y
1
2
●
●
●
●
−3
−2
−1
0
1
2
3
x
Watch out for multiple execution of figure chunks:
> x <- 0
> x <- x + 1
> plot(x, x)
> x
[1] 1
2
4.0
3.5
3.0
2.0
2.5
x
●
2.0
2.5
3.0
3.5
4.0
x
Now look at x:
> x
[1] 3
This happens because R runs the code once to create a PDF, once to
create a DVI, and once just in case you turned both of those off.
3
Tables
Sweave itself doesn’t do anything special with tables, but there are various
R packages that can produce LaTeX tables. For example,
> x <- matrix(1:6, nrow = 2, dimnames = list(c("a", "b"), c("c",
+
"d", "this that")))
> x
3
x c
a 1
b 2
d this that
3
5
4
6
c d this that
a 1 3
5
b 2 4
6
> library(Hmisc)
> latex(x, file = "")
4
Slow code
Code chunks are computed every time you run Sweave, so you don’t want
to put slow computations in them. You can run those outside of Sweave
and use save() from R and load() in your Sweave document to make the
objects available. There are also packages that cache each chunk, such as
cacheSweave.
5
Named blocks
Code chunks can be named, and then re-used later just by giving the name
as <<name>> within another chunk. For example,
4
> set.seed(123)
> x <- rnorm(100)
> plot(x, col = "red")
●
●
2
●
●
●
●
1
●
●
●●
0
●●
●
●
●
●
●
●
●
●
−1
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
● ●●
●
● ●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
● ●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
−2
x
●
●
●
●
●
●
●
0
20
40
60
Index
5
80
100
>
>
>
>
set.seed(123)
x <- rnorm(100)
plot(x, col = "red")
abline(h = mean(x))
●
●
2
●
●
●
●
1
●
●
●●
0
●●
●
●
●
●
●
●
●
●
−1
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
● ●●
●
● ●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
● ●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
−2
x
●
●
●
●
●
●
●
0
20
40
60
Index
6
80
100
>
>
>
>
>
set.seed(123)
x <- rnorm(100)
plot(x, col = "red")
abline(h = mean(x))
abline(h = median(x), col = "red")
●
●
2
●
●
●
●
1
●
●
●●
0
●●
●
●
●
●
●
●
●
●
−1
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
● ●●
●
● ●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●
●
● ●
●
●●
●
●
●
●
●
●
●
x
●
●
●
●
●
●
●
●
●
−2
●
●
●
0
20
40
60
80
100
Index
6
More resources
See the “Reproducible Research” task view on http://cran.r-project.org,
or just Google for “Sweave”.
7
7
Setting up TeXShop
With Dave Gabrielson’s help, I started using TeXShop, and have the following
instructions to set it up to use with Sweave:
1. In Preferences – Typesetting – Sync Method, choose “SyncTeX”.
2. Create a file called Library/TeXShop/Engines/Sweave.engine containing the lines
#!/bin/tcsh
Rscript -e "patchDVI::SweavePDF(
'$1' )"
in your home directory, and give it executable permissions.
3. Install the patchDVI package from repository http://R-forge.r-project.
org into R. (This needs to be version 1.8.1572 or better.)
4. If you are using R 2.12.2 or earlier, you need to install a new copy of
Sweave.sty. The current one is available at https://svn.r-project.
org/R/share/texmf/tex/latex/Sweave.sty.
5. When editing a .Rnw file in TeXShop, choose the Sweave engine from
the menu.
6. Add the \SweaveOpts{concordance=TRUE} line to your document.
8
Fly UP