Hi, andrii. I would suggest the following modification of the script using reactiveValues. The two observe functions will change the sync value when selectizeInput value changed. And the sync value will update the selected of the selectizeInputs.
And I move the selectizeInputs script back to ui because if rendering ui in server, the selectizeInput on page two will not available initially and cannot update. And the selectizeInput on page two can be only rendering lazily when you click tab "Two".
library(shiny)
# 1. Values
list_values <- c(1:3)
names(list_values) <- c("One", "Two", "Three")
# 2. Index of selected element
index_selected <- 1
server <- function(input, output, session) {
vals <- reactiveValues(sync = 1)
observe(
{
req(input$si_page_one_selector)
vals$sync <- input$si_page_one_selector
}
)
observe(
{
req(input$si_page_two_selector)
vals$sync <- input$si_page_two_selector
}
)
observe(
{
req(vals$sync)
updateSelectizeInput(session, 'si_page_one_selector', selected = vals$sync)
updateSelectizeInput(session, 'si_page_two_selector', selected = vals$sync)
}
)
}
ui <- fluidPage(
navbarPage("Demo",
tabPanel("One",
fluidPage(
fluidRow(selectizeInput('si_page_one_selector', "Selector",
choices = list_values,
multiple = FALSE,
selected = list_values[index_selected])),
fluidRow("Page one"))),
tabPanel("Two",
fluidPage(
fluidRow(selectizeInput('si_page_two_selector', "Selector",
choices = list_values,
multiple = FALSE,
selected = list_values[index_selected])),
fluidRow("Page two")))
)
)
# Run the application
shinyApp(ui, server)