【问题标题】:Load two excel data sets in Shiny after each other在 Shiny 中依次加载两个 Excel 数据集
【发布时间】: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 ~ .)
})
})

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我找到了一个非常简单的解决方案:

    outputOptions(output, "contentsIndex", suspendWhenHidden=FALSE) 放在Server.R 之后output$contentsIndex

    这将停止暂停工作表选择选项,然后在加载第二个数据集后直接显示。

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多