Search code examples
rsqldfrpostgresql

Check that connection is valid


I'm using RPostgreSQL and sqldf inside my function like this:

MyFunction <- function(Connection) {

    options(sqldf.RPostgreSQL.user      = Connection[1], 
            sqldf.RPostgreSQL.password  = Connection[2],
            sqldf.RPostgreSQL.dbname    = Connection[3],
            sqldf.RPostgreSQL.host      = Connection[4], 
            sqldf.RPostgreSQL.port      = Connection[5])

    # ... some sqldf() stuff
}

How do I test that connection is valid?


Solution

  • One approach is to simply try executing the code, and catching any errors with a nice informative error message. Have a look at the documentation of tryCatch to see the details regarding how this works.

    The following blog post provides an introduction to the exception-based style of programming.