How to Hide or Disable In-Function Printed Message

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

How to block calls to print?

Python lets you overwrite standard output (stdout) with any file object. This should work cross platform and write to the null device.

import sys, os

# Disable
def blockPrint():
sys.stdout = open(os.devnull, 'w')

# Restore
def enablePrint():
sys.stdout = sys.__stdout__


print 'This will print'

blockPrint()
print "This won't"

enablePrint()
print "This will too"

If you don't want that one function to print, call blockPrint() before it, and enablePrint() when you want it to continue. If you want to disable all printing, start blocking at the top of the file.

How can I hide messages in R markdown when message=FALSE doesn't work

I don't know this function. Perhaps it triggers a cat. I would try to use this function:

quiet <- function(x) {
sink(tempfile())
on.exit(sink())
invisible(force(x))
}

like this:

quiet(glove$fit_transform(tcm, n_iter = 1000, ......))

Suppress automatic output to console in R

The issue is due to the fact that the function has multiple print statements, where stop, warning, or message would have been appropriate so that people can use suppressWarnings or suppressMessages.

You can work arount it using invisible(capture.output()) around your whole assignment (not just the right side).


f1 <- function(n, ...){
print("Random print statement")
cat("Random cat statement\n")
rnorm(n = n, ...)
}

f1(2)
#> [1] "Random print statement"
#> Random cat statement
#> [1] -0.1115004 -1.0830523
invisible(capture.output(x <- f1(2)))
x
#> [1] 0.0464493 -0.1453540

See also suppress messages displayed by "print" instead of "message" or "warning" in R.

Suppress output of a function

It isn't clear why you want to do this without sink, but you can wrap any commands in the invisible() function and it will suppress the output. For instance:

1:10 # prints output
invisible(1:10) # hides it

Otherwise, you can always combine things into one line with a semicolon and parentheses:

{ sink("/dev/null"); ....; sink(); }

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

Hide printing statement in RMarkdown

If you use message in your function instead of print, you can suppress the message

```{r} 
f <- function() {
message("Some printing") # change this line
return(1)
}

res <- f()
print(res) # original prints both
```
#> Some printing
#> [1] 1

either explicitly with suppressMessages:

```{r} 
res <- suppressMessages(f())
print(res)
```
#> [1] 1

or via the message=FALSE chunk option:

```{r, message=FALSE} 
res <- f()
print(res)
```
#> [1] 1

Messages designed for this kind of use. If you really want to keep print, you could subset (which is awkward), or use capture.output to capture and store the message while storing the result in another variable:

```{r}
f <- function() {
print("Some printing")
return(1)
}

trash <- capture.output(res <- f())
print(res)
```
#> [1] 1

...but that's still pretty awkward.

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>


Related Topics



Leave a reply



Submit