【问题标题】:tryCatch to Prevent R Shiny App Crushing on MySQL Connection ErrortryCatch 以防止 R Shiny 应用程序因 MySQL 连接错误而崩溃
【发布时间】:2022-11-10 06:28:54
【问题描述】:

当错误的连接凭据传递给连接字符串时,我的 Shiny App 崩溃了。然后,我将连接字符串放在 try Catch 中,如下所示: ,,,

 ConnectToDb <- function(){
        con <- tryCatch({
          dbConnect(MySQL(), 
                    user = input$db_user,
                    password = input$db_password,
                    dbname = input$db_name,
                    host = input$db_host,
                    port = input$db_port)
          print("Connection made")
          ####
          sql <- "SELECT * FROM PMSAnalytics;"
          
          data <- dbGetQuery(con, sql)
          
          #   # Disconnect from the DB
          dbDisconnect(con)
          #   # Convert to data.frame
          data <- data.frame(data)
          data$timestamp <- as_datetime(now())
          data
          ####
          
        }, error = function(e) {
          message('Please confirm your login details')
          print(e)
        },
        warning = function(w){
          message('A warning has occured')
          print(w)
          return(NA)
        }
        )
      }

,,, 现在应用程序不会崩溃,但是当使用错误的凭据时不会传递错误消息或警告,并且我也没有获得连接成功。我已经检查了这个网站是否有类似的问题,但我似乎没有得到任何问题。请协助完善代码。

问候, 克里斯

【问题讨论】:

    标签: mysql r shiny connection


    【解决方案1】:

    我使用“showNotification”,它直接在闪亮的 ui 中显示通知,您也可以使用它来成功连接。 同样的选项是 持续时间 = 60(在这种情况下为 60 秒) 关闭按钮 = 假

    例如: 错误=函数(e){ showNotification(paste0(e), type = 'error')

    }

    警告=函数(w){ showNotification(paste0(e), type = 'error') 返回(NA) }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多