Ron Ron - 12 days ago 6
R Question

What's similar to an #ifdef DEBUG in R?

I'm writing R code where I would like to have it run either in "non-debug" or "debug" mode. Under the debug mode, I would like the code to print-out runtime information.

In other languages, I would typically have some sort of print function that does nothing unless a flag is turned on (either for compilation or runtime).

For example, I can use #ifdef DEBUG (in compilation time), or set a debug level in run time.

What would be the equivalent way of doing this in R?

Answer

Same thing, minus the preprocessor:

  • Define a global variable variable (or use an options() value)
  • Insert conditional code that tests for the variable
  • Works with your functions (adding ..., verbose=options(myVerbose)), in your packages, etc pp
  • I have also used it in R scripts (driven by littler) using the CRAN package getopt to pick up a command-line option --verbose or --debug.
Comments