Updating selectInput in shiny app based on user selection

I have a simple shiny app with 2 select input :

shinyApp(
  ui = fluidPage(
    selectInput("Params1", "Parameter 1",choices=c('Significant wave height [m]' = 'Significant wave height',
                                                   'Peak period [s]' = 'Peak period', 
                                                   'Wind speed at 10m [m/s]' = 'Wind speed at 10m', 
                                                   'Surface current speed [m/s]' = 'Surface current speed'),selected = 'Significant wave height')
    ,selectInput("Params2", "Parameter 2", choices=c('Significant wave height [m]' = 'Significant wave height',
                                                     'Peak period [s]' = 'Peak period', 
                                                     'Wind speed at 10m [m/s]' = 'Wind speed at 10m', 
                                                     'Surface current speed [m/s]' = 'Surface current speed'),selected = 'Peak period')
  ),
  server = function(input, output,session) {
    observeEvent(input$Params1, {
      available_choices <- setdiff(c('Significant wave height [m]', 'Peak period [s]', 'Wind speed at 10m [m/s]', 'Surface current speed [m/s]'), input$Params1)
      updateSelectInput(session, "Params2", choices = available_choices, selected = input$Params2)
    })
    
    observeEvent(input$Params2, {
      available_choices <- setdiff(c('Significant wave height [m]', 'Peak period [s]', 'Wind speed at 10m [m/s]', 'Surface current speed [m/s]'), input$Params2)
      updateSelectInput(session, "Params1", choices = available_choices, selected = input$Params1)
    })
  }
)

I’m expecting upon app initiation, both select inputs appears with the selected choice, but they are not !
Am I missing something here ?!!

You initialize both selectInput with choices which are named, e.g. 'Peak period [s]' = 'Peak period'. From ?selectInput:

choices: List of values to select from. If elements of the list are
named, then that name — rather than the value — is displayed to the
user. …

Now when the app is started, both of your observeEvent are firing, this is because they have ignoreInit = FALSE by default, from ?observeEvent:

ignoreInit: If TRUE, then, when this observeEvent is first
created/initialized, ignore the handlerExpr (the second argument),
whether it is otherwise supposed to run or not. The default is FALSE.
See Details.

In these observeEvent you update both selectInput with e.g. input$Params2, but input$Params2 is the displayed value and hence can’t match the available choices, that’s why it comes to an empty field, the value which shall be set as selected is not among the available choices. However, once one ran through both observeEvent it will work because then you have updated the choices and the updated versions do not have names and carry directly the unit signs.

Below you find a working version of your code, I just used a list instead of a vector and so the structure keeps preserved.

library(shiny)

chc <- list('Significant wave height [m]' = 'Significant wave height',
            'Peak period [s]' = 'Peak period',
            'Wind speed at 10m [m/s]' = 'Wind speed at 10m',
            'Surface current speed [m/s]' = 'Surface current speed')

ui <- fluidPage(
  selectInput(
    "Params1",
    "Parameter 1",
    choices = chc,
    selected = 'Significant wave height'
  )
  ,
  selectInput(
    "Params2",
    "Parameter 2",
    choices = chc,
    selected = 'Peak period'
  )
)

server <- function(input, output, session) {
  observeEvent(input$Params1, {
    available_choices <- setdiff(chc, input$Params1)
    updateSelectInput(session,
                      "Params2",
                      choices = available_choices,
                      selected = input$Params2)
  })
  
  observeEvent(input$Params2, {
    available_choices <- setdiff(chc, input$Params2)
    updateSelectInput(session,
                      "Params1",
                      choices = available_choices,
                      selected = input$Params1)
  })
}

shinyApp(ui, server)

2

Here is the corrected version of your code:

library(shiny)

  ui = fluidPage(
    selectInput("Params1", "Parameter 1",choices=c('Significant wave height [m]',
                                                   'Peak period [s]', 
                                                   'Wind speed at 10m [m/s]', 
                                                   'Surface current speed [m/s]'),selected = 'Significant wave height [m]')
    ,selectInput("Params2", "Parameter 2", choices=c('Significant wave height [m]',
                                                     'Peak period [s]', 
                                                     'Wind speed at 10m [m/s]', 
                                                     'Surface current speed [m/s]'),selected = 'Peak period [s]')
  )
  server = function(input, output,session) {
    observeEvent(input$Params1, {
      available_choices <- setdiff(c('Significant wave height [m]', 'Peak period [s]', 'Wind speed at 10m [m/s]', 'Surface current speed [m/s]'), input$Params1)
      updateSelectInput(session, "Params2", choices = available_choices, selected = input$Params2)
    })
    
    observeEvent(input$Params2, {
      available_choices <- setdiff(c('Significant wave height [m]', 'Peak period [s]', 'Wind speed at 10m [m/s]', 'Surface current speed [m/s]'), input$Params2)
      updateSelectInput(session, "Params1", choices = available_choices, selected = input$Params1)
    })
  }

  shinyApp(ui,server)

In the selectInput() function, you do not need to assign values to your choices yet. That is why the selected choice was not being displayed. You will only do that in the server side of the code when you want to make use of the choices by doing something like this

switch(inputs$params1,
'Significant wave height [m]' = 'Significant wave height',
'Peak period [s]' = 'Peak period', 
'Wind speed at 10m [m/s]' = 'Wind speed at 10m', 
'Surface current speed [m/s]' = 'Surface current speed')

The above code will enable you to select each input and use its value in your application.

I hope this helps!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật