Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shiny use number of select elments in conditionalPanel

Tags:

r

shiny

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(
...
      )
    )
  )
))
like image 251
mlist Avatar asked Feb 27 '14 09:02

mlist


3 Answers

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...
     }
})
like image 142
amit Avatar answered Nov 11 '22 02:11

amit


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

like image 5
Julien Navarre Avatar answered Nov 11 '22 02:11

Julien Navarre


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"

like image 4
Robin Avatar answered Nov 11 '22 02:11

Robin