4

what I am trying to do

I would like to iterate through a tibble and create multiple observeEvents. I have a reproducible example below. The commented out code works, but I would like to create the observeEvents programmatically with pwalk.

Basically I am trying to accomplish something similar as this post: using purrr::walk to instate multiple event observers. (Different goal though. My object is to update the selectInputs across tabs when one selectInput is changed.)

reprex

library(shiny)
library(purrr)
library(tibble)

choices1 <- c('A', 'B', 'C', 'D')
choices2 <- c('E', 'F', 'G', 'H')

dat_inputs <- tribble(
  ~ observe_input, ~ input_id,
  'tab11',         'tab21',
  'tab11',         'tab31',
  'tab21',         'tab11',
  'tab21',         'tab31',
  'tab31',         'tab11',
  'tab31',         'tab21'
  
)

ui <- navbarPage(
  title = 'Test Navbar Page',
  tabPanel(
    title = 'Tab 1',
    selectInput('tab11', 'Tab 11', choices = choices1
    ),
    selectInput('tab12', 'Tab 12', choices = choices2
    )
    ),
  tabPanel(
    title = 'Tab 2',
    selectInput('tab21', 'Tab 22', choices = choices1
    ),
    selectInput('tab12', 'Tab 12', choices = choices2
    )
    ),
  tabPanel(
    title = 'Tab 3',
    selectInput('tab31', 'Tab 31', choices = choices1
    ),
    selectInput('tab32', 'Tab 32', choices = choices2
    )
    )
)

server <- function(input, output, session) {
  
  # observeEvent(input$tab11, {
  #   updateSelectInput(inputId = 'tab21', selected = input$tab11)
  #   updateSelectInput(inputId = 'tab31', selected = input$tab11)
  # })
  # 
  # observeEvent(input$tab21, {
  #   updateSelectInput(inputId = 'tab11', selected = input$tab21)
  #   updateSelectInput(inputId = 'tab31', selected = input$tab21)
  # })
  # 
  # observeEvent(input$tab31, {
  #   updateSelectInput(inputId = 'tab11', selected = input$tab31)
  #   updateSelectInput(inputId = 'tab21', selected = input$tab31)
  # })
  
# edit - commenting out the old code so that the solution takes its place
  
  # this code does not work
  # if you delete handler.quoted = TRUE the shiny app runs but the
  # observeEvents don't work
  
  # pwalk(
#   dat_inputs,
#   ~ observeEvent(
#     input[[.x]],
#     updateSelectInput(inputId = input[[.y]], selected = input[[.x]]),
#     handler.quoted = TRUE
#   )
# )

################################
# solution from accepted answer:
################################
pwalk(
    dat_inputs,
    ~ observeEvent(
        input[[.x]],
        updateSelectInput(inputId = .y, selected = input[[.x]],
                          session = session)
        )
  ) 
  
}

shinyApp(ui, server)
Avery Robbins
  • 132
  • 1
  • 9

1 Answers1

2

Well, three things, you are using input=input[[.y]] rather than just input=.y; when you use the expression like that it's actually not quoted; and you need to pass along the session=. Try

pwalk(
    dat_inputs,
    ~ observeEvent(
      input[[.x]],
      updateSelectInput(inputId = .y, selected = input[[.x]], session=session)
    )
  )
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you! Your solution worked. I obviously didn't realize I was trying to use input[[.y]] instead of .y haha. I am curious about the session=session part. The code seems to work with and without that part. Why add it then? Thanks again. – Avery Robbins Aug 31 '21 at 13:34
  • I was still using `shiny 1.5` where `session=` is required. It appears it is no longer required in `shiny 1.6` – MrFlick Aug 31 '21 at 17:23