Learn R Programming

pkgmaker (version 0.32.10)

.silenceF: Silencing Functions

Description

Generates a wrapper function that silences the output, messages, and/or warnings of a given function.

Usage

.silenceF(f, level = 7L)

Value

a function

Arguments

f

function to silence

level

a single numeric (integer) that indicates the silencing level, which encodes the set of output to be silenced.

It is interpreted like unix permission bit system, where each bit of the binary expression of the silencing level corresponds to a given type of output:

  • 0: nothing silenced;

  • 1: stdout;

  • 2: stderr messages;

  • 4: stderr warnings.

For example, level 3 = 2 + 1 means silencing stdout and stderr, while 5 = 3 + 2 means silencing stderr messages and warnings, but not outputs to stdout. The default value is 7 = 4 + 2 + 1, which silences all output.

Negative values are supported and mean "silence everything except the corresponding type", e.g., level = -1 silences all except stdout (computed as the binary complementary of 7, i.e. 7 - 1 = 5 = 3 + 2). See examples.

Examples

Run this code

f <- function(){
	cat("stdout message\n")
 message("stderr message")
	warning("stderr warning", immediate. = TRUE)
}

# example of generated wrapper
g <- .silenceF(f)
g

# use of silencing level
for(l in 7:-7){ message("\nLevel: ", l); .silenceF(f, l)() }

# inline functions
ifun <- .silenceF(function(){ f(); invisible(1) })
ifun()
ifun <- .silenceF(function(){ f(); 1 })
ifun()
ifun <- .silenceF(function(){ f(); 1 }, 2L)
ifun()

Run the code above in your browser using DataLab