An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120920/49c8122b/attachment.pl>
Sweave - if \Sexpr{} than \SweaveInput{"my.Rnw"}
4 messages · Witold E Wolski, Duncan Murdoch, Yihui Xie
On 20/09/2012 8:47 AM, Witold E Wolski wrote:
Depending on an R computation I would like to include an Sweave documents
in the main Sweave document.
How can I do it?
So I was thinking .... to use Latex features :
\newif\ifpaper
\ifpaper
\SweaveInput{"my1.Rnw"}
\else
\SweaveInput{"my2.Rnw"}
\fi
But how do I set paper to true or false given an \Sexpr ??
\papertrue % or
\paperfalse
Any ideas?
The SweaveInput directives are processed before any expressions are
evaluated, so you can't do it that way. You can have Sweave chunks emit
LaTex code, so this might achieve a similar effect:
<<results=tex>>=
if ( test ) name <- "my1"
else name <- "my2"
Sweave( paste0(name, ".Rnw") )
paste0("\\input{", name, ".tex}")
@
I've never tried having a Sweave chunk call Sweave(), so there might be
troubles there, and you might only be able to input .tex files, not Rnw
files.
Duncan Murdoch
If you want to program Sweave documents, you can try the knitr package. This case will be something like: <<>>= paper <- TRUE # or change it to FALSE @ <<child=if (paper) 'my1.Rnw' else 'my2.Rnw'>>= @ i.e. you use the logical variable 'paper' to control which child document to include in the parent document. See http://yihui.name/knitr/ Regards, Yihui -- Yihui Xie <xieyihui at gmail.com> Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA
On Thu, Sep 20, 2012 at 7:47 AM, Witold E Wolski <wewolski at gmail.com> wrote:
Depending on an R computation I would like to include an Sweave documents
in the main Sweave document.
How can I do it?
So I was thinking .... to use Latex features :
\newif\ifpaper
\ifpaper
\SweaveInput{"my1.Rnw"}
\else
\SweaveInput{"my2.Rnw"}
\fi
But how do I set paper to true or false given an \Sexpr ??
\papertrue % or
\paperfalse
Any ideas?
cheers
--
Witold Eryk Wolski
Triemlistrasse 155
8047 Zuerich
On 20/09/2012 9:05 AM, Duncan Murdoch wrote:
On 20/09/2012 8:47 AM, Witold E Wolski wrote:
Depending on an R computation I would like to include an Sweave documents
in the main Sweave document.
How can I do it?
So I was thinking .... to use Latex features :
\newif\ifpaper
\ifpaper
\SweaveInput{"my1.Rnw"}
\else
\SweaveInput{"my2.Rnw"}
\fi
But how do I set paper to true or false given an \Sexpr ??
\papertrue % or
\paperfalse
Any ideas?
The SweaveInput directives are processed before any expressions are
evaluated, so you can't do it that way. You can have Sweave chunks emit
LaTex code, so this might achieve a similar effect:
<<results=tex>>=
if ( test ) name <- "my1"
else name <- "my2"
Sweave( paste0(name, ".Rnw") )
paste0("\\input{", name, ".tex}")
@
I've never tried having a Sweave chunk call Sweave(), so there might be
troubles there, and you might only be able to input .tex files, not Rnw
files.
I was curious, so I tried this. It's fine to run Sweave in a code
chunk, but it prints a fair bit by default, so the chunk up above won't
work exactly as written. You need to cat() the \input line, and tell
Sweave not to print anything, or just ignore what it prints. I think
this would do it:
<<echo=false, results=tex>>=
if ( test ) name <- "my1"
else name <- "my2"
Sweave( paste0(name, ".Rnw"), quiet=TRUE )
cat( paste0("\\input{", name, ".tex}") )
@
Duncan Murdoch