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