Reputation: 77
I'm building an app with shinyDashboard. I want to display several selectInput in sidebarMenu regarding the selected tabItem AND tabPanel. The same selectInput are used in different tabItem.
It looks simple but I struggle with the conditional syntax in conditionalPanel using multiples arguments with both AND (&&), OR (||) and IN (%in%) operators. I tried to add bracket but it is not doing the job.
I wrote this code, with is reproductible and working but not doing what I want as its always display the selectInputs.
library(shinydashboard)
library(dplyr)
mtcars$gear <- as.character(mtcars$gear)
all_gears <- sort(unique(mtcars$gear))
mtcars$cyl <- as.character(mtcars$cyl)
all_cyl <- sort(unique(mtcars$cyl))
ui <- dashboardPage(
dashboardHeader(title = "test"),
dashboardSidebar(
sidebarMenu(id="menu1",
menuItem(
"Dashboard",
tabName = "dashboard",
icon = icon("dashboard")
),
menuItem(
"Indicators",
tabName = "indicators",
icon = icon("info-circle")
)
),
conditionalPanel(
condition = "input.menu1 == 'dashboard' && input.tabselected %in% c('1','2')",
selectInput(
inputId = "cylinders",
label = "Select number of cylinders",
choices = all_cyl,
selected = '4',
multiple = TRUE,
selectize = FALSE
)
),
conditionalPanel(
condition = "(input.menu1 == 'dashboard' && input.tabselected == 2) || input.menu1 == 'indicators'",
selectInput(
inputId = "gearsnumber",
label = "Select number of gears",
choices = all_gears,
selected = '3',
multiple = TRUE,
selectize = FALSE
)
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
tabsetPanel(
tabPanel("Graph", value=1, plotOutput("plot")),
tabPanel("Table", value=2, dataTableOutput("table")),
tabPanel("Empty", value=3)
)
),
tabItem(tabName = "indicators",
infoBoxOutput("totalweight")
)
)
)
)
server <- function(input, output, session) {
selectedDatacyl <- reactive({
req(input$cylinders)
df <- as.data.frame(mtcars)
df$gear <- as.character(df$gear)
df$cyl <- as.character(df$cyl)
df <- mtcars
df %>% dplyr::filter(cyl %in% input$cylinders)
})
selectedDatagears <- reactive({
req(input$gearsnumber)
df <- selectedDatacyl()
df %>% dplyr::filter(gear %in% input$gearsnumber)
})
output$plot <- renderPlot({
ggplot( data = selectedDatacyl(), aes(x = rownames(selectedDatacyl()), y = mpg)) + geom_point()
})
output$table <- DT::renderDataTable({
DT::datatable( data = selectedDatagears(),
options = list(pageLength = 14),
rownames = FALSE)
})
output$totalweight <- renderInfoBox({
infoBox(
"Total weight",
paste0(sum(selectedDatagears()$wt), "lbs"),
icon = icon("chart-area"),
color = "green"
)
})
}
shinyApp(ui = ui, server = server)
What should I do to make thoses conditions operational? Thanks to all contribs.
Upvotes: 4
Views: 2219
Reputation: 84519
The condition in conditionalPanel
is a JavaScript expression, not a R expression.
So you have to replace
input.menu1 == 'dashboard' && input.tabselected %in% c('1','2')
with
input.menu1 == 'dashboard' && (input.tabselected == '1' || input.tabselected == '2')
or
input.menu1 == 'dashboard' && ['1','2'].indexOf(input.tabselected) > -1
Upvotes: 6