"Select All" does not work in shiny

Hi,
I am making a dashboard of gapminder dataset. I have given option "All" in continents and countries. But when the application opens, the names of continent does not appear; rather numbers (1,2,3,4,5) appear. Why does this happen?
How can I solve this issue?

library(tidyverse)
library(janitor)
library(gapminder)
library(plotly)
library(shiny)
library(shinydashboard)

windowsFonts(a=windowsFont("Times New Roman"))


gapminder<-gapminder %>% 
  clean_names()


ui<-dashboardPage(
  skin = "red",
  dashboardHeader(title = "Dashboard of Gapminder Dataset", titleWidth = 550),
  dashboardSidebar("Select your inputs here",
                   selectInput("continent","Select the Continent",choices = c("All",unique(gapminder$continent)),multiple = T),
                   selectInput("country","Select the Country",choices = NULL,multiple = T),
                   selectInput("year","Select the Year",choices = c("All",unique(gapminder$year)),multiple=T)),
  dashboardBody(
    tabsetPanel(
      tabPanel(title = "Life Expectancy Tab",
               fluidRow(
                 column(6,plotlyOutput("plot1",height = 600,width = '100%')),
                 column(6,plotlyOutput("plot2",height = 600,width = '100%')),
                 column(6,plotlyOutput("plot3",height = 600,width = '100%')))
  )
)))

server<-function(input,output,session){
  
  observe({
  x<-gapminder %>% 
    filter(continent %in% input$continent) %>% 
    select(country)
  updateSelectInput(session,"country","Select the Country",choices = c("All",x))
  
  })
    

  #continent-wise life exp
  
  cont_le<-reactive({
    
    gapminder %>% 
      group_by(continent,year) %>% 
      summarise(avg_le=mean(life_exp)) %>% 
      filter(continent %in% input$continent)
  })
  
  #country-wise life exp
  
  country_le<-reactive({
    
    gapminder %>% 
      group_by(continent,country,year) %>% 
      summarise(mean_le=mean(life_exp)) %>% 
      filter(continent %in% input$continent,
             country %in% input$country)
  })
  
  #life exp as of 2007 (latest year data)
  
  latest_le<-reactive({
    
    gapminder %>% 
      filter(year==2007) %>% 
      group_by(continent) %>% 
      summarise(avg_le=mean(life_exp)) %>% 
      filter(continent %in% input$continent)
    
  })
  
  
  
  
  output$plot1<-renderPlotly({
    
    ggplot(cont_le(),aes(year,avg_le,group=continent,color=continent))+
      geom_line(size=1.5)+
      theme_minimal()+
      labs(title = "Continent-wise Life Expectancy",
           x="Year",
           y="Average Life Expectancy")+
      theme(plot.title = element_text(face="bold",hjust = .5,size = 18),
            text = element_text(family = "a"),
            axis.title = element_text(face = "bold",size=15))
  
  })
  
  output$plot2<-renderPlotly({
    
    ggplot(country_le(),aes(year,mean_le,group=country,color=country))+
      geom_line(size=1.5)+
      theme_minimal()+
      labs(title = "Country-wise Average Life Expectancy",
           x="Year",
           y="Average Life Expectancy")+
      theme(plot.title = element_text(hjust=.5,size = 18,face='bold'),
            text = element_text(family = "a"),
            axis.title = element_text(face = "bold",size = 15))
  })
  
  output$plot3<-renderPlotly({
    
    ggplot(latest_le(),aes(continent,avg_le,fill=continent))+
      geom_col(width=0.6)+
      theme_minimal()+
      labs(title = "Average Life Expectancy of all continents as of 2007",
           x="Continent",
           y="Average Life Expectancy")+
      theme(plot.title = element_text(hjust=.5,size = 18,face='bold'),
            text = element_text(family = "a"),
            axis.title = element_text(face = "bold",size = 15))
    
  })
  
  
  
}

shinyApp(ui,server)

