【发布时间】: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,是的,错字似乎是问题所在。如果我解析文件名,这是一个更好的主意,请将其添加为答案并将其标记为正确