Reactable onClick not working within module, even when properly namespaced

Hello Shiny folks!
I am super frustrated because I know I have solved this problem before but for whatever reason am having a devil of a time with it, I feel like I have tried everything and am out of ideas.

I have a modular/Golem Shiny app, where at one point I want to be able to click in a Reactable and have it pop up a modal with information about that row (i.e. set the "modalId" so to speak to be a component from that row).
I have successfully done this before several times but on a computer/code that I no longer have access to.

For whatever reason, I can't get ANY onClicks to work right now - I thought it might be a bootstrap thing, since the bootswatch theme I was using made a shinyWidgets::pickerInput not work at all, but even after getting rid of the theme it still doesn't work.

I have tried every way I possibly can in terms of getting the namespace to work, and nothing has worked - it seems like it isn't registering the click at all.
I am using logger to log input changes and when I click nothing happens at all, no matter what I do - even if I ignore the namespace and use a global variable (through sendCustomMessage)

Any ideas? I have been banging my head against this for 2 days now and feel like it has to be something really simple/stupid that I just keep not catching. I would appreciate any help, you'd be my hero! Thank you!
MVE is below, does this work for anyone else?:

mod_list_test_ui <- function(id){
  ns <- NS(id)

  tagList(
    fluidPage(
# tried, doesn't work
#       tags$head(
#         tags$script(paste0('Shiny.addCustomMessageHandler("testmessage",
#   function(message) {
#     Shiny.setInputValue("',ns("foo"),'",message);
#   }
# );'))
# ),
      
      fluidRow(
                                shinyWidgets::pickerInput(inputId = ns("shinySelector2"),
                                            label = "this works",
                                            choices = c("woof","meow","beep"),
                                            selected = "beep",
                                            multiple = TRUE) 
                                
      ),
      verbatimTextOutput(ns("fooOutput")),
      reactableOutput(ns("tableInitial"))
    )
  )
}


mod_list_test_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    
##tried, doesn't work
    # session$sendCustomMessage(type = "testmessage",
    #                           message = "initial")

#have tried every version of this - putting it inline, separately, ns, session$ns, no ns (master variable through the above), manually putting in the namespace - and no dice. have tried to get it to even just make a window alert or a console log and it won't do that either
    jsCode <- paste0('function(rowInfo, column) {
    Shiny.setInputValue("',ns("foo"),'","howdy");
  };')
 
    output$tableInitial <- renderReactable({
      data.frame(animals = c("cow","pig","chicken"),
                 people = c("david","pete","bob"),
                 stringsAsFactors = F) %>%
        reactable(
          onClick = JS(jsCode)
        )
    })

    output$fooOutput <- renderText({
      if (is.null (input[["foo"]]) ){
        "nothing"
      }else{
        input[["foo"]]
      }
    })
    
    
  })
}

In case this helps anyone debug, even doing a one-file test app with the demo reactable onClick action doesn't work for me. Can someone confirm if this below works for them (gives an alert, or prints to the console on click), otherwise something might be wrong with my JS or something!
thank you!

library(shiny)
library(shinyWidgets)
library(reactable)
library(logger)

ui <- fluidPage(
  reactableOutput("reactableTest")
)

server <- function(input, output) {
  logger::log_shiny_input_changes(input)
  output$reactableTest <- renderReactable({
    data <- cbind(
      MASS::Cars93[1:5, c("Manufacturer", "Model", "Type", "Price")],
      details = NA
    )
    
    reactable(
      data,
      columns = list(
        # Render a "show details" button in the last column of the table.
        # This button won't do anything by itself, but will trigger the custom
        # click action on the column.
        details = colDef(
          name = "",
          sortable = FALSE,
          cell = function() htmltools::tags$button("Show details")
        )
      ),
      onClick = JS("function(rowInfo, column) {
    // Only handle click events on the 'details' column
    if (column.id !== 'details') {
      return
    }

    // Display an alert dialog with details for the row
    window.alert('Details for row ' + rowInfo.index + ':\\n' + JSON.stringify(rowInfo.values, null, 2))

    // Send the click event to Shiny, which will be available in input$show_details
    // Note that the row index starts at 0 in JavaScript, so we add 1
    if (window.Shiny) {
      Shiny.setInputValue('show_details', { index: rowInfo.index + 1 }, { priority: 'event' })
    }
  }")
    )
  })
  
  observeEvent(input$show_details,{
    print(input$show_details)
  })
}

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.