65

How do you print to stderr in R?

This would especially useful for scripts written in Rscript.

Marc Reside
  • 2,935
  • 1
  • 24
  • 26
Frank
  • 64,140
  • 93
  • 237
  • 324

5 Answers5

67

Actually the following works for me:

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

write("prints to stdout", stdout())
Frank
  • 64,140
  • 93
  • 237
  • 324
  • This is on Linux, with R 2.8.1 (using Rscript) – Frank Jul 10 '09 at 12:22
  • 2
    This code also works on Windows. For more formatting control, you can use cat instead of write. – Richie Cotton Jul 13 '09 at 09:01
  • 1
    FWIW, this is limited, in that it only works if the first argument can be outputted by `cat`. There are classes that require `print` to work, where you have to use `sink`, as Galwegian says below. – Harlan Jul 08 '13 at 18:10
19

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.

arielf
  • 5,802
  • 1
  • 36
  • 48
  • 1
    This is great!!! For ones who didn't understand the ... , Refer to this.. http://stackoverflow.com/questions/3057341/how-to-use-rs-ellipsis-feature-when-writing-your-own-function – myloginid Sep 04 '15 at 03:36
13
message('for writing diagnostic info to standard error')

message is used for generating ‘simple’ diagnostic messages which are neither warnings nor errors, but nevertheless represented as conditions. Unlike warnings and errors, a final newline is regarded as part of the message, and is optional. The default handler sends the message to the stderr() connection.

De Novo
  • 7,120
  • 1
  • 23
  • 39
8

Is it possible to configure the print function to print to stderr?

From Ripley himself:

No, but where standard output goes is controlled by sink(), so you can achieve the same effect. R internally has no idea what output comes from print() (which is not just one function but hundreds of methods).

Galwegian
  • 41,475
  • 16
  • 112
  • 158
8

Contrary to the accepted answer's suggestion to use the write() function, this would be an inappropriate use of the function as it is designed to be used for writing data to a file instead of messages. From the write() documentation, we have:

The data (usually a matrix) x are written to file file. If x is a two-dimensional matrix you need to transpose it to get the columns in file the same as those in the internal representation.

Moreover, note that write() provides a convenience wrapper for data output of columns.

write
# function (x, file = "data", ncolumns = if (is.character(x)) 1 else 5, 
#     append = FALSE, sep = " ") 
# cat(x, file = file, sep = c(rep.int(sep, ncolumns - 1), "\n"), 
#     append = append)

That said, I would recommend using cat() alongside of the appropriate condition handler stderr() or stdout() in file = ... parameter.

Thus, to write a message to standard error, one should use:

cat("a message that goes to standard error", file = stderr())

Or:

message("also sent to standard error")

For standard out, just use cat() directly as it is setup to write to stdout() by default.

cat("displays in standard out by default")
coatless
  • 20,011
  • 13
  • 69
  • 84
  • I don't understand the problem with write: `stdout`, `stdin`, and `stderr` [are handled like any other file (at least in *nix systems)](https://www.howtogeek.com/435903/what-are-stdin-stdout-and-stderr-on-linux/) with their own file descriptors. – Yan Foto Nov 06 '19 at 14:28
  • The `write()` function in _R_ is a convenience wrapper for outputting uniformly formatted data to a file. When writing text -- like status updates -- `message()` is better suited for the task as it is pre-configured to write to `stderr()`. Similarly, `cat()` excels at writing to `stdout()`. – coatless Nov 06 '19 at 17:05