Skip to content
Back to formatted view

Raw Message

Message-ID: <CAJ55+dLq1AusTghPYbst9K3CFGZLp0rRq=G_EDqgz-A_5bOtRA@mail.gmail.com>
Date: 2012-01-12T19:04:09Z
From: Thomas Lumley
Subject: is there an equivalent for #ifdef (C langage) in R
In-Reply-To: <E66794E69CFDE04D9A70842786030B93259D2E@PA-MBX03.na.tibco.com>

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