Sourcing modules

We are suddenly having problems with apps using modules that are stored in the 'R' subdirectory. These are not being sourced at launch, it needs to be forced. The following app does not now work unless lines 3 & 4 are uncommented. We are using shiny 1.7.1 and R 4.1.2. Any advice please?

library(shiny)

# modules <- list.files("R")
# sapply(paste0("R/", modules), source)

ui <- fluidPage(

    # Application title
    titlePanel("test app"),

    sidebarLayout(
        sidebarPanel(
          selectInput("tabs","Section", choices = c("a"))
        ),
        mainPanel(
          uiOutput("main")
        )
    )
)

server <- function(input, output) {
  
  vals <- reactiveValues(
    selected_tab_new = NULL,
    selected_tab_old = NULL
  )
  
  observeEvent(input$tabs, {
    vals$selected_tab_new <- input$tabs
    vals$selected_tab_old <- input$tabs
  })
  
  observe({
    
    if (!is.null(vals$selected_tab_new)) {
      output$main <- renderUI({
        rlang::as_function(paste0(vals$selected_tab_new, "UI"))(vals$selected_tab_new)
      })
      rlang::as_function(paste0(vals$selected_tab_new, "Server"))(vals$selected_tab_new)
      
    }
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

Module code

  
  ns <- NS(id)
  
  fluidPage(
    
    # shinyjs::useShinyjs(),
  
    fluidRow(
      
      column(width = 12, 
             
             h2("Module A")
             
      )
      
    )
  
  )
  
}

aServer  <- function(id) {
  moduleServer(id, function(input, output, session, x) {
  
  
  })
  
  
}```

Hi!

Maybe I misread your code, but to me it seems there are 2 things going on:

  1. You are not actually using any of your module functions (e.g. aServer) on your main server/UI functions.
  2. Is there any specific reason to think that you don't need to source the module scripts?

Hi agus, thanks for replying.

It's deliberately a minimum reproducible example, so the module server functions are empty, but the problem is that that module itself is not being automatically sourced on startup, which is the expected Shiny behaviour. See https://shiny.rstudio.com/articles/modules.html#:~:text=In%20an%20R%20script%20in%20the%20R/%20subdirectory

Ah, I see, I wasn't aware of this behavior.
Ok, so I tested it in shiny v1.5.0 with a minimal app and it worked :white_check_mark: . Namely, I placed a module file test-module.R in app/R/, while my app file is living in app/.

So, again 2 things come to mind in your example:

  • there might be an issue with relative paths (check whether you have the same structure as I used in my test)
  • you have no way to check whether your module is being sourced since you are not using it in your app (as I pointed out in my previous comment).

For the second issue, try adding a function in your module script that prints sth to console, and add a call to this function in your app server.

HTH

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.