模块化应用程序后,Shiny停止工作

时间:2019-03-18 11:11:15

标签: excel shiny shinydashboard

请在下面找到下面的工作非模块化和模块化非工作代码。

能否请您指导我们我们在哪一部分上犯了错误?因为当尝试使用Browser()进行调试时,它停止了自身运行

基本上 读, 显示和 下载读取的数据

这一切都在应用程序中发生。

未模块化的应用

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
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)

模块化应用

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)

0 个答案:

没有答案