在 R shiny 中重置 shinyalert 的输入

Reset the input for shinyalert in R shiny

它只会在我第一次点击时附加,因为 input$shinyalertTRUE 我点击后就可以了。有没有办法重置 shinyalert 的输入,以便在我按下操作按钮时重新触发我的观察 second/third.. 在会话中的时间。

我试图将 NULL/FALSE/0 分配给 input$shinyalert (input$shinyalert <- NULL) 但它给了我这个错误。

Warning: 
Error in $<-.reactivevalues: 
Attempted to assign value to a read-only reactivevalues object

这是我的代码

# observe event for my action button
observeEvent(input$action, {
      shinyalert("","Are you sure?", type="warning", showCancelButton = TRUE)
      })

observe({
      req(input$shinyalert)
      isolate({
        newrow <- data.frame(a = "fisrt",
                             b = "second",
                             c = "third")

      # appending to mytable in SQL server
      dbWriteTable(conn, "mytable", newrow, append = TRUE)
      })
    })

你不能像这样使用 R 回调函数吗:

library(shiny)
library(shinyalert)

ui <- fluidPage(
  useShinyalert(),
  actionButton("btn", "Append row")
)

server <- function(input, output, session){

  appendTable <- function(){
    newrow <- data.frame(a = "fisrt",
                         b = "second",
                         c = "third")
    dbWriteTable(conn, "mytable", newrow, append = TRUE)
  }

  observeEvent(input[["btn"]], {
    shinyalert("", "Are you sure?", type = "warning", showCancelButton = TRUE, 
               callbackR = function(x){
                 if(x) appendTable()
               })
  })

}

shinyApp(ui, server)

否则,要重置,您可以使用 JS 回调:

  observeEvent(input[["btn"]], {
    shinyalert("", "Are you sure?", type = "warning", showCancelButton = TRUE, 
               callbackJS = "function(x){
                 setTimeout(function(){Shiny.setInputValue('shinyalert', false);}, 0);
               }")
  })