【问题标题】:change default values based on another selectInput value in shiny R根据闪亮 R 中的另一个 selectInput 值更改默认值
【发布时间】:2019-01-30 19:28:05
【问题描述】:

我想要两个闪亮的 selectInput。第一个具有默认“A”的选择。我希望第二个输入值基于第一个输入值的默认值。 IE。如果第一个选择是 A,那么第二个的默认值是“LOW”,如果第一个选择是 C 或 D,那么第二个的默认值是“HIGH”。我需要选择将第二个输入更改为任何内容。

我目前正在使用带有 uiOutput 的 selectInput 将其链接在一起,如我的代码所示。目前,第二个值的默认值始终为“LOW”,即使我选择 C ​​或 D。我希望能够选择 C,并将第二个选项默认为“HIGH”

我的代码:

df=data.frame(expand.grid(col1=LETTERS[1:4],col2=c('LOW','MEDIUM','HIGH')))

ui = fluidPage(
  column(4,
         selectInput('d1','Drop 1',
                     choices = sort(unique(df$col1)),
                     selected = sort(df$col1)[1]),
         uiOutput("secondSelection")
  )
)

server <- function(input, output, session) {
  output$secondSelection = renderUI({
    selectInput("User", "Value Dependent on Drop 1:", 
                choices = as.character(df[df$col1==input$d1,"col2"]))
  }) 
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我找到了一个名为 updateselectinput 的函数,并添加了它以解决问题

    df=data.frame(expand.grid(col1=LETTERS[1:4],col2=c('LOW','MEDIUM','HIGH')))
    
    ui = fluidPage(
      column(4,
             selectInput('d1','Drop 1',
                         choices = sort(unique(df$col1)),
                         selected = sort(df$col1)[1]),
             selectInput('d2','Drop 2',
                         choices = sort(unique(df$col2))
                         )
      )
    )
    
    server <- function(input, output, session) {
    
      observe({
        x = input$d1
    
        if(x=='A'){
          updateSelectInput(session,'d2',
                            choices = sort(unique(df$col2)),
                            selected = 'LOW')
        }else if(x=='C' || x=='D'){
          updateSelectInput(session,'d2',
                            choices = sort(unique(df$col2)),
                            selected = 'HIGH')
        }else{
          updateSelectInput(session,'d2',
                            choices = sort(unique(df$col2)),
                            selected = 'MEDIUM')
        }
    
      })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 2018-07-09
      • 2019-10-07
      • 2015-05-07
      • 1970-01-01
      • 2022-01-08
      • 2015-03-23
      • 1970-01-01
      • 2014-12-22
      相关资源
      最近更新 更多