javascript confirm() for a button in shiny?

1,040 views
Skip to first unread message

Michael G.

unread,
Jan 28, 2014, 4:22:40 PM1/28/14
to shiny-...@googlegroups.com
Hi, was just wondering if there was support/some clever workaround for getting a confirmation window pop up for a button generated through shiny's actionButton function?

For example, after clicking the button have a small window pop up with "Are you sure?" and have the user confirm before, say, a time intensive function executes.

Thanks!
M

Michael G.

unread,
Jan 28, 2014, 5:22:57 PM1/28/14
to shiny-...@googlegroups.com
Oh I think I have something: Replacing a actionButton() with a includeHTML() call linking to a HTML file containing the a button code below worked for me... but remember to replace yourButtonID with yours. 

<button id="yourButtonID" type='button' class='btn action-button' onclick="return confirm('Are you sure?');" >Button</button>

I think my original question is now: there is no method in the current actionButton() function to put in that "onclick" attribute huh?

Thanks again
M
Message has been deleted

Michael G.

unread,
Jan 28, 2014, 5:40:36 PM1/28/14
to shiny-...@googlegroups.com
Ah, tags$button

tags$button( "Button Text", id="yourButtonID", type="button", class="btn action-button", onclick="return confirm('Are you sure?');" )

Patrick Toche

unread,
Jan 29, 2014, 9:11:26 AM1/29/14
to shiny-...@googlegroups.com
That's nice, I happen to see that at about the same time, just thought I'd mention it even though it looks like you have worked it out.

Michael G.

unread,
Jan 29, 2014, 11:51:53 AM1/29/14
to shiny-...@googlegroups.com
Thanks Patrick, I ran into another question: where does that value (TRUE/FALSE) of the confirm("Are you sure?") from the button get stored to?

It seems like it is not input$yourButtonID because that still remains numeric (the number of times clicked)...

M

Patrick Toche

unread,
Jan 29, 2014, 2:53:59 PM1/29/14
to shiny-...@googlegroups.com
well Michael, it's interesting you should ask, because I was asking myself the same question ...

I played around with the code Winston had in the link I mentioned, but I'm also missing that crucial piece of information.

I would have thought session$clientData$something  but couldn't work it out and didn't find much information out there.

Sorry I can't help. I'll be watching this space if something comes up.

# server.R

library("shiny")

shinyServer(
  function(session, input, output) {
      
      values <- reactiveValues(confirm = 0)
      
      observe({
          if (is.null(input$submit) || input$submit == 0){return()}
          js_confirm <- 'confirm("Do you want to submit now?");' # confirmation pop-up
          session$sendCustomMessage(type='jsCode', list(value = js_confirm))
      })
      
      observe({
          input$submit # this might be replaced by something like session$clientdata$etc.
          text <- isolate(input$inText)
          output$outText <- renderUI({
              h4(text)
          })
      })
      
  }
)


# ui.R

library("shiny")

shinyUI(
  basicPage(
    tags$head(
      tags$style(type='text/css', 
        "select, textarea, input[type='text'] {margin-bottom: 0px;}"
        , "#submit {
          color: rgb(255, 255, 255);
          text-shadow: 0px -1px 0px rgba(0, 0, 0, 0.25);
          background-color: rgb(189,54,47);
          background-image: -moz-linear-gradient(center top , rgb(238,95,91), rgb(189,54,47));
          background-repeat: repeat-x;
          border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
        }"
      ),
      tags$script(HTML('
          Shiny.addCustomMessageHandler("jsCode",
            function(message) {
              eval(message.value);
            }
          );'
      ))
    )
    ,
    textInput(inputId = "inText", label = "", value = "type text here")
    ,
    actionButton(inputId = "submit", label = "Submit")
#  
#   alternative approach: button with pop-up
#    , tags$button("Activate", id = "ButtonID", type = "button", class = "btn action-button", onclick = "return confirm('Are you sure?');" )
    ,
    tags$br()
    ,
    tags$hr()
    ,
    uiOutput("outText")
  )
)

Patrick Toche

unread,
Jan 29, 2014, 2:55:48 PM1/29/14
to shiny-...@googlegroups.com
the bit values <- reactiveValues(confirm = 0)  is left over from some experiments that didn't come off, it shouldn't be there anymore...

John Richardson

unread,
Jan 2, 2015, 6:05:05 PM1/2/15
to shiny-...@googlegroups.com
This works for me:

        myRet<<-tags$button( "Clear Log", id="clear", type="button", class="btn action-button", 
          onclick="val=confirm('Are you sure?');
          Shiny.onInputChange('confirm', val);"),

The value is now in input$confirm

Bob Tao

unread,
Feb 12, 2016, 6:46:25 PM2/12/16
to Shiny - Web Framework for R
This piece works in adding a confirmation step. However if error is caught by validation(need) in reactive block in server.R after submit,  it will remain in that state even after error is corrected and submit button is clicked again. 
Reply all
Reply to author
Forward
0 new messages