Hi Shiny Experts,
Please find the below working unmodularized and modularized non-working code below.
Could you please guide us on which part we made mistake ? Because when tried Browser() to debug, it stopped running itself
library(shiny)
library(readxl)
# Increase band width for shiny to handle bigger file
options(shiny.maxRequestSize = 30 * 1024 ^ 2)
# Function to read excels
read_excel_allsheets <- function(filename, tibble = FALSE) {
sheets <- readxl::excel_sheets(filename)
x <-
lapply(sheets, function(X)
readxl::read_excel(
filename,
sheet = X,
col_names = T,
skip = 5
,
col_types = "text"
))
if (!tibble)
x <- lapply(x, as.data.frame)
names(x) <- sheets
x
}
ui <- fluidPage(fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput(
'file1',
'Choose XLSX File (Convert xls to xlsx)',
accept = c('.xlsx')
),
tags$hr(),
downloadButton('downloadData', 'Download')
),
mainPanel(DT::dataTableOutput("contents"))
)
))
server <- function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
mysheets <- read_excel_allsheets(inFile$datapath)
# SheetName is Table
data <- mysheets$Table
data
})
output$contents <- DT::renderDataTable({
DT::datatable(getData(), options = list(pageLength = 7, scrollX = TRUE))
})
output$downloadData <- downloadHandler(
filename = function() {
paste("Pricing Dimension for PPM dated-", Sys.Date(), ".xlsx")
},
content = function(file) {
write_xlsx(getData(), file)
}
)
}
shinyApp(ui, server)
Modularized code
library(shiny)
library(readxl)
# Increase band width for shiny to handle bigger file
options(shiny.maxRequestSize=30*1024^2)
# Function to read all excel sheet necessary
read_excel_allsheets <- function(filename, tibble = FALSE) {
sheets <- readxl::excel_sheets(filename)
x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X, col_names = T, skip = 5
,col_types = "text"
))
if(!tibble) x <- lapply(x, as.data.frame)
names(x) <- sheets
x
}
# Module UI to display sidebar content
mod_example_readDownloadUI <- 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"))
)
}
# Server function to display
mod_example_display <- function(input, output, session) {
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
mysheets <- read_excel_allsheets(inFile$datapath)
data <- mysheets$Table
data
})
output$contents <- DT::renderDataTable({
DT::datatable(getData()
,options = list(pageLength = 7,scrollX = TRUE))
})
}
# Server function to download the displayed data
mod_example_download <- function(input, output, session){
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
mysheets <- read_excel_allsheets(inFile$datapath)
# Sheet Name was Table
data <- mysheets$Table
data
})
output$downloadData <- downloadHandler(
filename = function() {
paste("updated file dated-", Sys.Date(), ".xlsx")
},
content = function(file) {
write_xlsx(DT::datatable(getData(),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_example_readDownloadUI("example_sidemod"))
)
)
),
# BODY -----
shinydashboard::dashboardBody(
shinydashboard::tabItems(
shinydashboard::tabItem("example", mod_example_displayUI("example_bodymod"))
)
)
)
)
server <- function(input, output) {
shiny::callModule(mod_example_display, "example_sidemod")
shiny::callModule(mod_example_download, "example_bodymod")
}
shinyApp(ui,server)