Shinyalert capturing the callback

Hi,

I'm exploring the package shinyalert and im trying to capture the value of TRUE when a user presses OK and FALSE when they press cancel. Below is the code taken straight from the docs where i have just added a cancel button. I just want a very simple example of if the user presses OK, the code will cat(x) which will be TRUE otherwise it will cat(x) as false.

The below is a working example where pressing the button cause the pop up to appear

# Working Example
if (interactive()) {
  library(shiny)
  library(shinyalert)
  
  shinyApp(
    ui = fluidPage(
      useShinyalert(),  # Set up shinyalert
      actionButton("btn", "Click me")
    ),
    server = function(input, output) {
      observeEvent(input$btn, {
        # Show a simple modal
        shinyalert(title = "You did it!", type = "success")
      })
    }
  )
}

Created on 2018-09-26 by the reprex package (v0.2.0).

If i try to add code to capture the variable i get

# When i Try to add an input
if (interactive()) {
  library(shiny)
  library(shinyalert)
  
  shinyApp(
    ui = fluidPage(
      useShinyalert(),  # Set up shinyalert
      actionButton("btn", "Click me")
    ),
    server = function(input, output) {
      observeEvent(input$btn, {
        # Show a simple modal
        shinyalert(title = "You did it!", type = "success",
                   type = "input",
                   callbackR = function(x) { if(x != FALSE) message("Hello ", x) })
      })
    }
  )
}

Created on 2018-09-26 by the reprex package (v0.2.0).

error message Warning: Error in shinyalert: formal argument "type" matched by multiple actual arguments
[No stack trace available]

The documentation can be found here and they look great so i would love to get them working

Thanks for your time

Hi All,

I should have stuck at it a bit longer before posting
Answer is below. type = 'input', is only if you want to actually type something into the message box

# Working Example
if (interactive()) {
  library(shiny)
  library(shinyalert)
  
  shinyApp(
    ui = fluidPage(
      useShinyalert(),  # Set up shinyalert
      actionButton("btn", "Click me")
    ),
    server = function(input, output) {
      observeEvent(input$btn, {
        # Show a simple modal
        shinyalert(title = "The Big Short",showCancelButton = TRUE, showConfirmButton = TRUE,
          callbackR = function(x) { if(x != FALSE) cat("Hello ", x) }
        )
      })
    }
  )
}

Created on 2018-09-26 by the reprex package (v0.2.0).

Thank you for your time

3 Likes