[R] Sweave - documenting a long function

Frank E Harrell Jr fharrell at virginia.edu
Tue Nov 5 12:55:06 CET 2002


On Mon, 4 Nov 2002 20:25:58 -0000
John.Gavin at ubsw.com wrote:

> Hi,
> 
> I would like to use Sweave to document a long function.
> Is it possible to split the function's code into chunks
> such that Sweave will accept each chunk without complaining.
> 
> I have tried various approaches without sucess
> but I feel sure that someone has done this already.
> Here is one attempt
> 
> ==============
> % First, define the funciton header
> <<defFunHdr, eval=FALSE>>=
> x <- function(a =1)
> @
> 
> % Explain the code in the first part of the function
> <<defFunBodyPt1, eval=FALSE>>=
> { a <- a+1
> @
> 
> % Explain the code in the second part of the function
> <<defFunBodyPt2, eval=FALSE>>=
>  a <- a^2
>  a
>  } # end of function
> @
> 
> % now bring the chunks together so that Sweave evaluates the function.
> <<evalFun, echo=FALSE>>=
> <<defFunHdr>>
> <<defFunBodyPt1>>
> <<defFunBodyPt2>>
> @
> 
> % Finally, show an example of the function
> <<exampleFun>>=
> x(3)
> @
> ===========
> 
> eval=FALSE doesnt seem to prevent the code being evaluated,
> as I get an error with the first chunk.
> 
> (label=defFunHdr)
> Error in parse(file, n, text, prompt) : parse error
> Execution halted
> 
> The output from Stangle seems ok
> i.e. I can execute it without error.
> 
> I am on R 1.6.0, on Windows NT4.
> 
> Regards,
> 
> John.
> 
> John Gavin <john.gavin at ubsw.com>,
>

It may work better to implement variations of 'pretty printing' for Sweave so that you can use different fonts, etc., from within one code chunk by using S comments.  When I'm not using Sweave, I do this with a Perl parser that does things such as changing <- to a left arrow and changing # ... text ... to \scom{... text ...}.  I define \scom in LaTeX to use a smaller Times-Roman font that for on-screen viewing is blue.  See http://biostat.virginia.edu/latex for details.  Perhaps Friedrich Leisch will consider allowing the user to specify a customized Perl script or S function that translates S code for pretty printing (including specifying prompt characters other than R>).

Frank
-- 
Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list