0

I am trying to use each values selected in a pickerInput control in r shiny app. I have a sample code as follows:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  pickerInput(inputId = "testPicker",
              label = "Select Multiple",
              choices = c(2021, 2020,2019,2018,2017),
              selected = c(2021, 2020,2019,2018,2017),
              multiple = TRUE,
                options = list(
                    `actions-box` = TRUE,
                    "max-options" = 4,
                    "max-options-text" = "You can ONLY select 4 years!"
                )),
  verbatimTextOutput("year1"),
  verbatimTextOutput("year2")
  verbatimTextOutput("year3")
)

server <- function(input, output, session) {
    
    output$year1 <- renderPrint(input$testPicker[3])
    output$year2 <- renderPrint(input$testPicker[2])
    output$year3 <- renderPrint(input$testPicker[1])
}

shinyApp(ui, server)

I was expecting this code above to work but it is not printing any values. I want to use each selected value in a search to get a different data frame like this:

total_for_year1 <- main_data() %>%
            filter(Year == input$testPicker[4]) %>%
            group_by(LGA) %>%
            summarise("Total Students" = sum(`Class Student Count`)) %>%
            ungroup()

The data I am using can be downloaded from https://github.com/BAderinto/rshiny_data/blob/main/df.csv I needed the user to select multiple years, then I use each of the values to filter and return a data frame each for each year selected.

Any suggestion?

BWO
  • 95
  • 2
  • 6
  • Why doesn’t your current attempt work? – Limey Oct 11 '21 at 15:55
  • It actually did not work as it wasn't displaying any values. But if I use ```output$year <- renderPrint(input$testPicker)``` it prints all the values using ```verbatimTextOutput("year")``` – BWO Oct 11 '21 at 16:03
  • That’s almost useful! You don’t include either your input data nor your attempt to filter it in your MWE app. Both of those would be _very_ useful pieces of information. Please help us to help you. – Limey Oct 11 '21 at 16:07
  • Thank you I have added the data – BWO Oct 11 '21 at 16:31
  • Your code works fine for me. Perhaps you need to reboot your laptop or restart RStudio. – YBS Oct 11 '21 at 19:51

1 Answers1

0

Thank you all. I actually omitted a comma between the verbatimTextOutput statements

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  pickerInput(inputId = "testPicker",
              label = "Select Multiple",
              choices = c(2021, 2020,2019,2018,2017),
              selected = c(2021, 2020,2019,2018,2017),
              multiple = TRUE,
                options = list(
                    `actions-box` = TRUE,
                    "max-options" = 4,
                    "max-options-text" = "You can ONLY select 4 years!"
                )),
  verbatimTextOutput("year1"),
  verbatimTextOutput("year2"),
  verbatimTextOutput("year3")
)

server <- function(input, output, session) {
    
    output$year1 <- renderPrint(input$testPicker[3])
    output$year2 <- renderPrint(input$testPicker[2])
    output$year3 <- renderPrint(input$testPicker[1])
}

shinyApp(ui, server)

I was now able to manipulate my project using the individual values selected. thank you

BWO
  • 95
  • 2
  • 6