Increase connection attempt with DBI

I'm trying to build a Shiny application which will be connected to an unstable PostgreSQL Database with DBI. It works fine when the database is stable, but gives Whitelabel Error whenever the database is slow or down for a moment. How can I configure my application so that it will keep trying to connect a number of times before throwing error? A sample code is given below:

library(shiny)
library(DBI)

con <- dbConnect(
  RPostgres::Postgres(),
  host = "myip",
  port = "myport",
  dbname = "mydb",
  user = "user",
  password = "password"
)

ui <- fluidPage(
  tableOutput("mytable")
)

server <- function(input, output, session){
  output$mytable <- renderTable(
    dbGetQuery(
      con,
      "select * from mytable;"
    )
  )
}

shinyApp(ui, server)

something like:

  repeat {
    if (dbCanConnect(drv, ...) == TRUE) {
      do_whatever_you_like
      break
    }
    Sys.sleep(1)
  }

or have a look on insistently() function from purrr package.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.