【问题标题】:Vary the choices in selectinput based on other conditions in shiny R根据闪亮 R 中的其他条件改变 selectinput 中的选择
【发布时间】:2014-12-22 11:30:06
【问题描述】:

我有一个闪亮的 selectInput 面板。到目前为止,我只处理了 selectInput 中选择的固定值。

现在我想根据闪亮的 Ui 中的其他一些条件来改变这些选择。

例子:

Ui.R

shinyUI(fluidPage(
fluidRow(column(3,
wellPanel(
                  h4("Data Upload"),
                  fileInput('file1', h5('Choose Your Model Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT')),
                  fileInput('file2', h5('Choose Your Observation Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.xlsx'))    
                ),  
wellPanel(uiOutput("check"))))

Server.R

shinyServer(function(input, output) {
output$check <- renderUI({
   selectInput("check", label = h4("Dataset Selection"), choices = c("Model" = 1, "Observation" = 2, "Both" = 3), selected = 1, multiple = F )
  })
a <- reactive({
   fileinput1 <- input$file1
   if (is.null(fileinput1))
   return(NULL)
   read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))
 })

 #Upload Observation Data 

 b <- reactive({
   fileinput2 <- input$file2
   if (is.null(fileinput2))
   return(NULL)
   #xlfile <- list.files(pattern = ".xlsx")
   xlfile <- fileinput2[1]
   wb <- loadWorkbook(xl_file)
   sheet_ct <- wb$getNumberOfSheets()
   b <- rbindlist(pblapply(1:sheet_ct, function(x) {
     res <- read.xlsx(xl_file, x)
   }), fill=TRUE)
   b <- b [-c(1),]
   print (b)
   })

现在我想根据文件输入在 selectInput 中动态做出选择。

【问题讨论】:

  • 查看?updateSelectInput 及其家族函数来处理这些问题。也看看?conditionalPanel
  • 我已经尝试过 updateselectinput 并未能实时更新选择列表......就像用户上传文件1我需要根据“模型”更新选择列表......如果他然后上传第二个文件然后提供所有三个选项..如果只上传第二个文件然后选择 = 只是“观察”。请让我知道如何实现。

标签: r shiny


【解决方案1】:

我已尝试纠正 server.R 文件中的一些问题。请注意,我遵循以下算法

  1. 如果先上传 file1,则选择“型号”
  2. 如果随后上传了 file2,则选择应为“Model”、“Observation”、“Both”
  3. 如果先上传 file2,则选择“观察”
  4. 如果 file1 随后上传,则选择应为“Model”、“Observation”、“Both”

库(闪亮)库(xlsx)

shinyServer(function(input, output) {

  a <- reactive({
    fileinput1 <- input$file1
    if (is.null(fileinput1))
      return(NULL)
    #read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))

    #Please change this part back to your code as I dont have your file based on the column names above
    read.table(fileinput1$datapath, header= TRUE)
  })

  #Upload Observation Data 

  b <- reactive({
    fileinput2 <- input$file2
    if (is.null(fileinput2))
      return(NULL)
    #xlfile <- list.files(pattern = ".xlsx")
    xlfile <- fileinput2$datapath
    wb <- loadWorkbook(xlfile)
    sheet_ct <- wb$getNumberOfSheets()
    b <- rbind(list(lapply(1:sheet_ct, function(x) {
      res <- read.xlsx(xlfile, x)
    })))
    b <- b [-c(1),]
    print(b)
  })

  getModel <- reactive({
    if(!is.null(a()) & !is.null(b()))
    {
      c("Model", "Observation", "Both")
    }
    else if(!is.null(a()))
    {
      "Model"
    }
    else if(!is.null(b()))
    {
      "Observation"
    }


  })
  output$check <- renderUI({
    selectInput("check", label = h4("Dataset Selection"), choices = as.list(getModel()), multiple = F )
  })


})

【讨论】:

  • 感谢您的回答。只是想知道有没有一种方法可以将闪亮(ui.R)中的fileInput的进度条与反应组件b计算联系起来。因为如您所知,合并 xlsx 中的工作表需要一些时间。
  • 我想你可以把这个b &lt;- rbind(list(lapply(1:sheet_ct, function(x) { res &lt;- read.xlsx(xlfile, x) })))改成b &lt;- rbind(list(lapply(1:sheet_ct, function(x) { res &lt;- read.xlsx(xlfile, x) withProgress(message = 'Calculation in progress', detail = 'This may take a while...', value = 0, { for (i in 1:sheet_ct) { incProgress(1/sheet_ct) Sys.sleep(0.25) } }) })))
  • 谢谢。 ?withProgress --> 没有这样的功能。使用上面的代码,它会抛出错误:Unexpected Symbol.
  • 嗨,这适用于 shiny_0.10.2.1。请看link
  • 很抱歉在这里问一个额外的问题......请检查是否可以,我已添加到现有问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 2019-01-30
  • 2014-12-15
  • 2021-02-24
  • 1970-01-01
  • 2017-03-21
  • 2021-06-18
相关资源
最近更新 更多