0

In an R Shiny application, I am trying to use DT::replaceData to update the data to show with current state (e.g. filtering) preserved. While it works with a simple shiny app, it does not when I modularize the app and invoke from callModule.

In the example below, choosing species in the top box is supposed to trigger replacement of data to show below.

Here is a working example:

library(shiny)

ui <- fluidPage(
  selectInput('species', 'Choose Species',
              choices=unique(iris$Species),
              selected=unique(iris$Species), multiple=TRUE),
  DT::dataTableOutput('dt')
)

server <- function(input, output, session) {
  output$dt <- DT::renderDataTable({
    DT::datatable(
      iris, filter='top',
      options = list(autoWidth=TRUE)
    )
  })

  observeEvent(is.null(input$species), {
    DT::replaceData(
      DT::dataTableProxy('dt'),
      dplyr::filter(iris, Species %in% input$species)
    )
  })
}

shinyApp(ui, server)

And this is the modularized version that is not working:

library(shiny)

ui <- function(id) {
  ns <- NS(id)
  tagList(
    selectInput(ns('species'), 'Choose Species',
                choices=unique(iris$Species),
                selected=unique(iris$Species), multiple=TRUE),
    DT::dataTableOutput(ns('dt'))
  )
}

server <- function(input, output, session) {
  output$dt <- DT::renderDataTable({
    DT::datatable(
      iris, filter='top',
      options = list(autoWidth=TRUE)
    )
  })

  observeEvent(is.null(input$species), {
    print(input$species)
    DT::replaceData(
      DT::dataTableProxy('dt'),
      dplyr::filter(iris, Species %in% input$species)
    )
  })
}


mainUi <- fluidPage(ui('app'))
mainSrv <- function(input, output, session) {
  callModule(server, 'app')
}
shinyApp(mainUi, mainSrv)

I would like to know why the second example does not work, and how to fix it if possible.


Update

Solved!

It has been fixed since DT v0.3. See: https://github.com/rstudio/DT/issues/357

Kota Mori
  • 6,510
  • 1
  • 21
  • 25
  • 1
    I think the problem is in that while creating a proxy you use "dt" and the real output identifier is a namespace + id ("app-dt" in your case). At least this is what the javascript console in the browser is showing. Unfortunately this just answers the "Why" part as simply making it "app-dt" does not have any effect except of getting rid of the browser warning... – Mikolaj Mar 01 '18 at 18:58
  • @Mikolaj Turns out, I was using old version of the package. See the update. Thanks. – Kota Mori Mar 02 '18 at 12:40
  • Great to hear! I was using 0.2 as well... :) – Mikolaj Mar 02 '18 at 14:10

1 Answers1

0

It has been solved since v3.0. Reference: https://github.com/rstudio/DT/issues/357

So, simply solved by:

install.packages('DT')
packageVersion('DT')
# [1] ‘0.4’
Kota Mori
  • 6,510
  • 1
  • 21
  • 25