2

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)
      })
    })
Elaine Sea
  • 43
  • 5
  • Have you tried installing the latest version of shinyalert form github? Without a full code sample it's hard for me to see exactly what you think is broken, but I think the non-CRAN version will fix your issue. If it doesn't, please edit your post to include the code for a full shiny app – DeanAttali Feb 15 '20 at 20:49

1 Answers1

0

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);
               }")
  })
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225