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
Convert Accented Characters into Ascii Character
Remove 'Search' Option But Leave 'Search Columns' Option
Merge Dataframes on Matching A, B and *Closest* C
How to Train a Ml Model in Sparklyr and Predict New Values on Another Dataframe
Element-Wise Concatenation of String Vectors
Apply Function to Elements Over a List
Align Two Data.Frames Next to Each Other with Knitr
Dynamic Linking with Rpath Not Working Under Ubuntu 17.10
Data.Table Is Not Handling Integer64 in by Statement
Get Monthly Means from Dataframe of Several Years of Daily Temps
Annotate Values Above Bars (Ggplot Faceted)
Stop Lapply from Printing to Console
How to Handle Vectors Without Knowing the Type in Rcpp
Increase Space Between Bars in Ggplot
Number Format, Writing 1E-5 Instead of 0.00001
R: Web Scraping Yahoo.Finance After 2019 Change