简体   繁体   中英

Reset the input for shinyalert in R shiny

It will only append the first time I click because input$shinyalert is TRUE after I hit okay. Is there a way to reset the input for shinyalert so that it will re-trigger my observe when I hit action button the second/third.. time in a session.

I tried to assign NULL / FALSE / 0 to input$shinyalert ( input$shinyalert <- NULL ) but it gave me this error.

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

Here is my code

# 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)
      })
    })

Can't you use a R callback function, like this:

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)

Otherwise, to reset, you can use a JS callback:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM