[R] tryCatch?

Mikkel Grum mi2kelgrum at yahoo.com
Wed May 4 19:14:25 CEST 2011


I would like to do inserts into a database table, but do updates in the fairly rare cases in which the inserts fail. I thought tryCatch might be the way to do it, but I honestly do not understand the help file for tryCatch at all.

I thought something like this might work:
for (i in seq(along = tbl$key)) {
    tryCatch(sqlSave(pg, tbl[i, ], "tbl", append = TRUE, rownames = FALSE),
        if (fails) do {sqlUpdate(pg, tbl[i, ], index = "key")})
}

This obviously isn't the correct syntax, but could tryCatch do this if I got the syntax right? And any tips on what the right syntax would be?

Mikkel



More information about the R-help mailing list