Unable to read excel data after Shiny Modularization

All I am trying to do is read, render and download excel. Not sure how we can display specific UI details such as uploading excel button in dashboard sidebar and rendering and download button in dashboard body

Only error when I tried to get rid this error in mod_exampleUI module function.
Error in mod_example("example_mod") :
argument "output" is missing, with no default

Please find the code below

library(shiny)
library(magrittr) # Load magrittr for the piping operator %>%
library(DT)
library(readxl)
library(tidyselect)
library(writexl)
library(dplyr)
library(tidyr)
library(readxl)
library(stringr)
# Increase band width for shiny to handle bigger file 
options(shiny.maxRequestSize=300*1024^2) 

# Module UI to display sidebar content
mod_exampleUI <- function(id) {
  ns <- shiny::NS(id)
  shiny::tagList(

    fileInput(ns("file1"), "Choose XLSX File (Convert xls to xlsx)",accept=c(".xlsx")),
    tags$hr(),
    downloadButton(ns("downloadData"), "Download")
  )
}

# Module UI to display Body content
mod_example_displayUI <- function(id) {
  ns <- shiny::NS(id)
  shiny::tagList(
    DT::dataTableOutput(ns("contents"))
  )
}

# Function to read all excel sheet necessary
mod_example_display <- function(input, output, session) {
  output$contents <- DT::renderDataTable({
    DT::datatable(readxl::read_excel(input$file1$datapath)
                  ,options = list(pageLength = 7,scrollX = TRUE))
  })
  
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("updated file dated-", Sys.Date(), ".xlsx")
    },
    
    content = function(file) {
      write_xlsx(DT::datatable(readxl::read_excel(input$file1$datapath),file))
    }
  )
}

ui <- fluidPage(
  
  shinydashboard::dashboardPage(
    skin = "yellow",
    # HEADER -----
    shinydashboard::dashboardHeader(
      title = "Modularizing App"
    ),
    # SIDEBAR -----
    shinydashboard::dashboardSidebar(
      shinydashboard::sidebarMenu(
        shinydashboard::menuItem('Example', tabName = 'example', icon = shiny::icon('file')),
        shinydashboard::tabItems(
          shinydashboard::tabItem("example", mod_exampleUI("example_sidemod"))
        )
      ) 
    ),
    # BODY -----
    shinydashboard::dashboardBody(
      shiny::tags$head(shiny::tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")),
      shinydashboard::tabItems(
        shinydashboard::tabItem("example", mod_example_displayUI("example_bodymod"))
      )
    )
  )
)

server <- function(input, output) {
    shiny::callModule(mod_example_display, "mod_example")
}

shinyApp(ui,server)

I think you have a bit of a broken module implementation here, which I suspect is causing your problem.

In short, my understanding of modules (which is certainly not 100% accurate) is that each module needs a server AND a UI function (much like a Shiny application does).

In the server side of the parent application, you call the server side of the module. In the UI side of the parent application, you call the UI side of the module. You tie these together with the "ID."

In your app, you call mod_exampleUI("example_sidemod"), mod_example_displayUI("example_bodymod") in the UI, and callModule(mod_example_display, "mod_example") in the server.

Nothing is tied together because none of the IDs match. Further, there is mismatch in the number of UI vs. server side modules.

Basically, what you want is:

  • call mod_example_displayUI("mod_example") in the UI and callModule(mod_example_display, "mod_example") in the server
  • call mod_exampleUI("example_sidemod") in the UI and callModule(mod_example_display, "example_sidemod") in the server... or some other server module

I haven't checked your modules themselves here to see if they actually work, but generally each server component needs a UI component and vice versa. Again, you tie those together with the IDs :smiley: Hope that helps!

1 Like

Thanks for suggestion. I had tried different approach with and without modularization in order to compare both.

Although this has tie between module Server and UI but not unique.

Thanks for suggestion.

Please find a solution

1 Like

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