Sweave with scan()-ed data

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Sweave with scan()-ed data

Michael Friendly
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?
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}




--
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sweave with scan()-ed data

Duncan Murdoch-2
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}
>
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Sweave with scan()-ed data

William Revelle
At 10:21 AM -0500 3/7/11, 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?
>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}

At 10:31 AM -0500 3/7/11, Duncan Murdoch wrote:

>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
>...snip...
>>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}
>...snip...
>>       .541   .537    .534   .35    .367   .32   .555
>>       .38   .358    .359   .424    .446   .325   .598   .452
>>
>>@
>>\end{frame}
>>\end{document}


Not a Sweave solution, but that data set is available in psych:

library(psych)
data(bifactor)
Thurstone


Bill


>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.