1
votes

In my shiny app I have a selectInput with multiple=True. This allows to select several variables from a list. I would like to add conditionalPanel for every variables of the list which will be display only when the variable is selected.

Exemple :

shinyUI(fluidPage(

    sidebarPanel(     
      selectInput("variables", "Choose  variables" , choices = c("VAR1", "VAR2", "VAR3", "VAR4"), multiple=TRUE),

      conditionalPanel( 
        condition = " input.variables == 'VAR1' ",
        selectInput("VAR1", "values", c("A", "B", "C"))
        )   
    ),
    mainPanel(
      )
    )

The problem is since I choose several variables my condition input$variables == 'VAR1' is false because input.variables contains several values. What condition would work here ? In R the condition would be : "VAR1" %in% input.variables

Thank you

4
You should provide a reproducible example. The server is missing.MLavoie

4 Answers

1
votes

The Javascript condition you're looking for is

input.variables.indexOf('VAR1') > -1
0
votes

You can make multiple conditions using the || so the code looks like:

conditionalPanel(
         condition = c("input$variables == 'VAR1' || input$variables == 'VAR2'"),
#here your follwing selectinput

That means if your input is VAR1 or its VAR2 or both, the conditional panel accept and it goes to the selectinput. also you can use && to make VAR1 and VAR2 etc.

Also if you want to make one conditionalpanel for each variable, don't use multiple = TRUE, and change to a single choice selectinput.

0
votes

Join the selected variables into a character string, then do a pattern matching check. This should work

selected <- paste(input$variables, collapse=",")
if ("VAR1" %in% selected)
0
votes

I search this issue also and figure out that the condition must be follow by Javascript. For anyone also's searching this, Laurent give the correct answer at above.

We can also you the condition below:

conditionalPanel(condition = "input.variables.includes('VAR1')")