4

I have tried expressions with suppressMessages(expr), suppressWarnings(expr), but they keep outputting messages.

eg:

suppressWarnings(ksvm(y~., data=data, type='C-svc', cross=5, kernel=kernel))

keeps generating this message.

Setting default kernel parameters

How do I suppress messages from libraries? Is there a way to do this globally?

Have tried:

{r messages=FALSE, warnings=FALSE}
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
coolscitist
  • 3,317
  • 8
  • 42
  • 59

3 Answers3

12

Here is the link to the line where the output is generated: https://github.com/cran/kernlab/blob/master/R/ksvm.R#L88

Looking at that we see that the message is displayed with cat() not with message(). suppressMessages() does not suppress the cat output.

There are multiple ways to get rid of the cat output. One is to capture the message and then hide it like so:

invisible(capture.output(ksvm(...)))
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • Thank you that worked. I am very new to R and this really sucks. I was hoping the libraries would use some sort of logging mechanism which could be easily suppressed. Is there a way to suppress all such "cat", "print" or other outputs from libraries? – coolscitist Mar 16 '18 at 20:31
  • @coolscitist Well I think they are supposed to use `message()`, `warning()` and the like. But some don't. I don't know if they have a reason not to use those or just generally are not aware of standard ways of showing messages to the user. To suppress cat - like mentioned there are multiple tricks you can use. Some can be found here: https://stackoverflow.com/q/5310393/1953718 – Karolis Koncevičius Mar 16 '18 at 20:35
  • @coolscitist Also you can try writing your own function `suppressCat()` or something like that. This answer might be helpful for that: https://stat.ethz.ch/pipermail/r-help/2008-January/151465.html – Karolis Koncevičius Mar 16 '18 at 20:35
3

If it does not say that it is a warning, you should use suppressMessages. Try putting the function call in braces:

suppressMessages({ksvm(y~., data=data, type='C-svc', cross=5, kernel=kernel)})
C. Braun
  • 5,061
  • 19
  • 47
2

You can pass an empty list to the kpar argument.

Like ksvm(y~., data=data, type='C-svc', cross=5, kernel=kernel, kpar = list())

Michael Kuehn
  • 21
  • 1
  • 6