Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I have a long R script that throws some warnings, which I can ignore. I could use

suppressWarnings(expr)

for single statements. But how can I suppress warnings in R globally? Is there an option for this?

closed

2 Answers

+1 vote
by
edited
 
Best answer

To suppress warnings in the global settings, you can use the warn option in options function as follows:

options(warn=-1)

To temporarily suppress warnings in global settings and turn them back on, use the following code:

defaultW <- getOption("warn") 

options(warn = -1) 

[YOUR CODE] 

options(warn = defaultW)

If you want to explore more in R programming then watch this R programming tutorial for beginner:

0 votes
by (33.1k points)

You want options(warn=-1). But, note that warn=0 is not the safest warning level and it should not be assumed as the current one, particularly within scripts or functions. Thus the safest way to temporary turn off warnings is:

oldw <- getOption("warn")

options(warn = -1)

[your "silenced" code]

options(warn = oldw)

Hope this answer helps you!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked Aug 10, 2019 in R Programming by Ajinkya757 (5.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...