通过 R Shiny 中的先前输入限制输入的选项

Limit the options of an input by a previous input in R Shiny

我正在构建一个闪亮的应用程序,想知道如何根据第一个输入中的 selected 选项自动限制 select 输入的选择。 该应用程序如下所示:

library(shiny)
library(shinyWidgets)
library(dslabs)
library(tidyverse)
library(plotly)


ui <- fluidPage(
    
    titlePanel("Traffic Incidents"),
    sidebarLayout(
        sidebarPanel(
            # inputs
            selectizeInput("vehicleInput", "Vehicle",
                           choices = unique(incident$vehicle),  
                           selected="448", multiple =FALSE),
            selectizeInput("groupInput", "Incident Group",
                           choices = unique(incidents$group),  
                           selected="None", multiple =FALSE)
        ),  
        
        mainPanel(
            # outputs
            plotlyOutput("plot"),
            br(), br(),
            plotlyOutput("plot2")
            
        ) 
    ) 
)   

server <- function(input, output) {
    
        d <- reactive({
            filtered <-
                incidents %>%
                filter(vehicle == input$vehicleInput,
                       group %in% input$groupInput)    
            
        }) 
         
    output$plot <- renderPlotly({
        
        box <- plot_ly(d(), x = ~month, y = ~casualties)  %>%
            layout(title = "Incident Casualties",
                   yaxis = list(title=input$dataInput))
        
    })
    
    output$plot2 <- renderPlotly({
        
        box <- plot_ly(d(), x = ~month, y = ~costs)  %>%
            layout(title = "Incident Costs",
                   yaxis = list(title=input$dataInput))
        
    })
    
}

shinyApp(ui=ui, server=server)

我想知道如何通过“车辆”输入中的 selected 选项来限制“事件组”输入中的选项,即只能选择与以下相关的事件组个人车辆。

我知道我应该使用 observe() 函数,但是,我确实很难正确使用它。

感谢您的任何建议!

也许这就足够了

ui <- fluidPage(
  
  titlePanel("Traffic Incidents"),
  sidebarLayout(
    sidebarPanel(
      # inputs
      uiOutput("veh"),
      uiOutput("grp")
    ),  
    
    mainPanel(
      # outputs
      plotlyOutput("plot"),
      br(), br(),
      plotlyOutput("plot2")
      
    ) 
  ) 
)   

server <- function(input, output) {
  
  output$veh <- renderUI({
    selectizeInput("vehicleInput", "Vehicle",
                   choices = unique(incident$vehicle),  
                   selected="448", multiple =FALSE)
  })
  
  d1 <- reactive({
    data <- incidents %>% filter(vehicle %in% req(input$vehicleInput))
  })
  
  output$grp <- renderUI({
    req(d1())
    selectizeInput("groupInput", "Incident Group",
                   choices = unique(d1()$group),  
                   selected="None", multiple =FALSE)
  })
  
  d <- reactive({
    req(d1(),input$groupInput)
    filtered <- d1() %>%  filter(group %in% input$groupInput)
  }) 
  
  output$plot <- renderPlotly({
    
    box <- plot_ly(d(), x = ~month, y = ~casualties)  %>%
      layout(title = "Incident Casualties",
             yaxis = list(title=input$dataInput))
    
  })
  
  output$plot2 <- renderPlotly({
    
    box <- plot_ly(d(), x = ~month, y = ~costs)  %>%
      layout(title = "Incident Costs",
             yaxis = list(title=input$dataInput))
    
  })
  
}

shinyApp(ui=ui, server=server)