As we knew, if a function is not used correctly, an error message will throw/display in R:> rnorm(-1)Error in rnorm(-1) : invalid arguments I would like to catch the error message or assign an error message to a variable so that I can use them some where else. So, how to catch error messages in R? Thanks, -james
?try guox at ucalgary.ca wrote:> As we knew, if a function is not used correctly, > an error message will throw/display in R: > >> rnorm(-1) > Error in rnorm(-1) : invalid arguments > > I would like to catch the error message or assign an error message to a > variable so that I can use them some where else. > So, how to catch error messages in R? Thanks, > > -james > > ______________________________________________ > 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.
Try this: x <- tryCatch(rnorm(-1), error = c) On Thu, May 27, 2010 at 5:25 PM, <guox@ucalgary.ca> wrote:> As we knew, if a function is not used correctly, > an error message will throw/display in R: > > > rnorm(-1) > Error in rnorm(-1) : invalid arguments > > I would like to catch the error message or assign an error message to a > variable so that I can use them some where else. > So, how to catch error messages in R? Thanks, > > -james > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]