Namespace issue with shiny.onInputchange in Shiny module

Hi,
I am trying to embed action buttons in a datatable but I have no clue where to insert this session$ns or "ns", which should be somewhere in the getRemoveButton or ShinyInput functions below.

Thanks in advance

library(DT)
library(shiny
library(dplyr)

getRemoveButton <- function(n, idS = "", lab = "Pit") {
  if (stringr::str_length(idS) > 0) idS <- paste0(idS, "-")
  ret <- shinyInput(actionButton, n,
                    'button_', label = "Remove",
                    onclick = sprintf('Shiny.onInputChange(\"%sremove_button_%s\",  this.id)' ,idS, lab))
  return (ret)
}

shinyInput <- function(FUN, n, id, ses, ...) {
  as.character(FUN(paste0(id, n), ...))
}


tablebuttUI <- function(id) {
  ns <- NS(id)
  tagList(
    fluidRow(DT::dataTableOutput(ns("myTable")))
  )
}

tablebutt <- function(input, output, session) {
    
  values <- reactiveValues()
  values$tab <- tibble(
    Row = 1:3L,
    id = 1:3L) %>%
    rowwise() %>%
    mutate(Remove = getRemoveButton(id, idS = "", lab = "Tab1"))
  
  proxyTable <- DT::dataTableProxy("tab")
  
  output$myTable <- DT::renderDataTable({
    DT::datatable(values$tab,
                  options = list(pageLength = 25,
                                 dom        = "rt"),
                  rownames = FALSE,
                  escape   = FALSE,
                  editable = TRUE)
  })
  
  observeEvent(input$remove_button_Tab1, {
    myTable <- values$tab
    s <- as.numeric(strsplit(input$remove_button_Tab1, "_")[[1]][2])
    myTable <- filter(myTable, id != s)
    replaceData(proxyTable, myTable, resetPaging = FALSE)
    values$tab <- myTable
  })

}
  
shinyApp(
  ui = fluidPage(
    tablebuttUI("test")
  ),
  
  server = function(input, output) {
    callModule(tablebutt, "test")
  }
)
1 Like

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