简体   繁体   English

SHINY R根据用户选择读取不同版本的Excel文件

[英]SHINY R reading different versions of excel files based on user selections

I have a shiny app, that reads in files that are uploaded by a user Different people have different versions of excel. 我有一个闪亮的应用程序,可以读取用户上传的文件。不同的人具有不同的excel版本。 So if a user is using excel 2007 or excel 2010 we use one section of code. 因此,如果用户使用的是excel 2007或excel 2010,我们将使用一段代码。 If they upload it in excel 2003 we use a different library to read the file. 如果他们在excel 2003中上载文件,我们将使用其他库来读取文件。 The user specifies in the form which version of excel they have 用户以表格形式指定他们拥有的excel版本

The function to do this is below 执行此操作的功能如下

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

To implement this in shiny, i have two widgets. 为了实现这一点,我有两个小部件。 A fileInput and a selectInput . 一个fileInput和一个selectInput The user selects which version of excel they are running and then selects the file which then is read in by the function get_data . 用户选择他们正在运行的excel版本,然后选择文件,然后由function get_data读取该文件。 I suspect its because I'm not utilizing the reactivity correctly. 我怀疑是因为我没有正确利用反应性。 When i run the app and upload the file i get the error message 当我运行应用程序并上传文件时,出现错误消息

Error: object 'storagetType' not found 错误:找不到对象“ 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)
})

})

You not need selectInput simply parse name of file. 您不需要selectInput只需解析文件名。

Also some typo fixed 还修正了一些错字

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM