[R] tryCatch?

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed May 4 19:21:45 CEST 2011


Start with try(): you may find it easier to understand.

if(inherits(try(<call1>), "try-error")) <call2>

so in your case

if(inherits(try(sqlSave(pg, tbl[i, ], "tbl", append = TRUE,
                         rownames = FALSE))))
     sqlUpdate(pg, tbl[i, ], index = "key")

or some such.

On Wed, 4 May 2011, Mikkel Grum wrote:

> 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
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list