Learn R Programming

R.AlphA.Home (version 2.0.2)

printif: Conditionally Print an Object

Description

This function prints `x` if `show` is `TRUE`; otherwise, it returns `x` unchanged. Its main usage is to "close" dplyr-like treatment chains (using This creates an extremely handy way to process accurate line-by-line debugging, printing results when necessary and removing the print option easily without having to rewrite everything or take care about the last element in the chain.

This saves much code writing and debugging time.

It is also useful to design functions so that users can easily stop elements from being printed

Given the purpose of this function, it is much more convenient to use 1 and 0 for the 'show' argument than TRUE or FALSE, as this can be switched easily in the editor.

Usage

printif(x, show = FALSE, ...)

Value

The object `x`, printed if `show` is `TRUE`.

Arguments

x

Any object.

show

A logical value indicating whether to print `x` (default: `FALSE`).

...

Additional arguments passed to `print()`.

Examples

Run this code
# Basic usage
printif(42, show = TRUE)
printif(42, show = FALSE)

# Using numeric shortcuts
printif("Hello", 1)
printif("Hello", 0)

# Most useful usage : in a pipeline (requires dplyr)
if (requireNamespace("dplyr", quietly = TRUE)) {
  library(dplyr)
  mtcars %>%
    filter(mpg > 20) %>%
    summarise(mean_hp = mean(hp)) %>%
    printif(1)
}

Run the code above in your browser using DataLab