Have a progressBox add more than one input and add together

I was wondering if it was possible to be able to add more than one input that will change the outcome of the progressBox. Currently if the user selects from the dropdown it updates with the country and the amount of occurences in that country but now I have changed the dropdown so it now can take multiple selections, so was wondering if I can change the progressBox so it takes in the selected countries and states them all and then adds the occurences together. I hope this makes sense, thanks!
UI:

infoBoxOutput("progressBox", width = 6)

Server:

server <- function(input, output, session) {
output$progressBox <- renderInfoBox({
    infoBox(
      input$vicNation,
      value = paste("Total", nrow(newngo %>% filter(Victim.Nationality == input$vicNation)),
                    collapse = " "),
      icon = icon("user-friends"),
      color = "black",
      fill = T
    )
  })
}

Hi @fitzer23!

Can you try composing a small, self-contained, reproducible example illustrating your question? Right now, it's hard to work on your problem because the code above isn't runnable as-is. Even very experienced people generally prefer to work on code problems by running code, rather than by reading it.

I have made a reproducible example that is needed to test. If anything else needs to be added please let me know.

library(shinyjs)
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(shinyWidgets)

vNation = c("India", "India", "India", "Myanmar", "Myanmar", "Nepali", "Nepali", "Nepali", "Nepali", "United Kingdom", "Oman")
newngo = vNation
newngo = as.data.frame(newngo)

UI:

ui <- dashboardPage(skin = ("black"),
                    dashboardHeader(title = "Human Trafficking"),
                    
                    dashboardSidebar(
                      sidebarMenu(
                      )
                    ),
                        
                    dashboardBody(
                      
                      fluidRow(
                        infoBoxOutput("progressBox", width = 6)
                      ),
                      
                      fluidRow(     
                        box(width = 6, solidHeader = TRUE, status = "primary",
                            title = "Victim Nationality", 
                            selectInput("vicNation", "Select victim nationality: ", 
                                        choices = sort(unique(vNation)), selected = NULL,
                                        multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
                            plotlyOutput("Nationality", width = '750px', height = '300px')
                        )
                      )
                    )
)

Server:

server <- function(input, output, session) {
  filteredData <- reactive({
    filtNgo <- newngo
    
    if(!is.null(input$vicNation)) {
      filtNgo <- filtNgo %>% filter(vNation %in% input$vicNation)
    }
    filtNgo
  })
  
  output$Nationality <- renderPlotly({
    plot_ly(filteredData(), labels = vNation, type = "pie",
            marker = list(colors = colors,
                          line = list(color = '#FFFFFF', width = 1))) %>%
      layout(showlegend = FALSE)
  })
  
  output$progressBox <- renderInfoBox({
    infoBox(
      input$vicNation,
      value = paste("Total", nrow(newngo %>% filter(vNation == input$vicNation)),
                    collapse = " "),
      icon = icon("user-friends"),
      color = "black",
      fill = T
    )
  })

}

shinyApp(ui, server)

Currently if the user selects one country it states the name and how often it occurs. What I am looking for is if the user selects multiple options it will state both e.g. United Kingdom, India and then add the total of the two. Thanks

Updated to better reproducible example

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