I have multiple checkboxGroupButton widgets and a selectizeInput widget in my shiny app. Both types of input widgets utilise the same set of choices, and serve the same purpose. So I want to sync both of them; update the selectizeInput selectedTags, when an option of either of the checkboxGroupButtons are selected, and vice versa.
library(shiny)
library(shinyWidgets)
library(shinyjs)
categoriesList <- c("Research", "Rural Health", "Staff Experience", "Teamwork", "Telehealth", "Transition Care",
"Trauma and Injury Management", "Unwarranted Clinical Variation")
departmentsList <- c("Acute Medicine, Subacute and Community", "Speciality Medicine, Cancer and Critical Care",
"Surgery and Interventional Services", "Children's", "Women's and Newborn", "Mental Health",
"Allied Health and Patient Flow", "Residential Care", "Pathology", "Imaging", "Pharmacy")
organisationsList <- c("Organisation 1", "Organisation 2", "Organisation 3", "Organisation 4",
"Organisation 5", "Organisation 6", "Organisation 7", "Organisation 8",
"Organisation 9", "Organisation 10", "Organisation 11", "Organisation 12")
statusList <- c("Sustained", "Implementation", "Pre-implementation", "Future Initiative")
ui <- fluidPage(
titlePanel("App"),
mainPanel(
selectizeInput("selectedTags", "Select",
choices = list(
Categories = categoriesList,
Departments = departmentsList,
Organisations = organisationsList,
Status = statusList),
multiple = TRUE,
options = list('plugins' = list('remove_button'))),
checkboxGroupButtons(
inputId = "selectedCategories",
choices = categoriesList,
individual = TRUE
),
checkboxGroupButtons(
inputId = "selectedDepartments",
choices = departmentsList,
individual = TRUE
),
checkboxGroupButtons(
inputId = "selectedOrganisations",
choices = organisationsList,
individual = TRUE
),
checkboxGroupButtons(
inputId = "selectedStatus",
choices = statusList,
individual = TRUE
)
)
)
I tried adding observe events for each checkboxGroupButtons as follows
server <- function(input, output, session) {
observeEvent(input$selectedCategories, {
if(input$selectedCategories %in% input$selectedTags)
selected = input$selectedTags[input$selectedTags != input$selectedCategories]
else
selected = c(input$selectedTags, input$selectedCategories)
updateSelectInput(session, "selectedTags",
selected = selected)
})
observeEvent(input$selectedDepartments, {
if(input$selectedDepartments %in% input$selectedTags)
selected = input$selectedTags[input$selectedTags != input$selectedDepartments]
else
selected = c(input$selectedTags, input$selectedDepartments)
updateSelectInput(session, "selectedTags",
selected = selected)
})
observeEvent(input$selectedOrganisations, {
if(input$selectedOrganisations %in% input$selectedTags)
selected = input$selectedTags[input$selectedTags != input$selectedOrganisations]
else
selected = c(input$selectedTags, input$selectedOrganisations)
updateSelectInput(session, "selectedTags",
selected = selected)
})
observeEvent(input$selectedStatus, {
if(input$selectedStatus %in% input$selectedTags)
selected = input$selectedTags[input$selectedTags != input$selectedStatus]
else
selected = c(input$selectedTags, input$selectedStatus)
updateSelectInput(session, "selectedTags",
selected = selected)
})
}
shinyApp(ui = ui, server = server)
But the logic doesn't seem to work
How about this?
library(shiny)
library(shinyWidgets)
library(shinyjs)
categoriesList <- c("Research", "Rural Health", "Staff Experience", "Teamwork", "Telehealth", "Transition Care",
"Trauma and Injury Management", "Unwarranted Clinical Variation")
departmentsList <- c("Acute Medicine, Subacute and Community", "Speciality Medicine, Cancer and Critical Care",
"Surgery and Interventional Services", "Children's", "Women's and Newborn", "Mental Health",
"Allied Health and Patient Flow", "Residential Care", "Pathology", "Imaging", "Pharmacy")
organisationsList <- c("Organisation 1", "Organisation 2", "Organisation 3", "Organisation 4",
"Organisation 5", "Organisation 6", "Organisation 7", "Organisation 8",
"Organisation 9", "Organisation 10", "Organisation 11", "Organisation 12")
statusList <- c("Sustained", "Implementation", "Pre-implementation", "Future Initiative")
ui <- fluidPage(
titlePanel("App"),
mainPanel(
selectizeInput("selectedTags", "Select",
choices = list(
Categories = categoriesList,
Departments = departmentsList,
Organisations = organisationsList,
Status = statusList),
multiple = TRUE,
options = list('plugins' = list('remove_button'))),
checkboxGroupButtons("selectedCategories",choices = categoriesList,individual = TRUE),
checkboxGroupButtons("selectedDepartments", choices = departmentsList,individual = TRUE),
checkboxGroupButtons("selectedOrganisations",choices = organisationsList,individual = TRUE),
checkboxGroupButtons("selectedStatus",choices = statusList,individual = TRUE)
)
)
server <- function(input, output, session) {
observeEvent(input$selectedCategories, {
updateSelectInput(session, "selectedTags", selected = c(input$selectedTags, input$selectedCategories))
})
observeEvent(input$selectedDepartments, {
updateSelectInput(session, "selectedTags", selected = c(input$selectedTags, input$selectedDepartments))
})
observeEvent(input$selectedOrganisations, {
updateSelectInput(session, "selectedTags", selected = c(input$selectedTags, input$selectedOrganisations))
})
observeEvent(input$selectedStatus, {
updateSelectInput(session, "selectedTags", selected = c(input$selectedTags, input$selectedStatus))
})
}
shinyApp(ui = ui, server = server)