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)