werN
werN

Reputation: 129

Creating shiny outputs from custom html inputs

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)
}

Upvotes: 0

Views: 206

Answers (1)

gdevaux
gdevaux

Reputation: 2505

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

tags$button(id = "action", "BTN", class="action-button")

Upvotes: 1

Related Questions