Skip to content

?print_linter should mention writeLines() #2841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MichaelChirico opened this issue Mar 25, 2025 · 0 comments
Open

?print_linter should mention writeLines() #2841

MichaelChirico opened this issue Mar 25, 2025 · 0 comments

Comments

@MichaelChirico
Copy link
Collaborator

#' 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.

I often use writeLines() instead of cat(), e.g. replacing print() with cat() here is a bit awkward:

print(paste('time:', Sys.time()))

# ugh
cat('time: ', Sys.time(), '\n', sep='')
cat(paste('time:', Sys.time()), '\n', sep='')
# not bad in this simple example
cat(sprintf('time: %s\n', Sys.time()))

# drop-in replacement
writeLines(paste('time:', Sys.time()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant