73

I would like to clear the warnings() list using a command line.

I have tried with no success

> rm(last.warning, envir = baseenv()) 
Error in rm(last.warning, envir = baseenv()) : 
cannot remove variables from the base environment

any idea?

asked Apr 20, 2011 at 3:11

5 Answers 5

126

Try assign("last.warning", NULL, envir = baseenv())

answered Apr 20, 2011 at 3:26
Sign up to request clarification or add additional context in comments.

8 Comments

I agree this is the correct answer, but I would urge beginning users to think before using such a command. What are such users trying to do? Think hard before modifying an underlying variable (for you C++ users out there, think "private"). If you want a fresh R session, start a fresh R session. Using this and rm(list=ls()) will not get you one. If instead you want to remove warnings that are not correct and should not be addressed (are you sure of this?), then instead consider using suppressWarnings() on the corresponding function. Otherwise, you could miss important warnings.
Perfect answer.. This helps to recognize further warnings after saving previous warnings to log or file.
I get this: Error in assign("last.warning", NULL, envir = baseenv()) : cannot change value of locked binding for 'last.warning'
The error message is Error in assign("last.warning"...) occurres on non-vanilla R platforms (i.e. MRO and RRO), because last.warning is locked by default. To unlock the binding, use unlockBinding("last.warning", baseenv()). This implementation is consistent with ?warning, which says "If warn is zero (the default), a read-only variable last.warning is created."
As of R version 4.1.0, this now throws the error Error in assign("last.warning", NULL, envir = baseenv()): cannot add binding of 'last.warning' to the base environment. See @mmaechler 's answer below for a workable solution.
|
7

Just to emphasize what @Richie Cotton mentioned above (the help page now no longer mentions 2.4.0, but): This is assign("last.warning", envir = baseenv()) is really not recommended even though it's been the accepted answer here.

Rather, you should use the much more powerful error handler tools, notably for @BWMorlan's case above, you could use the - several times "advertized" tryCatch.WE() utility function which catches all warnings and errors and provides results when not error

r <- tryCatch.WE({ ... })

using demo(error.catching) in R to get the function and see it in action, or

file.show(system.file("demo/error.catching.R"))

to get the commented source.

answered Mar 5, 2021 at 17:31

1 Comment

Thanks for the very helpful (as always) answer Martin!
5

Take a look at suppressWarnings() to stop the warnings from showing up.

Notice in the help page for warnings that it says:

"....It is undocumented where last.warning is stored nor that it is visible, and this is subject to change. Prior to R 2.4.0 it was stored in the workspace, but no longer...."

Richie Cotton
122k47 gold badges254 silver badges371 bronze badges
answered Apr 20, 2011 at 3:18

3 Comments

Bill, your second link won't work for anyone who doesn't have the R-help server running on port 22913 on their loopback interface.
I looked at this function, but I still want to have warnings sometimes. I just want to clean the stack at some point.
I don't think this is what @RockScience have asked. He already has some warnings piled up (and he have extracted the information we wanted from them), and now he wants to clean the warnings().
5

I agree, I want to use a try() and gather up just the warnings generated by that try().

My solution for now is

assign("last.warning", NULL, envir = baseenv())
 myFit <- try(...)
 warned <- warnings()
assign("last.warning", NULL, envir = baseenv())
answered Jan 10, 2014 at 16:04

Comments

2

Simply use 'warning(immediate. = FALSE)' E.g.

x<-matrix(1:100,ncol=10)
plot(x,border="blue")
 Warning messages:
 1: In plot.window(...) : "border" is not a graphical parameter
 2: In plot.xy(xy, type, ...) : "border" is not a graphical parameter
 3: In axis(side = side, at = at, labels = labels, ...) :
 "border" is not a graphical parameter
 4: In axis(side = side, at = at, labels = labels, ...) :
 "border" is not a graphical parameter
 5: In box(...) : "border" is not a graphical parameter
 6: In title(...) : "border" is not a graphical parameter
warnings()
 Warning messages:
 1: In plot.window(...) : "border" is not a graphical parameter
 2: In plot.xy(xy, type, ...) : "border" is not a graphical parameter
 3: In axis(side = side, at = at, labels = labels, ...) :
 "border" is not a graphical parameter
 4: In axis(side = side, at = at, labels = labels, ...) :
 "border" is not a graphical parameter
 5: In box(...) : "border" is not a graphical parameter
 6: In title(...) : "border" is not a graphical parameter
> warning(immediate. = FALSE)
 Warning message:
> warnings()
 Warning message:
answered Dec 25, 2021 at 13:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.