Stop Lapply from Printing to Console

Stop lapply from printing to console

Use invisible, eg:

invisible(FUN("hello"))
hello 1
hello 2
hello 3

You can wrap it around the lapply call in the function too to make it tidier.

How to stop lapply from printing into the console?

Using capture.output(load_shapefiles(x)) worked where invisible(load_shapefiles(x)) did not.

Thanks to Rui Barradas for the answer

EDIT: st_read has a parameter called quiet which determines whether or not it will print info about the file that it is reading. Setting quiet to TRUE eliminates the need to wrap the function in capture.output or invisible.

How to suppress part of the output from `lapply()`?

An option is invisible

invisible(lapply(N.seq, print))
#[1] 1
#[1] 2
#[1] 3
#[1] 4
#[1] 5

If we want to convert the vector to list,

as.list(N.seq)

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 stationarity test that is printed to screen

In fact, you can suppress the output to R console by rerouting it. Two methods are available in R utils, sink, and capture.output. Both methods are intended to send output to a file.

Since you want to suppress the output of a single expression, you can use capture.output, with file=NULL (default). This will return your output as a string. To prevent showing this returned string in the R console, you can use invisible.

The final code can be:

library(fractal)

lg.day.ret.vec <- rnorm(100, mean = 5, sd = 3)
shap.p <- shapiro.test(lg.day.ret.vec)$p.value

invisible(capture.output(
stat.p <- attr(stationarity(lg.day.ret.vec),"pvals")[1]
))

Hope this helps. Let me know if not.

R: How to enable printing to console when using `future::plan(sequential)` when in `browser()`?

(Author of future here:) In future (>= 1.20.1) [2020-11-03], the official recommended solution is to use:

plan(sequential, split = TRUE)

This works with browser() and friends.

How to stop hist from printing all the information into the console

As suggested by @Rich Scriven invisible(lapply(...)) solves the problem. I thought I would re-post the answer from comments here so that my question does not hang in the air as unanswered.



Related Topics



Leave a reply



Submit