[R] Convert Sweave document to a function

From: David.Epstein <David.Epstein_at_warwick.ac.uk>
Date: Sun, 20 Mar 2011 12:19:27 -0700 (PDT)

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

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.

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 20 Mar 2011 - 19:30:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Mar 2011 - 17:20:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive