How to hide or disable in-function printed message

后端 未结 3 1904
情深已故
情深已故 2020-12-01 08:04

Suppose I have a function such as:

ff <- function(x) {
  cat(x, \"\\n\")
  x^2}

And run it by:

y <- ff(5)
# 5 
y
# [1         


        
3条回答
  •  被撕碎了的回忆
    2020-12-01 08:43

    Here's a nice function for suppressing output from cat() by Hadley Wickham:

    quiet <- function(x) { 
      sink(tempfile()) 
      on.exit(sink()) 
      invisible(force(x)) 
    } 
    

    Use it like this:

    y <- quiet(ff(5))
    

    Source: http://r.789695.n4.nabble.com/Suppressing-output-e-g-from-cat-td859876.html

提交回复
热议问题