Skip to content
Prev 155910 / 398513 Next

tryCatch question

On Fri, Sep 12, 2008 at 6:02 AM, Antje <niederlein-rstat at yahoo.de> wrote:
***Anything*** you catch in a tryCatch() will ***interrupt*** the main
expression from being evaluated.

Example:
[1] 3.3 2.1

There was no warning caught, so the main expression was completely
evaluated and 'y' got assigned.  Now consider this:
List of 2
 $ message: chr "NAs introduced by coercion"
 $ call   : language doTryCatch(return(expr), name, parentenv, handler)
 - attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
Error: object "z" not found

See how the warning is caught and tryCatch() is finished immediately.

So, if the string contains a numeric value you wish to convert it,
otherwise keep it as is, correct?  If you don't have NAs, Infs etc in
your strings, then you can easily to as follows:
[1] 3.3
Warning message:
NAs introduced by coercion
[1] "hello"

If you want "NA" to become as.double(NA) etc, then you have to add
identical(value, "NA") etc.

/Henrik