閃亮

2017-10-12 149 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