本文介绍了R trycatch() 与 err 和 warn 处理程序就位,但 Shiny 仍然崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重现:

#app.R
library(shiny)
library(RODBC)
savefunc <- function() {
      conn <- odbcConnect(...) #put in a conn string u know works
      df = data.frame(testing=c("testing"))
      columnTypes = list(testing="varchar(128)")
      tryCatch(
        {
          sqlSave(conn, dat=df, tablename ="...", #put in a pre-existing tbl
            rownames = FALSE, colnames = FALSE, append=TRUE, varTypes=columnTypes)
        },
        err=function(errorCondition) {
          cat("in err handler")
          message(errorCondition)
        },
        warn=function(warningCondition) {
           cat("in warn handler")
          message(warningCondition)
        },
        finally={
          odbcClose(conn)
        }
      )
    }

server <- function(input, output) {
  observeEvent(input$doBtn, {
    savefunc()
  })
}

ui <- shinyUI(fluidPage(
  mainPanel(
    actionButton("doBtn", "Do something")
  )
))

shinyApp(ui = ui, server = server)

错误没有被发现...虽然 R 似乎仍在运行(没有崩溃),但当您查看 Shiny 应用程序/用户界面时,您会看到它已经死了.控制台中的错误类似于:

Errors aren't caught... while R still seems to run (hasn't crashed), when you look at the Shiny app / UI you will see that it's died. The error in the console is something like:

Warning: Unhandled error in observer: unable to append to table xxx
observeEvent(input$doBtn)

(根据上面的示例,您可能会略有不同)但关键是错误现在会以闪亮的方式传播到观察者.有没有办法让闪亮的应用程序本身也进行故障转移?还是抑制观察者内部的错误?

(You may get sthing slightly diff based on the example above) but the point is that the error will now have travelled up to the observer in shiny. Is there a way to get the shiny app itself to also fail over? Or suppress the error inside the observer?

推荐答案

我刚刚有一次非常相似的经历(未捕获的错误),事实上,只需要替换 err=function(e)error=function(e)(我猜对于 warnwarning 也是一样).

I just had a very similar experience (uncatched errors), and as a matter of fact, just had to replace err=function(e) with error=function(e) (and I guess it goes the same for warn and warning).

编辑:

我实际上只是尝试过你的代码,对我来说,当我这样做时它可以工作:

I actually just tried with your code, and for me it works when I do this :

savefunc <- function() {
    tryCatch({
        conn <- odbcConnect(...)
        df = data.frame(testing=c("testing"))
        columnTypes = list(testing="varchar(128)")
        tryCatch({
            sqlSave(conn, dat=df, tablename="...", rownames=FALSE, colnames=FALSE, append=TRUE, varTypes=columnTypes)
        }, error=function(e) {
            cat(paste("in err handler\n"),e)
        }, warning=function(w) {
            cat(paste("in warn handler\n"),w)
        }, finally={
            odbcClose(conn)
        })
    }, error=function(e) {
        cat(paste("in err handler2\n"),e)
    }, warning=function(w) {
        cat(paste("in warn handler2\n"),w)
    })
}

它显然很脏,我不确定它为什么会起作用.我刚刚摆脱了 message() 调用并封装了两个 trycatch().也许值得一试.

It is obviously very dirty, and I am not really sure why it works. I just got rid of the message() calls and encapsulated two trycatch().Deserves a try, maybe.

这篇关于R trycatch() 与 err 和 warn 处理程序就位,但 Shiny 仍然崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:26