SHINY R根据用户select读取不同版本的excel文件

我有一个shiny的应用程序,读取由用户上传的文件不同的人有不同的版本的Excel。 所以,如果用户使用Excel 2007或Excel 2010,我们使用一段代码。 如果他们在Excel 2003中上传,我们使用不同的库来读取文件。 用户在表格中指定了哪个版本的excel

下面是做这个的function

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 。 用户select正在运行的哪个版本的excel,然后select由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) }) }) 

你不需要selectInput只是parsing文件的名字。

还有一些错字固定

  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)