0

In a modular shiny application, I have several tabs in which I use the datamods package to filter the data and display them with DT (renderDT). My data is quite large and is not displayed instantly. When profiling the application with the profviz package I noticed that retrieving the filters via datamods took a long time in ALL the tabs where these filters are used.
For example, if I'm on tab 1, in the background, the application has also loaded all the filters for the other tabs, which results in the table (DT) on tab 1 being displayed more slowly.
Is this normal?
Is there a way to load only the active tab?
Or how else?

ui & server :

library(shiny)
library(datamods)
library(profvis)
library(data.table)
library(DT)

fake_data = function(N) {
  col1 = rnorm(N)
  col2 = runif(N)
  col3 = sample(letters, N, replace = TRUE)
  col4 = rpois(N, lambda = 5)
  col5 = rnorm(N, mean = 50, sd = 10)
  col6 = sample(c(TRUE, FALSE), N, replace = TRUE)
  col7 = factor(sample(c("A", "B", "C"), N, replace = TRUE))
  col8 = as.Date("2023-06-27") + sample(0:365, N, replace = TRUE)
  col9 = sample.int(100, N, replace = TRUE)
  col10 = paste0("ID", 1:N)
  df = data.table(col1, col2, col3, col4, col5, col6, col7, col8, col9, col10)
  return(df)
}


app_ui <- function(request) {
  tagList(
    navbarPage("Shiny App",
               mod_1_ui("mod1"),
               mod_2_ui("mod2")))
}


app_server <- function(input, output, session) {
  r_global = reactiveValues()
  
  mod_1_server("mod1", r_global = r_global)
  mod_2_server("mod2", r_global = r_global)
}

example with 2 modules (2 tabs) :

mod_1_ui = function(id) {
  ns = NS(id)
  tabPanel("Tab 1",
           fluidRow(
             column(width = 3,
                    filter_data_ui(ns("filter_tab1"))),
             column(width = 9,
                    DT::DTOutput(outputId = ns("table_tab1")))
           ))
}


mod_1_server = function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    
    observe({
      r_global$df_tab1 = fake_data(100000)
    })
    
    #________________________________________________________________
    #                            TAB 1                           ----
    #________________________________________________________________
    
    data_tab1 <- reactive({
      r_global$df_tab1
    })
    ft1 = filter_data_server(id = "filter_tab1", data = data_tab1)
    
    observe({
      ft1$filtered()
      
      output$table_tab1 <- renderDT({
        datatable(ft1$filtered())
      })
    })    
  })
}



mod_2_ui = function(id) {
  ns = NS(id)
  
  tabPanel("Tab 2",
           fluidRow(
             column(width = 3, filter_data_ui(ns("filter_tab2_1"))),
             column(width = 9, DT::DTOutput(outputId = ns("table_tab2_1")))
           )
  )
}


mod_2_server = function(id, r_global) {
  moduleServer(id, function(input, output, session) {
    #________________________________________________________________
    #                            TAB 2                           ----
    #________________________________________________________________
    
    data_tab2_1 = reactive({
      r_global$df_tab1
    })
    
    ft21 = filter_data_server(id = "filter_tab2_1", data = data_tab2_1)

    observe({
      ft21$filtered()

      output$table_tab2_1 <- renderDT({
        datatable(ft21$filtered())
      })
    })
    
  })
}

Launch app (a golem app) :

source("app_ui.R")
source("app_server.R")
source("mod_1.R")
source("mod_2.R")
shinyApp(ui = app_ui,  server = app_server)

Here is the profvis report : enter image description here enter image description here

On profvis we can see that the filter on inactive tab 2 takes as long as the filter on active tab 1

nimliug
  • 349
  • 1
  • 11

0 Answers0