Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide or disable in-function printed message

Tags:

r

printing

Suppose I have a function such as:

ff <- function(x) {   cat(x, "\n")   x^2} 

And run it by:

y <- ff(5) # 5  y # [1] 25 

My question is how to disable or hide the 5 printed from cat(x, "\n") such as:

y <- ff(5) y # [1] 25 
like image 271
David Z Avatar asked Dec 10 '15 17:12

David Z


People also ask

How do you suppress print function in Python?

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 do you skip print in Python?

Search Code Snippets | how to print in python and skip a line. If you want to skip a line, then you can do that with "\n" print("Hello\n World\n!") #It should print: #Hello #World #!


2 Answers

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 
like image 57
romants Avatar answered Sep 21 '22 18:09

romants


Here's a nice function for suppressing output from cat() by Hadley Wickham:

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

Use it like this:

y <- quiet(ff(5)) 

Source: http://r.789695.n4.nabble.com/Suppressing-output-e-g-from-cat-td859876.html

like image 36
Ben Avatar answered Sep 22 '22 18:09

Ben