Suppress Automatic Output to Console in R

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 console output from Keras in R

This is a Keras issue (i.e. not an R or Tensorflow one) and, as I have already commented, there have been several people complaining about this, but a solution has not been proposed by Keras maintainers so far.

A hack, as already suggested in the issue thread above, is to locate the keras/backend/__init__.py file and comment out the following line:

sys.stderr.write('Using TensorFlow backend.\n')

Last time I checked, Keras in R only uses the Tensorflow backend, so you shouldn't need to repeat this for the other backend options in __init__.py.

suppress console output in r markdown, but keep plot

Wrapping any object in invisible will prevent automatically printing it.

You should be able to use

invisible(lapply(obj,function(x) plot(x,main="some plot")))

However the fact that echo=FALSE doesn't work suggests that there might be something else going on.

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

suppress print output sf object

Probably a bad approach, not knowing what further processing the return values will be subjected to (I get the sense that separate dims, head_sf are undesirable), and especially bad if you abhor loops, but uses variant of your invisible(capture.output as seen suppress auto console output:

library(sf)
library(tidyverse)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
nc1 <- nc
nc2 <- nc
nc3 <- nc
nc_lst <- list(nc1, nc2, nc3)

prepare_sf_head_for_further_process_loop3 <- function(nc_lst) {
a_quote <- list()
for(i in 1:length(nc_lst)) {
invisible(capture.output(a_quote[[i]] <- list(dims = dim(nc_lst[[i]]), head_sf = print(nc_lst[[i]], n = getOption('sf_max_print', default = 6)))))
}
return(a_quote)
}

>b3 <- prepare_sf_head_for_further_process_loop3(nc_lst)
>b3
[[1]]
[[1]]$dims
[1] 100 15

[[1]]$head_sf
Simple feature collection with 100 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
geographic CRS: NAD27
First 10 features:
AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74
1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1
2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0
3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5
4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1
5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9
6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7
7 0.062 1.547 1834 1834 Camden 37029 37029 15 286 0
8 0.091 1.284 1835 1835 Gates 37073 37073 37 420 0
9 0.118 1.421 1836 1836 Warren 37185 37185 93 968 4
10 0.124 1.428 1837 1837 Stokes 37169 37169 85 1612 1
NWBIR74 BIR79 SID79 NWBIR79 geometry
1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3...
2 10 542 3 12 MULTIPOLYGON (((-81.23989 3...
3 208 3616 6 260 MULTIPOLYGON (((-80.45634 3...
4 123 830 2 145 MULTIPOLYGON (((-76.00897 3...
5 1066 1606 3 1197 MULTIPOLYGON (((-77.21767 3...
6 954 1838 5 1237 MULTIPOLYGON (((-76.74506 3...
7 115 350 2 139 MULTIPOLYGON (((-76.00897 3...
8 254 594 2 371 MULTIPOLYGON (((-76.56251 3...
9 748 1190 2 844 MULTIPOLYGON (((-78.30876 3...
10 160 2038 5 176 MULTIPOLYGON (((-80.02567 3...

>class(nc_lst[[1]]
[1] "sf" "data.frame"
>class(b3[[1]]$head_sf)
[1] "sf" "data.frame"

Dang, don't know why getOption('sf_max_print', default = 6) didn't restrict to 6.
dplyer has controlled quote/unquote, but I haven't worked out application to this as yet. These are my bad suggestions so far.

Additionally, from tidyverse What Hadley suggests, taking Hadley's and swapping head and print,

glimpse_head2 <- function(x, n = 6) {
head(print(x, n))
invisible(x)
}

While this doesn't get the dim, seems far simpler than my above and provides the desired bbox values. And still doesn't restrict to 6, because the head method for sfg is hardcoded to 10L:

> getS3method('head', 'sfg')
function (x, n = 10L, ...)
{
structure(head(unclass(x), n = n, ...), class = class(x))
}
<bytecode: 0x5646a6a38228>
<environment: namespace:sf>

How to suppress output

Look at help(sink) to do that. On Unix I'd do

sink("/dev/null")    # now suppresses
.... # do stuff
sink() # to undo prior suppression, back to normal now

and the Windows equivalent (with a tip-of-the-hat to Johannes) is

sink("NUL")
....
sink()


Related Topics



Leave a reply



Submit