How to use updateSelectInput in flexdashboard?

巧了我就是萌 提交于 2019-12-24 12:34:59

问题


I'd like to change values of an inputbox based on another inputbox.

With R Shiny there is a method available: updateSelectInput. But I'm not sure how to use it in flexdashboard.


回答1:


This is quite late but also quite possible.

If this is your first selectInput()

raw_data <- mpg

selectInput(
  "manufacturer",
  label = "Select manufacturer",
  choices = c("All", sort(unique(raw_data$manufacturer)))
)

You can try either of these approaches. This one is overall less code:

renderUI({

  df <-
    raw_data %>%
    filter(
      manufacturer == input$manufacturer |
        input$manufacturer == "All"
    )

  selectInput(
    "model",
    label = "Select model",
    choices = c("All", sort(unique(df$model)))
  )

})

Or an observeEvent(). I think this one saves on performance as it only looks at one input, but it's not a huge difference.

selectInput(
    "model",
    label = "Select model",
    choices = c("All", sort(unique(raw_data$model)))
  )


observeEvent(input$manufacturer, {
  df <-
    raw_data %>%
    filter(
      manufacturer == input$manufacturer |
        input$manufacturer == "All"
    )

  updateSelectInput(
    session = session,
    inputId = "model",
    choices = c("All", sort(unique(df$model)))
  )
})


来源:https://stackoverflow.com/questions/45839542/how-to-use-updateselectinput-in-flexdashboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!