[R] Sweave data-figure coupling

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jan 5 21:14:37 CET 2009


On 1/5/2009 2:52 PM, Sebastian P. Luque wrote:
> Hi,
> 
> With the following Sweave minimal file:
> 
> 
> ---<--------------------cut here---------------start------------------->---
> \documentclass{article}
> 
> \usepackage{Sweave}
> 
> \begin{document}
> 
> <<binom-sim>>=
> thetas <- seq(0, 1, by=0.001)
> prior <- rep(1, length(thetas)) / length(thetas)
> lik <- dbinom(1, 1, thetas)
> lik.p <- prior * lik
> post <- lik.p / sum(lik.p)

You missed an @ after this chunk.  If this is true in the original, I'm 
surprised Sweave didn't report an error, but it probably ate up the 
second chunk as part of the first.

> 
> <<binom-sim-fig, fig=TRUE, include=FALSE, height=3, echo=FALSE>>=
> layout(matrix(1:2, ncol=2)); par(mar=c(5, 4, 2, 1), cex=0.75)
> matplot(thetas, cbind(prior, lik, post), type="l", lty=c(2, 1, 1),
>         xlab="theta", ylab="probability density")
> lik <- dbinom(60, 100, thetas)

It's a bad idea to generate random values in a fig=TRUE chunk, because 
the code gets evaluated multiple times, but I don't think this would 
cause your error.

Duncan Murdoch

> lik.p <- prior * lik
> post <- lik.p / sum(lik.p)
> 
> matplot(thetas, cbind(prior, lik, post), type="l", lty=c(2, 1, 1),
>         xlab="theta", ylab="probability density")
> @
> 
> \includegraphics[width=\linewidth]{test-binom-sim-fig}
> 
> 
> \end{document}
> ---<--------------------cut here---------------end--------------------->---
> 
> If the embedded chunks are evaluated directly in an R session, two
> different panel data are produced by layout(), as expected.  However,
> when weaving and latexing the file produces, then the two panels show
> the same data, corresponding to the last data set.  This doesn't happen
> if the data are much simpler, say using matplot() with simple sequences
> of numbers.  Any ideas what is causing this and how to get around it?
> 
> 
> 
> Cheers,
>




More information about the R-help mailing list