Search code examples
rshinydtshinymodules

editable DT inside shiny module


I would like to have an editable DT inside a shiny module. When I change a value in the DT, then the table updates and it is empty with the message inside the datatable:

"No matching records found"

My code is as follows:

Modules:

modDtUi <- function(id){ # UI module
  ns = NS(id)
  DT::dataTableOutput(ns('x1'))
} 


modDt <-  function(input, output, session, data){ # Server module

  x <- data
  output$x1 <- DT::renderDataTable(x, selection = 'none', editable = TRUE)

  proxy <- dataTableProxy('x1', session = session)

  observeEvent(input$x1_cell_edit, {
    info = input$x1_cell_edit
    str(info)
    print(info)
    i = info$row
    j = info$col
    v = info$value
    x[i, j] <<- DT::coerceValue(v, x[i, j])
    replaceData(proxy, x, resetPaging = FALSE, rownames = FALSE)
  })

}

app in flexdashboard:

```{r}
modDtUi("editable")
```

```{r}
callModule(modDt,"editable", data = iris)
```

It works well without modules, but I can't get the same results with shiny modules.

Thanks


Solution

  • Working from your code, the issue is that the proxy needs the global session (and not the module session). See my other answer for an alternative approach.

    You can simply pass the global session to the module via an argument.

    This works:

    library(shiny)
    library(DT)
    
    modDtUi <- function(id){ # UI module
      ns = NS(id)
      DT::dataTableOutput(ns('x1'))
    }
    
    
    modDt <-  function(input, output, session, data, globalSession){ # Server module
    
      x <- data
      output$x1 <- DT::renderDataTable(x, selection = 'none', editable = TRUE)
    
      proxy <- dataTableProxy('x1', session = globalSession)
    
      observeEvent(input$x1_cell_edit, {
        info = input$x1_cell_edit
        str(info)
        print(info)
        i = info$row
        j = info$col
        v = info$value
        x[i, j] <<- DT::coerceValue(v, x[i, j])
        replaceData(proxy, x, resetPaging = FALSE)
      })
    
    }
    

    You now have to add the global session in the module call.

    With a shiny app:

    ui <- fluidPage(
      modDtUi("editable")
    )
    
    server <- function(input, output, session) {
      callModule(modDt,"editable", data = iris, globalSession = session)
    }
    
    shinyApp(ui = ui, server = server)
    

    With a flexdashboard:

    ```{r}
    modDtUi("editable")
    ```
    
    ```{r}
    callModule(modDt, "editable", data = iris, globalSession = session)
    ```
    

    If you want to use your updated table in the rest of your app, simply return reactive(x) from your module and capture it when you call the module.

    editable_iris <- callModule(modDt,"editable", data = iris, globalSession = session)