Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress C warning messages in R

Tags:

c

r

gcc

libsvm

I am calling an R function from the R package e1071 which is interfaced with libsvm (a C program). This function is passing C (printf) warning messages to the R console. I know this because the warning messages are of the form (warning:...) whereas R warning messages are capitalized (i.e. Warning:...).

I've tried everything to get rid of these messages in R (sink, suppressWarnings, invisible) but nothing seems to work.

Any ideas?

Thanks!

like image 564
Michael Avatar asked Jan 12 '12 21:01

Michael


People also ask

How do I suppress a message in R?

The simplest way of handling conditions in R is to simply ignore them: Ignore errors with try() . Ignore warnings with suppressWarnings() . Ignore messages with suppressMessages() .

What are warning messages in R?

The warning R function generates a warning message. The stop R function generates an error message and stops executing the current R code.


1 Answers

The function uses stdio instead of Rprintf/REprintf or warning which is why re-direction of the R output won't work. The proper solution is to fix the calls in libsvm to use R output instead.

Hacking the stdio output is possible - you can re-direct the output to your own pipe and do what you want with it, but a) it's a bit of work in C and b) it's dangerous because you need to restore the standard behavior after you're done with the function - even if it errors out and c) in may interact with R output if used on a shell.

If you want a really whacky, dirty yet quick solution, run your function in collect(parallel(..., silent=TRUE))[[1]] from multicore - it suppresses stdout (you can add multicore:::closeStderr() if you want to suppress stderr as well).

like image 138
Simon Urbanek Avatar answered Oct 11 '22 16:10

Simon Urbanek