在 Shiny 中依次加载两个 excel 数据集
Load two excel data sets in Shiny after each other
我尝试使用一个提交按钮在 Shiny 中加载两个 excel 数据集。
用户界面有一个用于加载数据集的边栏和三个用于显示数据集和 ggplot 图的选项卡。
当link访问第一个数据集时,我得到一个sheet选择。接下来我link到第二个数据集。现在我没有得到 sheet 选择,直到我单击用户界面中显示第二个数据集的第二个选项卡。
然后我可以使用上传按钮,两个数据集都显示在选项卡 1 和选项卡 2 中,而选项卡 3 显示数据集 1 的图表。
我的问题是,为什么在单击用户界面中的第二个选项卡之前看不到第二个数据集的 sheet 选择菜单。
祝福
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 ~ .)
})
})
我找到了一个非常简单的解决方案:
将outputOptions(output, "contentsIndex", suspendWhenHidden=FALSE)
放在output$contentsIndex
之后的Server.R
中。
这将停止暂停 Sheet 选择选项,然后在加载第二个数据集后直接显示。
我尝试使用一个提交按钮在 Shiny 中加载两个 excel 数据集。
用户界面有一个用于加载数据集的边栏和三个用于显示数据集和 ggplot 图的选项卡。
当link访问第一个数据集时,我得到一个sheet选择。接下来我link到第二个数据集。现在我没有得到 sheet 选择,直到我单击用户界面中显示第二个数据集的第二个选项卡。
然后我可以使用上传按钮,两个数据集都显示在选项卡 1 和选项卡 2 中,而选项卡 3 显示数据集 1 的图表。
我的问题是,为什么在单击用户界面中的第二个选项卡之前看不到第二个数据集的 sheet 选择菜单。
祝福
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 ~ .)
})
})
我找到了一个非常简单的解决方案:
将outputOptions(output, "contentsIndex", suspendWhenHidden=FALSE)
放在output$contentsIndex
之后的Server.R
中。
这将停止暂停 Sheet 选择选项,然后在加载第二个数据集后直接显示。