Home > Net >  Shiny - Data Table - click on actionButton twice
Shiny - Data Table - click on actionButton twice

Time:12-10

I've this Shiny App:

library(shiny)
library(DT)


ui <- fluidPage(
  fluidRow(
    DT::dataTableOutput(outputId = "my_data_table"),
  )
)


server <- function(input, output) {
  
  myValue <- reactiveValues(check = '')
  
  shinyInput <- function(FUN, len, id, ...) {
    inputs <- character(len)
    for (i in seq_len(len)) {
      inputs[i] <- as.character(FUN(paste0(id, i), ...))
    }
    inputs
  }
  
  
  my_data_table <- reactive({
    tibble::tibble(
      Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
      Motivation = c(62, 73, 3, 99, 52),
      Actions = shinyInput(actionButton, 5,
                           'button_',
                           label = "Fire",
                           onclick = paste0('Shiny.onInputChange( \"select_button\" , this.id)') 
      )    
    )
  })
  
  output$my_data_table <- renderDataTable({
    my_data_table()
  }, escape = FALSE)
  
  
  observeEvent(input$select_button, {
    print(input$select_button)
  })
  
  
  
  
}

shinyApp(ui, server)

Every time I click on "fire" - it is print the row number of the button. It is works well, but if I click on the some button twice - it is only print the first click. I want to print it every time I click on the button, also if I click N times on the some row.

any suggestion will be welcome..

CodePudding user response:

Communicating with Shiny : you need to switch the priority from values to event. Otherwise it assumes, because the value of your message hasn't changed (i.e. the id of the button), no message gets sent. There is third argument to Shiny.onInputChange which takes an options object (e.g. {priority: "event"}) which will send a message everytime the button is clicked, even the same id twice in a row.

onclick js code should be

onclick = paste0('Shiny.onInputChange( \"select_button\" , this.id, {priority: "event"})') 
  • Related