Can the print() command in R be quieted?

后端 未结 5 1951
故里飘歌
故里飘歌 2021-02-14 04:58

In R some functions can print information and return values, can the print be silenced?

For example:

print.and.return <- function() {
  print(\"foo\         


        
5条回答
  •  感动是毒
    2021-02-14 05:42

    I agree with hadley and mbq's suggestion of capture.output as the most general solution. For the special case of functions that you write (i.e., ones where you control the content), use message rather than print. That way you can suppress the output with suppressMessages.

    print.and.return2 <- function() {
      message("foo")
      return("bar")
    }
    
    # Compare:
    print.and.return2()
    suppressMessages(print.and.return2())
    

提交回复
热议问题