简体   繁体   中英

R: Suppressing output in RStudio console (code and warnings)

I'm working on an R script that is quite long and will eventually have to be used by other people. I don't want RStudio to spit back every line of code to me when I'm running the script, because it is quite messy. Ideally, I want to write my own output to console that is more user-friendly, such as progress updates or custom warning/error messages.

Is there a way to do this? I tried using sink() to send my output somewhere else, but that only got rid of the output created by my code and not the output of code itself.

Thanks!

You can use suppressWarnings() to hide warnings.

> log(-1)
[1] NaN
Warning message:
In log(-1) : NaNs produced
> suppressWarnings(log(-1))
[1] NaN

try(call, silent = TRUE) for code that might throw errors.

> 1/"1"
Error in 1/"1" : non-numeric argument to binary operator
> try(1/"1", silent = TRUE)

There are also suppressPackageStartupMessages() and suppressMessages() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM