简体   繁体   中英

Shinydashboard multiple conditions in conditionalPanel with same inputs

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM