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.
It has been fixed since DT v0.3. See: https://github.com/rstudio/DT/issues/357
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’