Suppress C warning messages in R
Asked Answered
M

1

7

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!

Marcelina answered 12/1, 2012 at 21:28 Comment(5)
Why so vague? Are you interfacing to libsvm via an R package? Or are you working on something custom? GCC is a compiler, I doubt run-time warnings are coming from it, more likely they are printfs in the libsvm C code.Tedious
yep e1071 package - I think you're right, the messages are coming from a printf statement. Do I need to go into the C code and delete it? or is there something I can do from R?Marcelina
also I don't remember ever installing libsvm on my machine, I just downloaded the R package. Does that mean the libsvm source code is in the R package?Marcelina
I think this is related to your recent question, and your other recent question. Try and keep them all in the same place.Vogeley
In case it helps, the code printing the messages is in the file src/svm.cpp in the e1071 package sources.Bradfordbradlee
S
11

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).

Sharper answered 12/1, 2012 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.