Example of the code I am running below.
library(snowfall)
library(snow)
sfInit(parallel = TRUE, cpus = 3)
sfLibrary(raster)
Library raster loaded.
Library raster loaded in cluster.
I want to stop sfLibrary from printing the messages. I can't figure out how. Help please...
Thanks.
EDIT 1: This does not work:
suppressMessages(sfLibrary(raster))
Library raster loaded.
EDIT 2: This does not work:
suppressPackageStartupMessages(sfLibrary(raster))
Library raster loaded.
Library raster loaded in cluster.
Use the Source.
If you look at the source code for
sfLibrary, specifically where it prints those messages, you'll see that is usessfCat. Tracing that down (same file), it usescat.I know of two ways to prevent
catfrom dumping onto the console:capture.outputandsink.capture.output: "evaluates its arguments with the output being returned as a character string or sent to a file".Since
capture.outputreturns the captured output visibly as acharactervector, wrapping it ininvisibleor storing the return value into a variable (that is ignored and/or removed) will prevent its output on the console.sink: "send R output to a file".I personally find the use of
sink(in general) to be with some risks, especially in automation. One good example is thatknitrusessinkwhen capturing the output for code chunks; nested calls tosinkhave issues. An astute reader will notice thatcapture.outputusessink, so neither is better in that regard.Looking again at the source (first link above),
you'll see that it also calls
message, which is not caught bycapture.outputby default. You can always usecapture.output(..., type="message"), but then you aren't capturing thecatoutput as well. So you are left with having to capture both types, either with nestedcapture.outputor withsuppressMessages.I suggest you can either use
suppressMessages(invisible(capture.output(sfLibrary(raster))))or write some helper function that does that for you.