# Re: [R] Convert Sweave document to a function

From: Brian Diggs <diggsb_at_ohsu.edu>
Date: Mon, 21 Mar 2011 10:06:11 -0700

On 3/20/2011 12:19 PM, David.Epstein wrote:
> I like Sweave, which I consider to be a great contribution. I have just
> written a .Rnw document that comes to about 6 pages of mixed code and
> mathematical explanation. Now I want to turn the R code into a function. My
> R code currently contains statements like N<-1000 and theta<- pi/10. In the
> next version of the document, I want N and theta to be parameters of a
> function, so that they can be easily varied. My explanation of the code is
> still valid, and it seems to me that, if I only knew how to manage the
> trick, I would need to change almost nothing in the latex.
>
> The document contains about 6 different code chunks, and 7 different chunks
> of latex.
>
> I tried putting
> functionname<- function(N,theta) {
> into the first code chunk and
> }
> into the last code chunk, but Sweave said this was poor grammar and rejected
> it.
>
> Is there a reasonable way to make my .Rnw source into a function definition?
> I would like maintainability of the code to be a criterion for "reasonable",
> and I would like to keep latex explanations of what the code is doing
> adjacent to the code being explained.
>
> One other point is that I will want to export some of the variables computed
> in the function to outside the function, so that they are not variables
> local to the function body. I mention this only because it may affect the
> solution, if any, to my problem.
>
> Thanks for any help
> David

The problem you ran into is that an R function can only contain R code (and that each Sweave chunk must be parseable on its own). The best solution I know of (though it may not be a good one), is to put all the TeX code inside of a cat(), drop all the noweb notation (which may mean doing yourself what Sweave is doing itself in, for example, fig=TRUE or echo=TRUE chunks), and then wrap that in a function call.

For example, an Sweave set (pulled from Sweave-test-1.Rnw):

Now we look at Gaussian data:

<<>>=
library(stats)
x <- rnorm(20)
print(x)
print(t1 <- t.test(x))
@
Note that we can easily integrate some numbers into standard text: The third element of vector \texttt{x} is \Sexpr{x[3]}, the $p$-value of the test is \Sexpr{format.pval(t1$p.value)}. %$

Now we look at a summary of the famous iris data set, and we want to see the commands in the code chunks:

Would turn into (untested):

cat("
Now we look at Gaussian data:
")
cat("
\\begin{Schunk}
\\begin{Sinput}
library(stats)
x <- rnorm(20)
print(x)
print(t1 <- t.test(x))
\\end{Sinput}
\\begin{Soutput}
")
library(stats)
x <- rnorm(20)
print(x)
print(t1 <- t.test(x))
cat("
\\end{Soutput}
\\end{Schunk}

Note that we can easily integrate some numbers into standard text: The third element of vector \texttt{x} is ",x[3],", the $p$-value of the test is ",format.pval(t1\$p.value),"." ,sep="")
cat("
Now we look at a summary of the famous iris data set, and we want to see the commands in the code chunks:
")

This has several drawbacks. First, having to put all the TeX inside of a cat is ugly (and you lose any editor support for it actually being TeX). Second, you have to manually do all the Sweave part yourself, including duplicating the input and output (if both are wanted), meaning it is easy for things to get out of sync, and creating and including figures.

A different approach which might work better is the brew package. It is not Sweave, but can be used to created a file which can then be passed to Sweave (I think); I've not used it, but from what I've seen others say about it, it may be an approach to this sort of meta-templating in multiple languages (TeX and R).

> --
> View this message in context: http://r.789695.n4.nabble.com/Convert-Sweave-document-to-a-function-tp3391654p3391654.html
> Sent from the R help mailing list archive at Nabble.com.
>

--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help