我想制作一个闪亮的应用程序,它可以检索一些反应性值(用reactiveVal
初始化),这样,当发生错误时,我可以在应用程序崩溃之前保存“反应性值”的值。
我尝试了一些带有选项“ shiny.error
”(options(shiny.error = function(){...})
)“的事情,但是我可以得到这些值...
这是一个简单应用程序的框架:
library(shiny)
options(shiny.error = function() {
previous_frames <- sys.frames()
## Some code that enables to get the value of the reactive value "rv"
## when the error occured (and save in a file "recover.txt" for instance).
## I expected that I could retrieve the reactive value in "previous_frames",
## but it seems not to be the case...
})
ui <- fluidPage(
tagList(
actionButton("increment", label = "Increment")
, textOutput("value")
, actionButton("do_error", "Create an error")
)
)
server <- function(input, output, session) {
rv <- reactiveVal(0)
observeEvent(input$increment, {
rv(rv() + 1)
})
output$value <- renderText({
rv()
})
observeEvent(input$do_error, {
stop("an error")
})
}
shinyApp(ui = ui, server = server)
您是否知道发生错误时(最好使用option)如何检索“有效值”(即用reactiveVal
初始化,在这种情况下为rv()
的值) shiny.error
)?
答案 0 :(得分:0)
我刚刚找到了一个简单的解决方案:将options
调用放在服务器函数中,然后直接在observer
中调用该值:
library(shiny)
ui <- fluidPage(
tagList(
actionButton("increment", label = "Increment")
, textOutput("value")
, actionButton("do_error", "Create an error")
)
)
server <- function(input, output, session) {
options(shiny.error = function() {
# I can retrieve the value :)
observe({
print(paste0("value = ",rv()))
})
})
rv <- reactiveVal(0)
observeEvent(input$increment, {
rv(rv() + 1)
})
output$value <- renderText({
rv()
})
observeEvent(input$do_error, {
stop("an error")
})
}
shinyApp(ui = ui, server = server)*