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)))
(noting that
using cat()
entails supplying your own line returns, and that glue::glue()
might be preferable
to sprintf()
for constructing templated strings). 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.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "print('a')",
linters = print_linter()
)
#> <text>:1:1: warning: [print_linter] Use cat() instead of print() logging messages. Use message() in cases calling for a signalled condition.
#> print('a')
#> ^~~~~~~~~~
lint(
text = "print(paste(x, 'y'))",
linters = print_linter()
)
#> <text>:1:1: warning: [print_linter] Use cat() instead of print() logging messages. Use message() in cases calling for a signalled condition.
#> print(paste(x, 'y'))
#> ^~~~~~~~~~~~~~~~~~~~
# okay
lint(
text = "print(x)",
linters = print_linter()
)
#> ℹ No lints found.