简体   繁体   English

根据闪亮的R中的另一个selectInput值更改默认值

[英]change default values based on another selectInput value in shiny R

I want two selectInput's in shiny. 我想要两个selectInput闪亮。 The first one has a selection with default 'A'. 第一个选择为默认值“ A”。 I want the second input value to default based on the value of the first. 我希望第二个输入值基于第一个输入的值成为默认值。 ie. 即。 If the First selection is A, then the default of the second is 'LOW', if the first selection is C or D, the default of the second is 'HIGH'. 如果第一个选择为A,则第二个的默认值为'LOW';如果第一个选择为C或D,则第二个的默认值为'HIGH'。 I need to have the option of the second input to be changed to anything. 我需要将第二个输入更改为任何内容。

I am currently using a selectInput with a uiOutput to link it together, as my code shows. 正如我的代码所示,我目前正在使用带有uiOutput的selectInput将其链接在一起。 Currently, the default of the second value is always 'LOW', even when I select C or D. I want to be able to select C, and have the second choice default to 'HIGH' 当前,即使选择C或D,第二个值的默认值始终为“低”。我希望能够选择C,并将第二个选项的默认值设置为“高”。

My code: 我的代码:

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)

I found a function called updateselectinput, and added it in to fix the problem 我找到了一个名为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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DT:根据 R 闪亮应用程序中另一列的选择输入动态更改列值 - DT: Dynamically change column values based on selectinput from another column in R shiny app R/Shiny:selectInput 字段中的多个默认值 - R/Shiny: multiple default values in selectInput field 基于 R shiny 中的另一个的多个反应选择输入 - Multiple reactive selectinput based on another in R shiny SelectInput基于R Shiny中先前的SelectInput更新值 - SelectInput Value update based on previous SelectInput in R shiny 在 R Shiny 中检测到 selectInput 值更改为 NULL - Detecting a selectInput value change to NULL in R Shiny 根据R中的selectinput值更改图表 - Change chart based on selectinput value in R R闪亮的selectinput与数据库值 - R shiny selectinput with database values R Flex仪表板(发光):checkboxgroupinput和selectinput之间的动态行为。 防止selectInput返回默认值 - R Flex Dashboard (Shiny): Dynamic behavior between checkboxgroupinput and selectinput. Prevent selectInput to go back to default values 如何使用 actionButton 更改 R Shiny 中 selectInput 上的选定值? - How to use an actionButton to change the selected value on a selectInput in R Shiny? 闪亮的R:在selectInput值更改时重置其他输入值 - Shiny R: Reset other input values when selectInput value changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM