shiny module to generate DT object with interactions based on events

Viewed 124

I'm building a shiny app that has multiple "editable" tables where the user will be able to add new rows, edit existing ones, and write back this data to a data source.

I've been writing in modules to keep code split up so I have a module for "users" and I'd like to have the user table leverage the "editableDT" module.

Based on Yihui's shiny-proxy example I have the table and the Add button showing up but it seems the observe event isn't triggering.

How does this module need to be amended to so that the inner module is able to observe the button press?

Gist of code

Code included for ease of reading:

editableDT <- function(input
                       , output
                       , session
                       , table) {
  output$table <- DT::renderDataTable(table(),
                                      server = FALSE)

  proxy <- DT::dataTableProxy("table")

  observeEvent(input$add, {
    proxy %>% addRow(table()[1, ])
  })

}

editableDTInput <- function(id) {
  ns <- NS(id)
  tagList(actionButton(ns("add"), "Add Row"))
}

users <- function(input
                  , output
                  , session) {
  users <- reactive({
    iris
  })

  user <- callModule(editableDT, "usertbl", users)
  output$tblui <- renderUI(tagList(editableDTUI(session$ns("usertbl"))))
}

usersUI <- function(id) {
  ns <- NS(id)
  tagList(editableDTInput("usertbl"),
          uiOutput(ns("tblui")))
}

ui <- function() {
  fluidPage(usersUI("sample"))
}

server <- function(input, output, session) {
  callModule(users, "sample")
}

shinyApp(ui, server)
1 Answers

You didn't provide editableDTUI. I assume it is

editableDTUI <- function(id) {
  ns <- NS(id)
  DTOutput(ns("table"))
}

The app works after replacing

usersUI <- function(id) {
  ns <- NS(id)
  tagList(editableDTInput("usertbl"),
          uiOutput(ns("tblui")))
}

with

usersUI <- function(id) {
  ns <- NS(id)
  tagList(editableDTInput(ns("usertbl")),
          uiOutput(ns("tblui")))
}
Related