Learn R Programming

lintr (version 3.4.0)

print_linter: Block usage of print() for logging

Description

The default print method for character vectors is appropriate for interactively inspecting objects, not for logging messages. Thus checked-in usage like print(paste('Data has', nrow(DF), 'rows.')) is better served by using cat(), e.g. cat(sprintf('Data has %d rows.\n', nrow(DF))). Note that using cat() entails supplying your own line returns; glue::glue() might be preferable to sprintf() for constructing templated strings. Alternatively, writeLines() can be used to print character vectors with their own line returns. Lastly, note that message() differs slightly from cat() in that it prints to stderr by default, not stdout, but is still a good option to consider for logging purposes.

Usage

print_linter()

Arguments

Tags

best_practices, consistency

See Also

linters for a complete list of linters available in lintr.

Examples

Run this code
# will produce lints
lint(
  text = "print('a')",
  linters = print_linter()
)

lint(
  text = "print(paste(x, 'y'))",
  linters = print_linter()
)

# okay
lint(
  text = "print(x)",
  linters = print_linter()
)

Run the code above in your browser using DataLab