vprint

Barry Zeeberg [aut, cre]

2025-05-10

vprint: More Flexible Form of ‘if(verbose) print(…)’

 

 

Barry Zeeberg



R functions are not supposed to print text without giving the user the option to turn the printing off or on using a Boolean ‘verbose’ in a construct like ‘if(verbose) print(…)’.

But this black/white approach is rather rigid, and an approach with shades of gray might be more appropriate in many circumstances.

For instance, there might be print statements intended as

debugging during development or revision

essential for the user as an instruction or the location of a saved file

help out someone using the function for the first time, but annoying for anyone else

informative for the general user

informative for the specialized user

Each of these can be assigned a ‘class’ such as

-1 = debugging only
0 = always on
1,2,3 . . . = levels of user interest

Here are some examples to make this crystal clear

The first example is a debugging statement
The class is '-1', but the 'verbose' vector 1:2 does not include the class
vprint(-1,1:2,"debug")

The class is '1', and the 'verbose' vector 1:2 includes the class
vprint(1,1:2,"usually")

The class is '0', so we ignore the 'verbose' vector
vprint(0,1:2,"always")

The class is '3', but the 'verbose' vector 1:2 does not include the class
vprint(3,1:2,"sometimes")

Note that the class argument is hardwired into the function code by the developer. For example, for a debugging statement, ‘-1’ is hardwired in. The choice of whether or not to display this message is subsequently governed by the user selecting which values to include in the ‘verbose’ vector parameter.

the vprint() statement is used in place of

if(verbose)
  print(...)

so it makes the code more neat, more compact, more readable.

There is no extra overhead relative to the traditional ‘if(verbose) print(…)’ formalism, as the original ‘verbose’ parameter can be changed from a Boolean to an integer vector to convert to the ‘verbose’ vector.

You do not need to delete or comment out tons of debug statements that you might very well need again sooner than you think.

vprint() also has ‘quote=FALSE’ wrapped in, so the programmer does not need to keep inserting this nuisance text.