[R] Sweave - documenting a long function

Friedrich.Leisch@ci.tuwien.ac.at Friedrich.Leisch at ci.tuwien.ac.at
Tue Nov 5 10:58:23 CET 2002



Good point, unfortunately I don't think you can do this currently,
because all chunks are parsed, hence have to be complete
expressions. but I am rewriting this part of the code for 1.7
anyway, I'll do it such that this will work. .

what should curretently work is to have syntactically complete parts
of the *body* of the function in different chunks, but you have to
type the header twice, in terms of your example:

**********************************************************

% First, show the funciton header to the reader
<<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>>=
x <- function(a =1) {
<<defFunBodyPt1>>
<<defFunBodyPt2>>
}
@

% Finally, show an example of the function
<<exampleFun>>=
x(3)
@

**********************************************************


best,
fritz


>>>>> On Mon, 4 Nov 2002 20:25:58 -0000,
>>>>> John Gavin (JG) 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>,
  > Quantitative Risk Models and Statistics,
  > UBS Warburg, 100 Liverpool Street (6th floor),
  > London EC2M 2RH, UK.
  > Phone +44 (0) 207 567 4289
  > Fax   +44 (0) 207 568 5352

  > Visit our website at http://www.ubswarburg.com

  > This message contains confidential information and is intended only 
  > for the individual named.  If you are not the named addressee you 
  > should not disseminate, distribute or copy this e-mail.  Please 
  > notify the sender immediately by e-mail if you have received this 
  > e-mail by mistake and delete this e-mail from your system.

  > E-mail transmission cannot be guaranteed to be secure or error-free 
  > as information could be intercepted, corrupted, lost, destroyed, 
  > arrive late or incomplete, or contain viruses.  The sender therefore 
  > does not accept liability for any errors or omissions in the contents 
  > of this message which arise as a result of e-mail transmission.  If 
  > verification is required please request a hard-copy version.  This 
  > message is provided for informational purposes and should not be 
  > construed as a solicitation or offer to buy or sell any securities or 
  > related financial instruments.


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