Shiny, renderPlot() : problem with geom_line() and group

Hey guys, i recently got into Shiny and after searching for quite a while I don't know what else to try.

The code itself is very basic and gives a few graphs, it works perfectly, as an example :

unempview %>%
filter (active_population>5000, division != "Grand Duchy of Luxembourg") %>%
ggplot(aes(year , unemp_rate_diff, group = division, colour = division)) +
labs(title = "Unemployement rate difference between regions and national average",
   subtitle = "From the year 2001 to 2015", caption = "Taken from your own datasets",
   x = "Year", y = "Unemployement Difference") +

But when i try to apply it to Shiny i can't figure out how to use group or colour. With only one choice selected (other being "None" or the same), graph works fine but if I select 2 distinct regions it links all the points together as if group = 1 regardless of what i tried. Can't find a way to get colours in either.

If you guys could point out the logical mistake i'm doing or help in any way, i'd be grateful. Here's the code :

ui <- fluidPage(
titlePanel("My first Shiny App"),



                         label = "Choose a division",
                         choices = c("None",unempview$division),
                         selected = "None"),
                         label = "Choose another division (Optional)",
                         choices = c("None",unempview$division),
                         selected = "None"),
                           "Display difference with national rate",
                           value = FALSE)

server <- function(input, output) {

output$plot = renderPlot(unempview %>%
filter(division == input$choice1 | division == input$choice2) %>%
ggplot(aes(year, if (input$difference == TRUE) unemp_rate_diff 
                 else unemployment_rate_in_percent),
       group = division, colour = division) +
labs(title = "Unemployement rate comparaison between regions",
     subtitle = "From the year 2001 to 2015",
     caption = "Taken from your own datasets",
     x = "Year",
     y = if (input$difference == TRUE) "Unemployement Difference"
         else 'Unemployement Rate') +

Hi, I think your main problem is in filter. I imagine you don't have a division named "None", and so even when the user has provided no input, all records are filtered out.

To solve that problem, you need to apply the filter iff zero or more division names are supplied. If none are supplied, then no filtering should be performed. Is that what you want?

If so, I'd use a multiple select input to record as many divisions as the user wanted. Then, I'd filter by these divisions only if any divisions were supplied. Here's a simplified example of what I mean, using mtcars:


ui <- fluidPage(
    mainPanel(plotOutput("plot"), textOutput("selected_choice")),
        label = "Choose cylinders", 
        choices = mtcars$cyl %>% unique %>% sort,
        multiple = TRUE

server <- function(input, output, session) {
  output$plot <- renderPlot({
    mtcars %>% 
      { if (length(input$cyl) > 0) filter(., cyl %in% input$cyl) else . } %>%
      ggplot(aes(mpg, cyl)) + geom_line()
  output$selected_choice <- renderText({

shinyApp(ui, server)