Wrapping observeEvent in a user defined function

Hi,

I have this dummy app where we have two input 'boxes' . For each one, we have several choices, but we also have an option where the end user can select the "All" option. Thanks to @scottyd22's assistance , we have a code that works that includes the observeEvent function. Now, I don't want to keep repeating myself for each one of the inputs (as shown below). Is there a way I can wrap the observeEvent function in a user-defined function that only takes in the inputId as an argument?

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  pickerInput("variable1", "Variable1:",
              c("All", "Cylinders" = "cyl", "Transmission" = "am", "Gears" = "gear"),
              multiple = TRUE,
              selected = "All"), 
  pickerInput("variable2", "Variable2:",
              c("All", LETTERS),
              multiple = TRUE,
              selected = "All")
)

server <- function(input, output, session) {
  
  ## variable 1
  
  # tracks the last selection made (starts as NULL)
  last_selection1 = reactiveValues(d = NULL)
  
  # observe each time a selection is made
  observeEvent(input$variable1, {
    
    Selected = input$variable1
    
    # determines which option was just selected
    new_selection = setdiff(Selected, last_selection1$d)
    
    if(length(new_selection) > 0) {
      # if latest selection is "All", only keep "All"
      if(new_selection == 'All') {
        Selected = 'All'
        # if latest selection is not "All", keep everything except "All"
      } else {
        Selected = Selected[Selected != 'All']
      }
    }
    
    # update the input
    updatePickerInput(session = session,
                      inputId = 'variable1',
                      selected = Selected)
    
    # update the last selection made
    last_selection1$d <<- Selected
    
  }, ignoreNULL = F)
  
  
  ## variable 2
  
  # tracks the last selection made (starts as NULL)
  last_selection2 = reactiveValues(d = NULL)
  
  # observe each time a selection is made
  observeEvent(input$variable2, {
    
    Selected = input$variable2
    
    # determines which option was just selected
    new_selection = setdiff(Selected, last_selection2$d)
    
    if(length(new_selection) > 0) {
      # if latest selection is "All", only keep "All"
      if(new_selection == 'All') {
        Selected = 'All'
        # if latest selection is not "All", keep everything except "All"
      } else {
        Selected = Selected[Selected != 'All']
      }
    }
    
    # update the input
    updatePickerInput(session = session,
                      inputId = 'variable2',
                      selected = Selected)
    
    # update the last selection made
    last_selection2$d <<- Selected
    
  }, ignoreNULL = F)
  
  
  
}

shinyApp(ui, server)

Hello,

The Shiny modules is the way to go.

This topic was automatically closed 54 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.