Question

I would like to show content of my shiny app depending on the number of selected items of a multiselect input. So far I couldn't figure out what the condition should look like to make this work.

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel("select and conditional panel"),
  sidebarPanel(
    selectInput(inputId = "someSelect", multiple=TRUE, label = "Genes:", choices = colnames(someDataFrame), selected = c("ESR1", "CD44")),
  ),
  mainPanel(
    conditionalPanel(
      condition="length(input.someSelect.selected) > 2",
      tabsetPanel(
...
      )
    )
  )
))
Was it helpful?

Solution

You can't use a R function into the condition of the conditional panel. I think your condition should be : input.someSelect.length > 2

OTHER TIPS

It is probably a matter of taste, but I don't like the conditionalPanel construct, because it enters a javascript logic into an R code. Instead I prefer the uiOutput (and the respective renderUI), that can generate dynamic UI. while the conditionalPanel can handle only rather simple conditions, the dynamic UI approach can create conditional appearance that are based on more complex logic and can take advantage of the power of R. it is, however, slightly slower to respond to changes.

if you use this approach your ui.r should look something like:

mainPanel(uiOutput("myConditionalPanel"))

and your server.r would look something like:

output$myConditionalPanel = renderUI({
    if(length(input$someSelect)>2) {
         ## some ui definitions here. for example
         tabsetPanel(
             ...
         )
     } else {
         ## some alternative definitions here...
     }
})

Hopefully this helps someone else having the same issue I had...

Using the above answers I was having issues when trying to make a conditionalPanel only appear when 2 or more items were selected in a selectInput. The uiOutput/renderUi method was buggy and with the condition input.someSelect.length > 1 the conditionalPanel appeared even when nothing was selected in the selectInput.

With the conditionPanel, I needed to include a condition to check whether the selectInput was defined:
"input.someSelect != undefined && input.someSelect.length > 1"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top