【发布时间】:2015-01-13 14:20:56
【问题描述】:
我尝试使用一个提交按钮在 Shiny 中加载两个 excel 数据集。
用户界面有一个侧边栏来加载两个数据集和三个选项卡来显示两个数据集和一个 ggplot 图。
链接到第一个数据集时,我得到一个工作表选择。接下来我链接到第二个数据集。现在,在我单击显示第二个数据集的用户界面中的第二个选项卡之前,我没有选择工作表。
然后我可以使用上传按钮,两个数据集都显示在选项卡 1 和选项卡 2 中,而选项卡 3 显示数据集 1 的图。
我的问题是,为什么我在单击用户界面中的第二个选项卡之前看不到第二个数据集的工作表选择菜单。
最好的祝福
shinyUI(pageWithSidebar(
headerPanel("Claim Overview"),
sidebarPanel(
fileInput(inputId = "iFile", label = "Claims data", accept="application/vnd.ms-excel"),
tags$hr(),
uiOutput(outputId = "ui"),
fileInput(inputId = "iFileIndex", label = "Inflation data", accept="application/vnd.ms-excel"),
tags$hr(),
uiOutput(outputId = "uiIndex"),
submitButton("Upload!", icon("refresh"))
),
mainPanel(
tabsetPanel(
tabPanel("Claim data", dataTableOutput(outputId = "contents")),
tabPanel("Index data", dataTableOutput(outputId = "contentsIndex")),
tabPanel("Claim plot", plotOutput(outputId = "ClaimPlot"))
))
))
shinyServer(function(input, output) {
chooseFile <- reactive({
inFile <- input$iFile
if (!is.null(inFile)) {
wb <- loadWorkbook(inFile$datapath)
sheets <- getSheets(wb)
output$ui <- renderUI({
list(
selectInput(inputId = "sheet", label = "Select a sheet:", choices = sheets),
tags$hr()
)
})
return(list(path = inFile$datapath))
} else {return(NULL)}
})
chooseIndexFile <- reactive({
inFile <- input$iFileIndex
if (!is.null(inFile)) {
wb <- loadWorkbook(inFile$datapath)
sheets <- getSheets(wb)
output$uiIndex <- renderUI({
list(
selectInput(inputId = "sheet", label = "Select a sheet:", choices = sheets),
tags$hr()
)
})
return(list(path = inFile$datapath))
} else {return(NULL)}
})
output$contents <- renderDataTable({
objFile <- chooseFile()
if (!is.null(objFile)) {
Sheet <- input$sheet
if (!is.null(Sheet)){
wb <- loadWorkbook(objFile$path)
dat <- readWorksheet(wb, Sheet)
return(dat)
}
} else {return(NULL)}
})
output$contentsIndex <- renderDataTable({
objFile <- chooseIndexFile()
if (!is.null(objFile)) {
Sheet <- input$sheet
if (!is.null(Sheet)){
wb <- loadWorkbook(objFile$path)
dat <- readWorksheet(wb, Sheet)
return(dat)
}
} else {return(NULL)}
})
readClaimData <- function(){
objFile <- chooseFile()
if (!is.null(objFile)) {
Sheet <- input$sheet
if (!is.null(Sheet)){
wb <- loadWorkbook(objFile$path)
dat <- readWorksheet(wb, Sheet)
return(dat)
}
} else {return(NULL)}
}
readIndexData <- function(){
objFile <- chooseIndexFile()
if (!is.null(objFile)) {
Sheet <- input$sheet
if (!is.null(Sheet)){
wb <- loadWorkbook(objFile$path)
dat <- readWorksheet(wb, Sheet)
return(dat)
}
} else {return(NULL)}
}
output$ClaimPlot <- renderPlot({
x <- readClaimData()
ggplot(data = readClaimData(), aes(x=ClaimNo, y=Claim, fill = State)) + geom_bar(colour = "black", stat = "identity", position = position_dodge()) + facet_grid(Year ~ .)
})
})
【问题讨论】: