How can you always suppress messages in R?
Use the type parameter in sink
# Open a file to send messages to
zz <- file("messages.Rout", open = "wt")
# Divert messages to that file
sink(zz, type = "message")
message("not gonna show up in console")
Suppressing some messages in R but leaving others?
You can wrap this function in one of the suppress* functions, suppressMessages
, suppressWarnings
or suppressPackageStartupMessages
. See the help pages of those functions for more details.
Disable messages upon loading a package
Just use suppressMessages()
around your library()
call:
edd@max:~$ R
R version 2.14.1 (2011-12-22)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)
[...]
R> suppressMessages(library(ROCR))
R> # silently loaded
R> search()
[1] ".GlobalEnv" "package:ROCR" # it's really there
[3] "package:gplots" "package:KernSmooth"
[5] "package:grid" "package:caTools"
[7] "package:bitops" "package:gdata"
[9] "package:gtools" "package:stats"
[11] "package:graphics" "package:grDevices"
[13] "package:utils" "package:datasets"
[15] "package:methods" "Autoloads"
[17] "package:base"
R>
Suppress messages from underlying C-function in R
You had problems using sink()
and capture.output()
normally because sink()
does not redirect output from REprintf
, as we see in comments from the source code for REprintf
:
/* =========
* Printing:
* =========
*
* All printing in R is done via the functions Rprintf and REprintf
* or their (v) versions Rvprintf and REvprintf.
* These routines work exactly like (v)printf(3). Rprintf writes to
* ``standard output''. It is redirected by the sink() function,
* and is suitable for ordinary output. REprintf writes to
* ``standard error'' and is useful for error messages and warnings.
* It is not redirected by sink().
However, we can use type = "message"
to deal with this; from help("capture.output")
:
Messages sent to stderr() (including those from message, warning and
stop) are captured by type = "message". Note that this can be "unsafe" and should only be used with care.
First I make a C++ function with the same behavior you're dealing with:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector example_function(NumericVector x) {
REprintf("CPLEX environment opened\n");
REprintf("Closed CPLEX environment\n");
// As mentioned by Dirk Eddelbuettel in the comments,
// Rcpp::Rcerr goes into the same REprintf() stream:
Rcerr << "Some more stuff\n";
return x;
}
If I call it from R normally, I get:
example_function(42)
CPLEX environment opened
Closed CPLEX environment
Some more stuff
[1] 42
However, I could instead do this:
invisible(capture.output(example_function(42), type = "message"))
[1] 42
And while the output is is printed to the console, the message is not.
Warning
I would be remiss if I didn't mention the warning from the help file I quoted above:
Note that this can be "unsafe" and should only be used with care.
The reason is that this will eliminate all output from actual errors as well. Consider the following:
> log("A")
Error in log("A") : non-numeric argument to mathematical function
> invisible(capture.output(log("A"), type = "message"))
>
You may or may not want to therefore send the captured output to a text file in case you have to diagnose something that went wrong. For example:
invisible(capture.output(log("A"), type = "message", file = "example.txt"))
Then I don't have to see the message in the console, but if I need to check example.txt
afterward, the message is there:
Error in log("A") : non-numeric argument to mathematical function
How to suppress warnings globally in an R Script
You could use
options(warn=-1)
But note that turning off warning messages globally might not be a good idea.
To turn warnings back on, use
options(warn=0)
(or whatever your default is for warn
, see this answer)
How to hide or disable in-function printed message
You can use capture.output
with invisible
> invisible(capture.output(y <- ff(2)))
> y
[1] 4
or sink
> sink("file")
> y <- ff(2)
> sink()
> y
[1] 4
R - suppressMessages / suppressWarnings not working
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(...)))
R package development how to suppress messages generated from dependency package?
Here are some things you can do to reduce the noise when loading packages with devtools::load_all
:
devtools::load_all(..., quiet = TRUE)
handles messages for this single package, but not necessarily dependent packagestry explicitly loading required packages in
./R/zzz.R
in theonLoad
function. For example:.onLoad <- function(libname, pkgname) {
invisible(suppressPackageStartupMessages(
sapply(c("tibble", "purrr", "dplyr", "tidyr", "ggplot2", "data.table"),
requireNamespace, quietly = TRUE)
))
}(BTW: I used
sapply
here for laziness, not that it adds much to things. It could easily have been afor
loop with no consequence.)For a discussion about the use of
requireNamespace
in place oflibrary
, see "library vs require", and "Writing R Extensions" where it statesR code in the package should call library or require only exceptionally. Such calls are never needed for packages listed in ‘Depends’ as they will already be on the search path. It used to be common practice to use require calls for packages listed in ‘Suggests’ in functions which used their functionality, but nowadays it is better to access such functionality via :: calls.
What we are doing is technically not required, but I think by forcing doing it this way, it is encouraging more-silent operation. (This rides on the coat-tails of
Notice that I used
suppressPackageStartupMessages
. "Courteous" package maintainers usepackageStartupMessage
instead ofmessage
for their loading messages: the latter takes a bit more work and is much less discriminant than the former, which is very easily suppressed without unintended consequences. There are many packages that do not do this, for which I think it's fair to submit a PR to fix.Another comment about
requireNamespace
: this means that the functions in those packages will not be in the search path of the R sessions. If the user will always be using certain packages (e.g.,data.table
ordplyr
), then you might want to explicitly load them withlibrary
. From "Writing R Extensions" again:Field ‘Depends’ should nowadays be used rarely, only for packages which are intended to be put on the search path to make their facilities available to the end user (and not to the package itself): for example it makes sense that a user of package latticeExtra would want the functions of package lattice made available.
However, if you're being good about your package, then you are using
::
notation for all non-base packages anyway. There are certainly ways you can get around using::
, but (1) CRAN checks are rather intense at times, (2) explicit is usually "A Good Thing (tm)", and (3) it can actually make maintainability much easier (such as when a dependent package changes their API/ABI and you need to check all calls to their package, where searching forpkgname::
is much easier than searching for each of their functions individually).Some packages use
.onLoad
too liberally, doing things that are not strictly necessary and/or have unneeded side-effect. For this, you can always write a function such asload_deppkgs_silently(updatesearchpath=TRUE)
that can be called manually or on-load with the presence of an option. (I'm thinking about your end users here, I'm a big fan of providing flexibility and the ability to not load things they way I do.)
How to suppress RED progress messages that are not warning messages? SOLUTIONS NOT WORKING
How about:
suppressMessages(proteinToGenome(...))
Where the arguments you are using in the model replace ...
How to suppress warning messages when loading a library?
These are not messages but warnings. You can do:
suppressWarnings(library(RODBC))
or
suppressWarnings(suppressMessages(library(RODBC)))
to suppress both types.
Related Topics
How to Search for a String in One Column in Other Columns of a Data Frame
How to Set the Latex Path for Sweave in R
Calculating Standard Deviation Across Rows
Adding Shade to R Lineplot Denotes Standard Error
Running an R Script Using a Windows Shortcut
How to Get the First 10 Words in a String in R
Adding All Elements of Two Lists
How to Tell Which Packages I am Not Using in My R Script
How to Read a Subset of Large Dataset in R
R Equivalent of Stata Local or Global MACros
R: Is There a Good Replacement for Plyr::Rbind.Fill in Dplyr
How to Get Dimnames in Xtable.Table Output
Mathematical Expression in Axis Label
Wrapping Base R Reshape for Ease-Of-Use
Format Axis Tick Labels to Percentage in Plotly