?withCallingHandlers
E.g.,
R> f <- function(expr) {
warnings <- character()
withCallingHandlers(expr, warning=function(e){
warnings <<- c(warnings, conditionMessage(e))
invokeRestart("muffleWarning")
})
warnings
}
R> f(1:10)
character(0)
R> f(warning("Hmm"))
[1] "Hmm"
R> f({warning("Hmm"); x <- 666 ; warning("Possible
problem")})
[1] "Hmm" "Possible problem"
R> x
[1] 666
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On Behalf
> Of Christofer Bogaso
> Sent: Wednesday, June 12, 2013 12:07 PM
> To: r-help
> Subject: [R] Fetching and Saving warnings from a function
>
> Hello again,
>
> Let say I have following user defined function:
>
> Myfn <- function(x) {
> if (x < 0) {
> warning("Negative value")
> }
> return(x)
> }
>
> Now I want to create some function which will save the Warnings from
> 'Myfn', so that I can use those warnings later for more analysis.
Therefore
> I was thinking of following type of function:
>
> ProposedWarningStoreFunction <- function() {
> ......... ............ ...........
> }
>
> therefore,if I run 'ProposedWarningStoreFunction', I should get
following
> kind of results:
>
> Warnings <- ProposedWarningStoreFunction({Result <- Myfn(-4)})
>
> > Result
> -4
>
> > Warnings
> In Myfn(-3) : Negative value
>
> Can somebody here point me how to achieve this? Or is it possible to
> achieve at all?
>
> Thank you for your help.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.