0

我有国家和地区数据。我希望用户选择国家,然后可以根据需要选择基于国家/地区选择的区域。

我希望仪表板显示国家级别和地区级别的输出。

我知道这与我如何过滤数据有关,但我不知道如何获取动态区域列表并拥有国家级数据。

非常感谢任何帮助

数据:


Country <- as.character(c('England', 'Scotland', 'Wales', 'Ireland', 'Spain', 'England', 'Scotland', 'Wales', 'Ireland', 'Spain', 'England', 'Scotland', 'Wales', 'Ireland', 'Spain' , 'England', 'Scotland', 'Wales', 'Ireland', 'Spain'))

Region  <- as.character(c('North' , 'East', 'South', 'South', 'North' , 'South', 'East', 'North' , 'South', 'West', 'North' , 'South' , 'North' , 'West', 'North' , 'West', 'West', 'East', 'East', 'South'))

Value   <- as.numeric(c(100, 150, 400, 300, 100, 150, 300, 200, 500, 600, 300, 200, 250, 300, 100, 150, 300, 200, 500, 600))

Outcomes <- as.character(c('Green', 'Red','' , 'Amber', 'Green', 'Green', 'Red','' , 'Red', 'Amber', 'Red', 'Green', 'Green', 'Green','' ,'' , 'Amber', 'Amber', 'Red', 'Red'))

Outputs <- as.character(c('Red', 'Green', 'Green', 'Green', '','' , 'Amber', 'Green', 'Red','' , 'Red', 'Amber', 'Red', 'Green', 'Green', '','' , 'Amber', 'Amber', 'Red'))

Risk <- as.character(c('Green', 'Green', 'Red', 'Red','' , 'Amber', 'Green', 'Green', 'Amber','' , 'Green', 'Red', 'Amber', 'Green', 'Green', 'Red', '', 'Red', 'Amber', ''))


Joined_data2 <- data.frame(Country,stringsAsFactors=FALSE, Region, Value, Outcomes, Outputs, Risk)

Countrylist<- unique(Joined_data2$Country)

Regionlist <- unique(Joined_data2$Region)

用户界面

ui<- dashboardPage(
  dashboardHeader(title = "Performance"),
  
  
  dashboardSidebar(selectInput(inputId = "Country1", label = "Country", choices = c('All', Countrylist)),
                   (selectInput(inputId = "Region1", label = "Region", choices = c('All', Regionlist)))),
  
 
  dashboardBody( 
    fluidRow(
      box(valueBoxOutput(outputId = "Total", width = 12), title = "Total"),
   
      
      
    )
  ),
  
)

服务器:

server <- function(input, output, session) { 
  
  Test2 <- reactive({
    
    
    req(input$Country1) 
    if(input$Country1 == 'All') {
      Joined_data2 
    } 
    else 
      
      {
      Joined_data2 %>%
        filter(Country %in%  input$Country1)
    } 
    })
  
  
  
  output$Total <- renderValueBox({
    
    valueBox(Test2() %>%
               tally(), 
             
             req(input$Country1))
             
  })
    

      
     Country.choice <- reactive({
    Joined_data2 %>% 
   filter('Country' %in% req(input$Country1)) %>%
   pull('Region')
    })
      
    
      
      
        observe({
        
          updateSelectInput(session, "Region1", choices = Country.choice())
          
        })
}

shiny::shinyApp(ui=ui,server=server)

4

0 回答 0