Re: [R] Sweave with scan()-ed data

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Mon, 07 Mar 2011 10:31:36 -0500

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}
>
>
>
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 07 Mar 2011 - 15:35:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 Mar 2011 - 16:50:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive