简体   繁体   English

R Shiny:有条件地要求用户使用 select 选择器输入

[英]R Shiny: Conditionally require user to select pickerInput

I have two pickerInput values in the app.我在应用程序中有两个pickerInput值。 In the first, geography , the user can select to view either state (default) or county data.首先是geography ,用户可以 select 查看state (默认)或county数据。 If the user selects county , I'd like to require that they pick a state from the second pickerInput , which is just a list of states.如果用户选择county ,我想要求他们从第二个 pickerInput 中选择一个pickerInput ,这只是一个州列表。 It is not required that the user pick a state when input$geography == "state" .input$geography == "state"时,用户不需要选择 state。

I have considered putting this inside of a modalDialogue but it wasn't working.我考虑过将它放在modalDialogue中,但它不起作用。 I also tried an updatePickerInput which didn't work either.我还尝试了一个也不起作用的updatePickerInput

What is the best way of conditionally requiring the user to select a value from a pickerInput ?有条件地要求用户 select 来自pickerInput的值的最佳方法是什么?

Thank you.谢谢你。

Here is a solution with shinyjs :这是shinyjs的解决方案:



library(shiny)
library(shinyWidgets)
library(shinyjs)

ui <- fluidPage(

  useShinyjs(),

  pickerInput("pick_1", choices = c("state","county"), multiple = FALSE, selected = "state"),
  shinyjs::hidden(
    pickerInput("pick_2", choices = state.name, multiple = TRUE)
  )


)

server <- function(input, output, session) {

  observe({
    toggleElement("pick_2", condition = input$pick_1 == "state")

  })


}

shinyApp(ui, server)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM