Problem with using a reactive variable in shiny

server <- function(input, output) { output$regionOutput <- renderUI({ selectInput("regionInput", "region", c("All", sort(unique(mdg1a$region))), selected = "All" ) })

cir <- reactive({ #cir stands for Countries In Region if (is.null(input$regionInput)) { return(NULL) } if (input$regionInput == "All") { mdg1a } else { mdg1a %>% filter(region == input$regionInput) } })

output$countryOutput <- renderUI({ selectInput("countryInput", "country", sort(unique(cir()$country)), multiple = TRUE, selected = "") })

filtered <- reactive({ if (is.null(input$regionInput)) { return(NULL) } if (is.null(input$countryInput)) { return(NULL) } cir() %>% filter(year >= input$yearInput[1], year <= input$yearInput[2], country == input$countryInput ) })

output$plot <- renderPlot({ if (is.null(filtered())) { return() } if (NROW(unique(filtered()$country)) <= 4) { ggplot(filtered(), aes(year, value_n, color = country)) + geom_line() } }) # +xlim(min(filtered()$year), max(filtered()$year)) + ylim(min(filtered()$value_n), max(filtered()$value_n)) output$table <- renderTable({ if (is.null(filtered())) { return() } if (NROW(unique(filtered()$country)) <= 4) { filtered()[order(filtered()$year),] %>% select(year, country, value_n) %>% spread(country, value_n) } })

}

/r/rstats Thread