[R] Convert Sweave document to a function

David.Epstein David.Epstein at warwick.ac.uk
Sun Mar 20 20:19:27 CET 2011


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

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



More information about the R-help mailing list