闪亮

2017-10-12 229 views
2

背景闪亮

在tryCatch改变反应值在我的应用我想允许用户下载SQL数据库的快照。由于SQL连接可能存在一些错误,因此我想使用tryCatch构造。如果出现错误,我想向用户显示一条有意义的错误消息。为此,我创建了一个,其值设置在错误处理程序位中。我发现错误处理程序中的值已更改,但renderPrint函数未触发。任何想法我必须改变?

代码

library(shiny) 

ui <- fluidPage(downloadButton("dat"), verbatimTextOutput("debug")) 

server <- function(input, output) { 
    errMsg <- reactiveVal() 
    output$dat <- downloadHandler(filename = "test.xlsx", 
           content = function(nF) { 
            tryCatch({ 
             write.csv(mtcars, nF) 
             stop("simulate SQL error") 
            }, error = function(err) { 
             print("Error Handler") 
             errMsg("Error") 
            }) 

           }) 
    output$debug <- renderPrint(errMsg()) 

} 

shinyApp(ui, server) 

回答

1

我不知道为什么,但tryCatch似乎并不触发元素无效。但是你可以手动完成。改变这样的陈述似乎工作:

tryCatch({ 
    write.csv(mtcars, nF) 
    stop("simulate SQL error") 
}, error = function(err) { 
    print("Error Handler") 
    errMsg("Error") 
}, 
finally = invalidateLater(1)) 
+0

谢谢!这的确有诀窍! +1 – thothal

+1

顺便说一句,我不认为这是'tryCatch'的具体问题,而是'downloadHandler'。我尝试添加一个'actionButton'('button')和另一'reactiveVal'('count'),并将此服务器'observeEvent(输入$按钮,tryCatch(停止( “停止”),错误=功能(ERR )count(count()+ 1))'这里的文本被正确更新。 – thothal