I have a Shiny app where I would like to set a conditionalPanel()
depending on whether the user selects a tabPanel
from a tabBox
. My question is how can I reference the panel the user selected. In my example the idea is that if the user selects "Panel1" there appears a conditional panel. As you will see I tried to set the condition in the conditionalPanel()
call as condition = "input.tabs.tab1.box == 'p1'"
.
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
skin = "green",
dashboardHeader(title = "Referencing Box"),
dashboardSidebar(
width = 250,
sidebarMenu(
id = "tabs",
menuItem("First Tab", tabName = "tab1",
menuSubItem("Box", tabName = "box"))
)
),
dashboardBody(
tabItem(
tabName = "box",
tabBox(
id = "tb",
title = "Two Panels",
tabPanel(
id = "p1",
title = "Panel1"
),
tabPanel(
id = "p2",
title = "Panel2"
)
),
conditionalPanel(
condition = "input.tabs.tab1.box == 'p1'",
box(
id = "p3",
title = "Conditional Panel"
)
)
)
)
)
server <- function(input, output) { }
shinyApp(ui, server)
I've seen some examples around where people referenced the upmost layer, so a menuItem
with conditions like condition = "input.tabs == 'anova'"
, but in this case the referenced item is within a tabItem
and further within a tabBox
call.
In your case what you want to reference is what is the active tab of the "tabBox" with the id "tb".
So your condition for the conditional panel should be:
condition = "input.tb == 'p1'"
reference the tabbox that you want to know the active tab of.
Also in order to know which tabPanel is currently active you will also need to add a value parameter to the tabPanel, since id is sadly not enough.
Resulting in this code for the tabPanel
tabPanel(
value = "p1",
id = "p1",
title = "Panel1"
),
All together here is a working solution:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
skin = "green",
dashboardHeader(title = "Referencing Box"),
dashboardSidebar(
width = 250,
sidebarMenu(
id = "tabs",
menuItem("First Tab", tabName = "tab1",
menuSubItem("Box", tabName = "box"))
)
),
dashboardBody(
tabItem(
tabName = "box",
tabBox(
id = "tb",
title = "Two Panels",
tabPanel(
value = "p1",
id = "p1",
title = "Panel1"
),
tabPanel(
id = "p2",
title = "Panel2"
)
),
conditionalPanel(
condition = "input.tb == 'p1'",
box(
id = "p3",
title = "Conditional Panel"
)
)
)
)
)
server <- function(input, output) { }
shinyApp(ui, server)