I'm building an R shiny app with some filters that I want to apply on different pages and I want some of these filters to create extra inputs. I've been using conditionalPanel to show/hide the extra inputs but I'm having a problem with hiding the extra inputs when you go onto a different page.
In the example below if you select an age range, this age range is selected on all pages. If you select 60+ you get an additional input for gender and when you select a gender and change page this is applied on all the other pages. But, if you then select a different age range on a different page the gender conditionalPanel isn't hidden until you click on a different page. If I change the || to && in the condition statement it solves the problem I get another problem where the conditional panel doesn't show until you click on a different page.
library(shiny)
library(shinydashboard)
library(shinyWidgets)
agePanelUI <- function(id) {
ns <- NS(id)
fluidRow(
column(width = 9,
uiOutput(ns("select_age")))
)
}
agePanel <- function(input, output, session, x) {
output$select_age <- renderUI({
input$resetInput
radioGroupButtons(
inputId = paste0(x, "age_choice"),
choices = c('0-18', '19-34', "35-59", "60+"),
selected = '0-18')
})
}
filterPanelUI <- function(id) {
ns <- NS(id)
fluidRow(
column(width = 9,
uiOutput(ns('select_gender'))))
}
filterPanel <- function(input, output, session, x) {
output$select_gender <- renderUI({
input$resetInput
pickerInput(
inputId = paste0(x, "gender_choice"),
choices = c('F', 'M'),
options = list(`actions-box` = TRUE,`selected-text-format` = "count > 3"),
multiple = TRUE
)
})
}
ui <- dashboardPage(
header = dashboardHeader(
title = "testing",
titleWidth = 250),
dashboardSidebar(
width = 250,
sidebarMenu(
id = "menu",
menuItem("Information", tabName = "info"),
menuItem("page 1", tabName = "page1"),
menuItem("page 2", tabName = "page2"),
menuItem("page 3", tabName = "page3"))),
dashboardBody(
tabItems(tabItem("info"),
tabItem(tabName = "page1",
box(width = 12, status = "primary", solidHeader = TRUE,
fluidRow(
column(width = 9, h4("Selection"))),
agePanelUI(id = "id_11"),
conditionalPanel(
condition = "input.first_age_choice == '60+' || input.second_age_choice == '60+' || input.third_age_choice == '60+'",
filterPanelUI(id = "id_1")))),
tabItem(tabName = "page2",
box(width = 12, status = "primary", solidHeader = TRUE,
fluidRow(
column(width = 9, h4("Selection"))),
agePanelUI(id = "id_12"),
conditionalPanel(
condition = "input.first_age_choice == '60+' || input.second_age_choice == '60+' || input.third_age_choice == '60+'",
filterPanelUI(id = "id_2"))
)),
tabItem(tabName = "page3",
box(width = 12, status = "primary", solidHeader = TRUE,
fluidRow(
column(width = 9, h4("Selection"))),
agePanelUI(id = "id_13"),
conditionalPanel(
condition = "input.first_age_choice == '60+' || input.second_age_choice == '60+' || input.third_age_choice == '60+'",
filterPanelUI(id = "id_3"))
)))))
server <- function(input, output, session) {
callModule(module = filterPanel, id = "id_1", x = "first_")
callModule(module = filterPanel, id = "id_2", x = "second_")
callModule(module = filterPanel, id = "id_3", x = "third_")
callModule(module = agePanel, id = "id_11", x = "first_")
callModule(module = agePanel, id = "id_12", x = "second_")
callModule(module = agePanel, id = "id_13", x = "third_")
tab_select <- reactiveValues()
observe({
if (input$menu == "page1") {
tab_select$gender <- input$first_gender_choice
tab_select$age <- input$first_age_choice
} else if(input$menu == "page2") {
tab_select$gender <- input$second_gender_choice
tab_select$age <- input$second_age_choice
} else if(input$menu == "page3") {
tab_select$gender <- input$third_gender_choice
tab_select$age <- input$third_age_choice
}
})
observeEvent(input$menu, {
updatePickerInput(session, inputId = "first_gender_choice", selected = tab_select$gender)
updatePickerInput(session, inputId = "second_gender_choice", selected = tab_select$gender)
updatePickerInput(session, inputId = "third_gender_choice", selected = tab_select$gender)
updateRadioGroupButtons(session, inputId = "first_age_choice", selected = tab_select$age)
updateRadioGroupButtons(session, inputId = "second_age_choice", selected = tab_select$age)
updateRadioGroupButtons(session, inputId = "third_age_choice", selected = tab_select$age)
})
}
shinyApp(ui, server)