【问题标题】:SHINY R reading different versions of excel files based on user selectionsSHINY R根据用户选择读取不同版本的excel文件
【发布时间】:2016-09-13 10:43:09
【问题描述】:

我有一个闪亮的应用程序,它读取用户上传的文件 不同的人有不同版本的excel。因此,如果用户使用的是 excel 2007 或 excel 2010,我们会使用一段代码。如果他们在 excel 2003 中上传它,我们将使用不同的库来读取文件。用户在表单中指定他们拥有的 excel 版本

执行此操作的函数如下

get_data <- function(strFilePath, storageType) {

  if (is.null(strFilePath))
    return(NULL)

  if (storagetType == 'xls2010' || storagetType == 'xls2007'){
      df <- openxlsx:read.xlsx(strFilePath,sheet = 1)
  }
  else if (storagetType == 'xls2003'){
    df <- XLConnect:readWorksheetFromFile(strFilePath)
  } 

  return(df)
}

为了在闪亮中实现这一点,我有两个小部件。一个fileInput 和一个selectInput。用户选择他们正在运行的 excel 版本,然后选择由function get_data 读取的文件。我怀疑是因为我没有正确利用反应性。当我运行应用程序并上传文件时,我收到错误消息

错误:找不到对象“storagetType”

# Global.R
storage_types <- c(
  "Excel 2010" = "xls2010",
  "Excel 2007" = "xls2007",
  "Excel 2003" = "xls2003"
)

# UI.R
ui <- shinyUI(fluidPage(
 navbarPage("Navbar!",
             # Tab contains all the information to upload a file
             tabPanel("Upload Data",
                      # Side Panel with Options
                      fluidRow(
                        column(4, wellPanel(
                          id = "leftPanel",
                          div(
                            id = "Header",
                            h3("Options", align = "center"),
                            tags$hr()
                            ),

                          div(
                        selectInput("xlsversion", "2.  Select your Excel version", storage_types),
                        fileInput(inputId = 'file1',label =  '3.  Choose An Excel File'), 
                        )
)))))))

# Server.R

server <- shinyServer(
  function(input, output) {
   # When the Browser to the file location gets updated  
    upload_data <- reactive({
      inFile <- input$file1
      if (is.null(inFile))
        return(NULL)
      get_data(inFile$datapath, input$xlsversion)
})

})

【问题讨论】:

  • 它的错字? storageType 与 get_data 函数中的 storagetType 不同
  • 你需要什么selectInput("xlsversion",?为什么不简单地检查 inFile$name 是否以 xlsx 或 xls .. 结束(其他错误,它可以保护您免受崩溃)
  • 嗨@Batanichek,是的,错字似乎是问题所在。如果我解析文件名,这是一个更好的主意,请将其添加为答案并将其标记为正确

标签: r excel shiny


【解决方案1】:

您不需要selectInput 只需解析文件名。

还修正了一些错别字

 library(shiny)

get_data <- function(strFilePath, storageType) {

  if (is.null(strFilePath))
    return(NULL)

  file_ext=substring(storageType,nchar(storageType)-3)
  if (file_ext == 'xlsx' ){
    df <- openxlsx::read.xlsx(strFilePath,sheet = 1)
  }
  else if (file_ext == '.xls'){
    df <- XLConnect::readWorksheetFromFile(strFilePath,sheet=1)
  } else{
    return(data.frame("Bad file format"))
  }

  return(df)
}
# UI.R
ui <- shinyUI(fluidPage(
  navbarPage("Navbar!",
             # Tab contains all the information to upload a file
             tabPanel("Upload Data",
                      # Side Panel with Options
                      fluidRow(
                        column(4, wellPanel(
                          id = "leftPanel",
                          div(
                            id = "Header",
                            h3("Options", align = "center"),
                            tags$hr()
                          ),

                          div(
                           fileInput(inputId = 'file1',label =  '3.  Choose An Excel File') 
                          )
                        ))),
                      dataTableOutput("result")))))

# Server.R

server <- function(input, output) {
    # When the Browser to the file location gets updated  
    upload_data <- reactive({
      if (is.null(input$file1))
        return(NULL)
      get_data(input$file1$datapath, input$file1$name)
    })
    output$result=renderDataTable({
      upload_data()
    })
}

shinyApp(ui,server)

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 2019-04-01
    • 1970-01-01
    • 2018-01-08
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    相关资源
    最近更新 更多