freshman freshman - 25 days ago 10
R Question

Shiny: how to create a confirm dialog box

I would like to ask if it is possible to have a confirm dialog box, consisting of two buttons, in shiny.
Say, if I click a Delete button, then the dialog box pop up. User pick the choice and return. The app acts according to the user choice.

Answer

I modified part of your code to call

js_string <- 'confirm("Are You Sure?");'
session$sendCustomMessage(type='jsCode', list(value = js_string))

to call the confirm dialog instead of alert dialog box. Then

tags$script(
            HTML('
                Shiny.addCustomMessageHandler(
                    type = "jsCode"
                    ,function(message) {
                    Shiny.onInputChange("deleteConfirmChoice",eval(message.value));
                })
            ')
)

to send the value returned by the confirm dialog box. Then I just checeked the value of input$deleteConfirmChoice to determine what action is to be done. Thank you very much! I now understand how to send and receive messages to and from R and Javascript.