R: how to clear all warnings
RWarningsR Problem Overview
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?
R Solutions
Solution 1 - R
Try assign("last.warning", NULL, envir = baseenv())
Solution 2 - R
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...."
Solution 3 - R
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())
Solution 4 - R
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.
Solution 5 - R
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: