0

I have country and region data. I would like for the user to select the country and have the option to then select the region based on the country selection if they want to.

I want the dashboard to display outputs at a country level as well as region level.

I know it's to do with how I'm filtering the data but I can't figure out how to get the dynamic region list and have country level data.

Any help much appreciated

Data:


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

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:

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)

user14142459
  • 79
  • 1
  • 2
  • 6
  • 1
    Can you please make the code runnable, there are some wrong brackets, missing arguments and the `dplyr` stuff does not really work either. In general, couldn't you just make 2 `reactives`, one with filtering by region and one without? – starja Nov 17 '20 at 12:15
  • As @starja suggests, make sure this is minimally runnable - please edit your question. Also, you reference `Value (Annualised)` but I do not see that in your data frame. – Ben Nov 17 '20 at 14:08
  • Thank you for the comments both, I have updated now and edited the ode so it should work. – user14142459 Nov 18 '20 at 11:18

0 Answers0