[R] recommended way to group function calls in Sweave
Rainer Schuermann
rainer.schuermann at gmx.net
Wed Apr 25 17:34:17 CEST 2012
What I usually do when I have to write a report with some functions I use
multiple times is that I put them in a separate file (call it "setup.Rnw" or
so).
The first chunk there loads the libraries, sets initial variable values etc:
<<echo=FALSE, results=hide>>=
library( xtable )
d <- iris
ind <- 1
@
Then I have a number of chunks that are not evaluated at this point:
<<chunk_name_1,eval=FALSE,echo=FALSE>>=
summary(d[ , ind])
cor(d[ , ind])
@
<<chunk_name_2,eval=FALSE,echo=FALSE>>=
# produce a nice table from some data
@
In my master file, I load these "definitions" first
SweaveInput( "setup.Rnw" )
and from here, I can suse the named chunks almost like function calls, as you
you describe below. The advantage (for me) is that I have only one place where
I maintain the functions code, and only one line in the "real document",
rather than a lot of code, possibly distributed over the document..
Rgds,
Rainer
On Wednesday 25 April 2012 16:20:13 Liviu Andronic wrote:
> On Wed, Apr 25, 2012 at 3:41 PM, Duncan Murdoch
> <murdoch.duncan at gmail.com> wrote:
> > I would use the last method, or if the calls were truly repetitive (i.e.
> > always identical, not just the same pattern), use a named chunk.
> >
> Labeled chunks are indeed what I was looking for [1]. As far as I
> understand, this is what "Sweave functions" (or are these macros?)
> look like:
>
>
> <<>>=
> d <- iris
> ind <- 1:2
> @
>
> <<sw>>=
> summary(d[ , ind])
> cor(d[ , ind])
> @
>
> <<>>=
> d <- iris
> ind <- 2:4
> <<sw>>
> @
>
>
> Regards
> Liviu
>
> [1] vignette('Sweave', 'utils')
>
> ______________________________________________
> 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.
More information about the R-help
mailing list