rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
598 stars 181 forks source link

Capturing `input$*_cell_edit` of a top level-rendered `DT` for observation/reaction in a module namespace? #1150

Open r-cheologist opened 1 month ago

r-cheologist commented 1 month ago

This is aan adapted re-posting at the source of DT of this question. I remain stuck.

I am aiming to extend this example, which implements a shiny-rendered DT including the capture of any edits done to the rendered data.

My version is to split off datatable handling into a shiny module, but render the result in the toplevel namespace. In the above stackexchange.com issue, I have gotten introduced to session[["userData"]] and based on that manage to display the module-provided DT on the top namespace level.

I fail, however, to capture the input$*_cell_edit in a corresponding session[["userData"]] object for further reaction in the module. How would I go about this?

Minimal code demonstrating (the rendering side of) the issue follows:

requireNamespace("shiny")
requireNamespace("DT")

datatableUI <- function(id = "datatable")
{
  shiny::tagList(shiny::sidebarPanel(shiny::uiOutput("LocalTextPlaceholder")))
}

ui <- shiny::fluidPage(
  shiny::titlePanel("Module Version"),
  shiny::sidebarLayout(
    shiny::sidebarPanel(shiny::uiOutput("TextPlaceholder")),
    shiny::mainPanel(
      datatableUI(),
      shiny::uiOutput("DTPlaceholder"))))

datatableServer <- function(id = "datatable")
{
  shiny::moduleServer(id, function(input, output, session)
  {
    session$userData$DT <- DT::datatable(
      iris, selection = 'none', editable = TRUE, rownames = TRUE, extensions = 'Buttons',
      options = list(
        paging = TRUE, searching = TRUE, fixedColumns = TRUE,   autoWidth = TRUE, ordering = TRUE,
        dom = 'Bfrtip', buttons = c('csv', 'excel')),
      class = "display")
  })
}

server <- function(input, output, session) {
  datatableServer()
  shiny::observeEvent(
    session$userData$DT,
    {
      output$DTPlaceholder <- shiny::renderUI(session$userData$DT)
    }
  )
}

# Run the application
shinyApp(ui = ui, server = server)

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.