How do you print to stderr in R?

Actually the following works for me:

write("prints to stderr", stderr())

write("prints to stdout", stdout())

Here's a more flexible version for debugging/verbose use in Rscript. Not only it prints to stderr as you ask, but it also allows you to pass variable number of arguments, types etc, like printf does.

v <- function(...) cat(sprintf(...), sep='', file=stderr())

Now you can do things like:

v("name: %s  age: %d\n", name, age)

etc.

Tags:

Printing

R

Stderr