[R] tryCatch?

Mikkel Grum mi2kelgrum at yahoo.com
Wed May 4 19:59:41 CEST 2011


Beautiful Prof.

This worked:

for (i in seq(along = tbl$key)) {
    if (inherits(try(sqlSave(pg, tbl[i, ], "tbl", append = TRUE,
        rownames = FALSE), silent = TRUE), "try-error", TRUE))
	    sqlUpdate(pg, tbl[i, ], index = "key1")
    )
}

--- On Wed, 5/4/11, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:

> From: Prof Brian Ripley <ripley at stats.ox.ac.uk>
> Subject: Re: [R] tryCatch?
> To: "Mikkel Grum" <mi2kelgrum at yahoo.com>
> Cc: "R Help" <r-help at r-project.org>
> Date: Wednesday, May 4, 2011, 12:21 PM
> 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