[R] is there an equivalent for #ifdef (C langage) in R

Thomas Lumley tlumley at uw.edu
Thu Jan 12 20:04:09 CET 2012


On Fri, Jan 13, 2012 at 6:12 AM, William Dunlap <wdunlap at tibco.com> wrote:
> You could put your debug-only code into a function calls like
>   ifDebug(diagnosticPlot(c))
> and
>   ifDebug({
>        tmp <- timeConsumingFunction(c)
>        stopifnot(tmp < 1.0)
>   })
> When you want debug output define ifDebug as
>   ifDebug <- function(expr) force(expr)
> and when you don't want it define it as
>   ifDebug <- function(expr) NULL
> Because of lazy evaluation, the expression won't get evaluated
> in the latter case.
>

This is a nice solution, for the question as posted.

For other uses of #ifdef, it might be worth noting that it is less
tolerant of syntax errors than #ifdef is, since the expression has to
parse, not just tokenize.

  -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland



More information about the R-help mailing list