if you make a character vector c("All",...
then what you add to the right, should be character; and will be auto cast to character.
for character's this will not be a problem, for factors, you will get the integer representation, you can cast it to character though.

c("All",letters[1:5])
c("All",factor(letters[1:5]))
c("All",as.character(factor(letters[1:5])))

gapminder countries are factors

1 Like

The answer from @nirgrahamuk will address the issue with continents and countries showing as integers instead of names. To address the "Select All" question, I made some edits to your code below. For each *_le reactive and the observe section, I followed the same basic process. First, create the overall summary for the topic (which covers the "All" case). Then, if "All" is not one of the selections, filter this summary to the selected continent and/or country. The reason "All" was not working in the original code is because "All" is not an option in gapminder continents/countries, so trying to filter to "All" was returning an empty data set.

library(tidyverse)
library(janitor)
library(gapminder)
library(plotly)
library(shiny)
library(shinydashboard)

windowsFonts(a=windowsFont("Times New Roman"))


gapminder<-gapminder %>% 
  clean_names() %>%
  mutate(across(.cols = c('country', 'continent'), .fns = as.character))


ui<-dashboardPage(
  skin = "red",
  dashboardHeader(title = "Dashboard of Gapminder Dataset", titleWidth = 550),
  dashboardSidebar("Select your inputs here",
                   selectInput("continent","Select the Continent",choices = c("All",unique(gapminder$continent)),multiple = T),
                   selectInput("country","Select the Country",choices = NULL,multiple = T),
                   selectInput("year","Select the Year",choices = c("All",unique(gapminder$year)),multiple=T)),
  dashboardBody(
    tabsetPanel(
      tabPanel(title = "Life Expectancy Tab",
               fluidRow(
                 column(6,plotlyOutput("plot1",height = 600,width = '100%')),
                 column(6,plotlyOutput("plot2",height = 600,width = '100%')),
                 column(6,plotlyOutput("plot3",height = 600,width = '100%')))
      )
    )))

server<-function(input,output,session){
  
  observe({
    req(input$continent)
    
    x = gapminder
    
    # if All is not in selection, filter to selected continents
    if(!'All' %in% input$continent) {
      x = x %>%
        filter(continent %in% input$continent)
    }
    
    x = x %>%
      pull(country)
    
    updateSelectInput(session,"country","Select the Country",choices = c("All",x))
    
  })
  
  #continent-wise life exp
  
  cont_le<-reactive({
    req(input$continent)
    
    # summary for All continents
    out = gapminder %>% 
      group_by(continent,year) %>% 
      summarise(avg_le=mean(life_exp))
    
    # if All is not in selection, filter to selected continents
    if(!'All' %in% input$continent) {
      out = out %>%
        filter(continent %in% input$continent)
    }
    
    out
  })
  
  #country-wise life exp
  
  country_le<-reactive({
    req(input$continent, input$country)
    
    # summary for All countries
    out = gapminder %>% 
      group_by(continent,country,year) %>% 
      summarise(mean_le=mean(life_exp))
    
    # if All is not in continent selection, filter to selected continents
    if(!'All' %in% input$continent) {
      out = out %>%
        filter(continent %in% input$continent)
    }
    
    # if All is not in country selection, filter to selected countries
    if(!'All' %in% input$country) {
      out = out %>%
        filter(country %in% input$country)
    }
    
    out
  })
  
  #life exp as of 2007 (latest year data)
  
  latest_le<-reactive({
    req(input$continent)
    
    # summary for All continents
    out = gapminder %>% 
      filter(year==2007) %>% 
      group_by(continent) %>% 
      summarise(avg_le=mean(life_exp)) 
    
    # if All is not in continent selection, filter to selected continents
    if(!'All' %in% input$continent) {
      out = out %>%
        filter(continent %in% input$continent)
    }
    
    out
    
  })
  
  output$plot1<-renderPlotly({
    
    ggplot(cont_le(),aes(year,avg_le,group=continent,color=continent))+
      geom_line(size=1.5)+
      theme_minimal()+
      labs(title = "Continent-wise Life Expectancy",
           x="Year",
           y="Average Life Expectancy")+
      theme(plot.title = element_text(face="bold",hjust = .5,size = 18),
            text = element_text(family = "a"),
            axis.title = element_text(face = "bold",size=15))
    
  })
  
  output$plot2<-renderPlotly({
    
    ggplot(country_le(),aes(year,mean_le,group=country,color=country))+
      geom_line(size=1.5)+
      theme_minimal()+
      labs(title = "Country-wise Average Life Expectancy",
           x="Year",
           y="Average Life Expectancy")+
      theme(plot.title = element_text(hjust=.5,size = 18,face='bold'),
            text = element_text(family = "a"),
            axis.title = element_text(face = "bold",size = 15))
  })
  
  output$plot3<-renderPlotly({
    
    ggplot(latest_le(),aes(continent,avg_le,fill=continent))+
      geom_col(width=0.6)+
      theme_minimal()+
      labs(title = "Average Life Expectancy of all continents as of 2007",
           x="Continent",
           y="Average Life Expectancy")+
      theme(plot.title = element_text(hjust=.5,size = 18,face='bold'),
            text = element_text(family = "a"),
            axis.title = element_text(face = "bold",size = 15))
    
  })
  
}

shinyApp(ui,server)

Thank you so much for the edits. This is good now.

Thank you very much.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.