Convert Sweave document to a function

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

Convert Sweave document to a function

David.Epstein
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
Reply | Threaded
Open this post in threaded view
|

Re: Convert Sweave document to a function

Martin Morgan
Hi David --

On 03/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.

If I understand what you're after, maybe something like

some text describing block 1
<<block1, eval=FALSE>>=
## R code
cat("theta:", theta, "\n"
@
more text describing block 2
<<block2, eval=FASLE>>=
## more R code
cat("N", N, "\n")
@
These get assembled as
<<func>>=
functioname <- function(N, theta) {
<<block1>>
<<block2>>
N * theta
}
@
and to illustrate their use
<<doit>>=
val = functionname(100, 200)
@


>
> 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
>
> --
> 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.
>
> ______________________________________________
> [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.


--
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

______________________________________________
[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: Convert Sweave document to a function

Brian Diggs
In reply to this post by David.Epstein
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

______________________________________________
[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.