[R] Sweave with scan()-ed data

Duncan Murdoch murdoch.duncan at gmail.com
Mon Mar 7 16:31:36 CET 2011


On 07/03/2011 10:21 AM, Michael Friendly wrote:
> In an Sweave slide, I want to use sem::read.moments() and
> sem::specify.model(), which work
> by using scan() to read the following lines, up to the first blank
> line.  However, Sweave
> throws an error:
>
>   >  Sweave("sem-thurstone.Rnw")
> Writing to file sem-thurstone.tex
> Processing code chunks ...
>    1 : term hide (label=arrests-setup)
>    2 : echo term hide (label=thurstone-data)
>
> Error:  chunk 2 (label=thurstone-data)
> Error in sem-thurstone.Rnw:43:12: unexpected numeric constant
> 42:     .828
> 43:     .776   .779
>                  ^
>
> Is there some switch or option for Sweave that will make this work?

I don't think so.  The way Sweave works is not to pipe the code chunks 
into a console-like evaluator, it's to parse the whole code chunk, then
evaluate the expressions one by one.

So you can probably fake the behaviour by telling read.moments to read 
from somewhere else and showing different code than you really
executed on the slide, but I don't think there's a way to honestly do 
what you want.

You might be able to automate this, i.e. to write code that source()'s a 
file and echos the right tex code to make it look as though it was entered
at the command line, but it would be messy.

Duncan Murdoch
> Below is the slide in question in an executable example:
>
> \documentclass[dvipsnames,pdflatex,compress,beamer]{beamer}
> \usepackage{Sweave}
>
> \definecolor{Sinput}{rgb}{1,0,0}
> \definecolor{Scode}{rgb}{0,0,0.56}
> \definecolor{Soutput}{rgb}{0,0,1}
> \DefineVerbatimEnvironment{Sinput}{Verbatim}{formatcom={\color{Sinput}},fontsize=\footnotesize,baselinestretch=0.9}
> \DefineVerbatimEnvironment{Soutput}{Verbatim}{formatcom={\color{Soutput}},fontsize=\footnotesize,baselinestretch=0.85}
> \DefineVerbatimEnvironment{Scode}{Verbatim}{formatcom={\color{Scode}},fontsize=\small}
>
> \begin{document}
>
> \SweaveOpts{engine=R,height=6,width=6,results=hide,fig=FALSE,echo=TRUE}
> \SweaveOpts{prefix.string=fig/sem}
>
> \section{sem package: Second-order CFA, Thurstone data}
>
> \begin{frame}[fragile]
>     \frametitle{sem package: Second-order CFA, Thurstone data}
>     \framesubtitle{Data}
> Data on 9 ability variables:
> <<thurstone-data, echo=TRUE>>=
> R.thur<- read.moments(diag=FALSE, names=c('Sentences','Vocabulary',
>      'Sent.Completion','First.Letters','4.Letter.Words','Suffixes',
>      'Letter.Series','Pedigrees', 'Letter.Group'))
>       .828
>       .776   .779
>       .439   .493    .46
>       .432   .464    .425   .674
>       .447   .489    .443   .59    .541
>       .447   .432    .401   .381    .402   .288
>       .541   .537    .534   .35    .367   .32   .555
>       .38   .358    .359   .424    .446   .325   .598   .452
>
> @
> \end{frame}
> \end{document}
>
>
>
>



More information about the R-help mailing list