Shiny DT row action button do not fire the event if clicked twice

Hi there,
I need to add a custom button on specific DT rows. Everything works fine, except for the fact that once you make a click on a row, Shiny on the Server side does not accept more clicks from the same button. If I click on a button in a different row, everything works, but I cannot click twice on the same button. The event is not fired.
Any suggestions?

Here is a snippet of code.

library(shiny)
library(DT)
library(dplyr)
library(tidyr)

data <- data.frame(
col1 = c("Total", "Account", "Account", "Total"),
col2 = c(1, 2, 3, 4)
)

data <- data %>%
mutate(
button = case_when(
col1 == "Account" ~ sprintf(
'Click me',
col2, "Shiny.setInputValue('button', this.id);"),
TRUE ~ NA_character_
)
)

ui <- fluidPage(
fluidRow(
column(
width = 6,
DTOutput("tbl1", height = "500px")
)
)

)

server <- function(input, output){

output[["tbl1"]] <- renderDT({
datatable(data, escape = F, fillContainer = TRUE, selection = 'none')
})

observeEvent(input[["button"]], {
print('fired')
splitID <- strsplit(input[["button"]], "_")[[1]]
row <- splitID[2]
showModal(modalDialog(
title = paste0("Row ", row, " clicked"),
size = "s",
easyClose = TRUE,
footer = NULL
))
})
}

shinyApp(ui, server)

Dear All, I found the solution thanks to this post

I am posting the solution for anyone interested in solving the same problem.

As the post says: The observeEvent(input$select_button...,{}) is triggered only when input$select_button change of value. BUT if you click twice on the same row, it doesn't change it's value because its value is based on the row. You need to find another way to define the button id I think.

So, to solve the error, change the this.id part of the code by, for example, adding the date of firing.

Here is the working code:
library(shiny)
library(DT)
library(dplyr)
library(tidyr)

data <- data.frame(
col1 = c("Total", "Account", "Account", "Total"),
col2 = c(1, 2, 3, 4)
)

data <- data %>%
mutate(
button = case_when(
col1 == "Account" ~ sprintf(
'Click me',
col2, "Shiny.setInputValue('button', this.id + '' + Date.now());"),
TRUE ~ NA_character

)
)

ui <- fluidPage(
fluidRow(
column(
width = 6,
DTOutput("tbl1", height = "500px")
)
)

)

server <- function(input, output){

output[["tbl1"]] <- renderDT({
datatable(data, escape = F, fillContainer = TRUE, selection = 'none')

})

observeEvent(input[["button"]], {
splitID <- strsplit(input[["button"]], "_")[[1]]
print(splitID)
row <- splitID[2]
dateNow <- splitID[3]
showModal(modalDialog(
title = paste0("Row ", row, " clicked"),
size = "s",
easyClose = TRUE,
footer = NULL
))
})
}

shinyApp(ui, server)

This topic was automatically closed 54 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.