For loop in observe Event

Hi all,

I am trying to create a action button to all rows in the datatable (This is just a sample. But actual table contains lots of rows). For, I have hard coded the observe event . Since I am facing some issues here. The output I am getting now is only when clicked on first row action button, the observe event is triggering but I need this trigger to happen when clicked on all rows. (Basically a for loop)

library(shiny)
library(shinydashboard)
library(DT)
library(glue)
number_compare <- data.frame(replicate(2, sample(1:100, 10, rep=TRUE)))


sidebar <- dashboardSidebar()

body <- dashboardBody(
  fluidRow(box(width = 12, solidHeader = TRUE,
               DTOutput("example_table"))
  )
)

ui <- dashboardPage(dashboardHeader(title = "Example"),
                    sidebar,
                    body
)


server <- function(input, output) {
  

  
  number_compare <- number_compare %>% mutate(rn = row_number(), button = glue::glue(HTML('<button id="button{rn}" type="button" class="btn btn-default action-button">Ask a question</button>')))
  
  output$example_table <- DT::renderDT({
    datatable(
      number_compare,
      escape = FALSE
      ,options=list(preDrawCallback=JS(
        'function() {
     Shiny.unbindAll(this.api().table().node());}'),
        drawCallback= JS(
          'function(settings) {
       Shiny.bindAll(this.api().table().node());}')))
  })
  
  observeEvent(input[[paste0("button",number_compare["rn"][1,])]],{
  print("Cleicked")
  })

  
  
  
}

shinyApp(ui, server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.