Get an infoBox to update automatically from selectInput

Have an info box that will update nationality based on the selected input but trying to get it to update with the total amount that the nationality has appeared.

Have tried adding table() to produce the added amount but didn't work.

'output$progressBox <- renderInfoBox({
    infoBox(
      paste(input$Nationality), paste("Total", table(ngo$Victim.Nationality)), icon = icon("user-friends"),
      color = "purple"
    )
  })'

I want that when a nationality is selected from drop down list it will update nationality and total amount of occurrences

Hi,

We're happy to help you, but in order to look in more detail at the issue, you'll need to provide us with code that recreates the issue. This is especially important in Shiny since the reactive environments connect different parts of the code and bugs travel between them.

Look at the attached post how to create a minimal reproducible example:

Good luck
PJ

A sample of the data set.

Victim Nationality Victim Age
United Kingdom 18
US 22
Uzbekistan 15
Bangladesh 18
US 25
US 18
United Kingdom 15
Bangladesh 17
US 15
Uzbekistan 19

Sample piece of the code.

ui <- dashboardPage(
dashboardBody(
fluidRow(
                box( width = 4, solidHeader = TRUE, status = "primary",
                  selectInput("Nationality", "Choose victim nationality: ",
                              list("AF", "AL", "Asian", "Bangladesh", "BD", "BF", "BG", "BO", "Brazilian", "BY", "CD", "Chinese",
                                   "CI", "CN", "CO", "Czech Republic", "Eastern Europe", "ER", "GH", "GN", "GOA", "GW", "HD", "ID", "IN", 
                                   "India", "Indian", "Jordan", "KG", "KH", "KR", "KZ", "LA", "LK", "Lybanese", "MD", "MG", "ML", "MM",
                                   "MX", "Myanmar", "NE", "Nepal", "Nepali", "NG", "Nigeria", "NP", "Oman", "PH", "Polish", "Qatar", "RO", 
                                   "Romanian", "Russian", "SL", "SN", "Spain", "Sudan", "SV", "Syrian", "TH", "TJ", "TM", "UA", "UG", 
                                   "United Kingdom", "US", "UZ", "Uzbekistan", "VN", "ZZ")
                  ),
infoBoxOutput("progressBox")
))))

server <- function(input, output) {
utput$progressBox <- renderInfoBox({
    infoBox(
      paste(input$Nationality), paste("Total", table(ngo$Victim.Nationality)), icon = icon("user-friends"),
      color = "purple"
    )
  })
}

So when the user selects a nationality out of the drop down menu it will update the infoBox to what country it is and the amount of time that country occurs. I think thats what you need, if need anything let me know thanks.

Hi,

Here is my approach:

library("shiny")
library("shinydashboard")
library("dplyr")

ui <- dashboardPage(
  dashboardHeader(), #needed to run
  dashboardSidebar(), #needed to run
  dashboardBody(
    fluidRow(
      box(solidHeader = TRUE, status = "primary",
           selectInput("Nationality", "Choose victim nationality: ", choices = ""),
           infoBoxOutput("progressBox", width = NULL) #needs to have width = NULL to display properly
      ))))

server <- function(input, output, session) { #need to add session to update selectInput
  
  #Sample dataset
  ngo = data.frame(
    Victim.Nationality = c("United Kingdom", "US", "Uzbekistan", "Bangladesh", "US"),
    Victim.Age = c(18,22,15,18,25))
  
  #Tip for selectInput options: use updatSelectInput to load names from file
  nationalities = c("AF", "AL", "Asian", "Bangladesh", "BD", "BF", "BG", "BO", "Brazilian", "BY", "CD", "Chinese",
                       "CI", "CN", "CO", "Czech Republic", "Eastern Europe", "ER", "GH", "GN", "GOA", "GW", "HD", "ID", "IN", 
                       "India", "Indian", "Jordan", "KG", "KH", "KR", "KZ", "LA", "LK", "Lybanese", "MD", "MG", "ML", "MM",
                       "MX", "Myanmar", "NE", "Nepal", "Nepali", "NG", "Nigeria", "NP", "Oman", "PH", "Polish", "Qatar", "RO", 
                       "Romanian", "Russian", "SL", "SN", "Spain", "Sudan", "SV", "Syrian", "TH", "TJ", "TM", "UA", "UG", 
                       "United Kingdom", "US", "UZ", "Uzbekistan", "VN", "ZZ")
  #OR ...
  nationalities = sort(unique(ngo$Victim.Nationality))
    
  updateSelectInput(session, "Nationality", choices = nationalities) #update the input choices
  
  output$progressBox <- renderInfoBox({
    infoBox(
      input$Nationality, 
      #You needed to calculate the number of times the nationality appeared (several ways to do it)
      value = paste("Total", nrow(ngo %>% filter(Victim.Nationality == input$Nationality)),
                    collapse = " "), 
      icon = icon("user-friends"),
      color = "purple",
      fill = T
    )
  })

  }

shinyApp(ui, server)

Few notes:

  • Please test your code and add packages used before submitting it as I had to add stuff before I could run it successfully
  • You can use the updateSelectInput to add choices to inputs from the server side
  • The infoBoxOutput needed width = NULL to display properly

Hope it works now!
PJ

Thanks PJ, works perfectly

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