Tor
Tor

Reputation: 1612

Suppress output of a function

I'm looking to suppress the output of one command (in this case, the apply function).

Is it possible to do this without using sink()? I've found the described solution below, but would like to do this in one line if possible.

How to suppress output

Upvotes: 125

Views: 152892

Answers (11)

Sharpie
Sharpie

Reputation: 17653

Use the capture.output() function. It works very much like a one-off sink() and unlike invisible(), it can suppress more than just print messages. Set the file argument to "/dev/null" on UNIX or "NUL" on windows, or, better, use nullfile() (>= 3.6.0) for platform independence. If you need to support R< 3.6, use R.utils::nullfile(). For example, considering Dirk's note:

> invisible(cat("Hi\n"))
Hi

> capture.output(cat("Hi\n"), file = nullfile())
> 

Upvotes: 73

ramon vazquez
ramon vazquez

Reputation: 5

for the return(something) part inside an R function:

return(invisible(something))  

works ok

invisible(return(something))

does not work at all

Upvotes: 0

stevec
stevec

Reputation: 52268

If you're wondering how to suppress a warning() you can use suppressWarnings() like so:

suppressWarnings(warning("hi"))

Whereas these two will still show the warning:

invisible(warning("Hi"))
# shows 'Hi'

capture.output(warning("Hi"), file='NUL')
# shows 'Hi'

Upvotes: 1

tjebo
tjebo

Reputation: 23737

Making Hadley's comment to an answer: Use of apply family without printing is possible with use of the plyr package

x <- 1:2
lapply(x, function(x) x + 1)
#> [[1]]
#> [1] 2
#> 
#> [[2]]
#> [1] 3

plyr::l_ply(x, function(x) x + 1)

Upvotes: 0

deep_friar
deep_friar

Reputation: 11

Here is a version that is robust to errors in the code to be shushed:

quietly <- function(x) {
  sink("/dev/null") # on Windows (?) instead use `sink("NUL")`
  tryCatch(suppressMessages(x), finally = sink())
}
  • This is based directly on the accepted answer, for which thanks.
  • But it avoids leaving output silenced if an error occurs in the quieted code.

Upvotes: 0

stevec
stevec

Reputation: 52268

In case anyone's arriving here looking for a solution applicable to RMarkdown, this will suppress all output:

```{r error=FALSE, warning=FALSE, message=FALSE}
invisible({capture.output({

# Your code goes here
2 * 2
# etc
# etc


})})
```

The code will run, but the output will not be printed to the HTML document

Upvotes: 7

Danny
Danny

Reputation: 3336

The following function should do what you want exactly:

hush=function(code){
  sink("NUL") # use /dev/null in UNIX
  tmp = code
  sink()
  return(tmp)
}

For example with the function here:

foo=function(){
  print("BAR!")
  return(42)
}

running

x = hush(foo())

Will assign 42 to x but will not print "BAR!" to STDOUT

Note than in a UNIX OS you will need to replace "NUL" with "/dev/null"

Upvotes: 31

Bryan King
Bryan King

Reputation: 1

invisible(cat("Dataset: ", dataset, fill = TRUE))
invisible(cat(" Width: " ,width, fill = TRUE))
invisible(cat(" Bin1:  " ,bin1interval, fill = TRUE))
invisible(cat(" Bin2:  " ,bin2interval, fill = TRUE))
invisible(cat(" Bin3:  " ,bin3interval, fill = TRUE))

produces output without NULL at the end of the line or on the next line

Dataset:  17 19 26 29 31 32 34 45 47 51 52 59 60 62 63
Width:  15.33333

Bin1:   17 32.33333
Bin2:   32.33333 47.66667
Bin3:   47.66667 63

Upvotes: 0

Kristy
Kristy

Reputation: 111

you can use 'capture.output' like below. This allows you to use the data later:

log <- capture.output({
  test <- CensReg.SMN(cc=cc,x=x,y=y, nu=NULL, type="Normal")
})

test$betas

Upvotes: 11

Aniko
Aniko

Reputation: 18864

R only automatically prints the output of unassigned expressions, so just assign the result of the apply to a variable, and it won't get printed.

Upvotes: 11

Shane
Shane

Reputation: 100164

It isn't clear why you want to do this without sink, but you can wrap any commands in the invisible() function and it will suppress the output. For instance:

1:10 # prints output
invisible(1:10) # hides it

Otherwise, you can always combine things into one line with a semicolon and parentheses:

{ sink("/dev/null"); ....; sink(); }

Upvotes: 117

Related Questions