0

Why is the observeEvent() not getting triggered when i use tags$button with a corresponding id, but it is when i use actionButton with an inputId. Where is the difference and how can i make my custom html tags$button to be an actual "event"?

This does not work:

library(shiny)
if (interactive()) {
  options(device.ask.default = FALSE)
  
  ui = fluidPage(
    
    tags$button(id = "action", "BTN"),
    plotOutput(outputId = "plot"))    
  
  server = function(input, output, session){
    
    observeEvent(input$action, {
      
      output$plot = renderPlot({
        hist(iris$Sepal.Length)})
    })
  }
  shinyApp(ui, server)
}

This does:

library(shiny)
if (interactive()) {
  options(device.ask.default = FALSE)
  
  ui = fluidPage(
    
    actionButton(inputId = "action", label = "BTN"),
    plotOutput(outputId = "plot"))    
  
  server = function(input, output, session){
    
    observeEvent(input$action, {
      
      output$plot = renderPlot({
        hist(iris$Sepal.Length)})
    })
  }
  shinyApp(ui, server)
}
werN
  • 109
  • 7

1 Answers1

1

It works if you add the action-button class to your button

tags$button(id = "action", "BTN", class="action-button")
gdevaux
  • 2,308
  • 2
  • 10
  • 19
  • this really works :) what is class "action-button" doing with my button under the hood? – werN Jul 29 '21 at 09:27
  • It transforms it to an event listener. You can see the difference by inspecting the HTML of the button in your browse. – gdevaux Jul 29 '21 at 09:30
  • and I guess if I don't use this `class = 'action-button'` - workaround, I'll have to do a lot of manual javascript programming, right? – werN Jul 29 '21 at 10:05
  • I honestly have no idea – gdevaux Jul 29 '21 at 13:47