10

This is essentially a follow-up with detailed example of this question (no answer came through): conditionalPanel in shiny (doesn't seem to work)

Example app: Displays panels ("list1", "list2", etc.) based on user-selection. "list3" was not selected and should not display.

ui.R

displayList <- c("list1", "list2", "list3")

shinyUI(pageWithSidebar(
  headerPanel("Shiny Display List"),

  sidebarPanel(
    checkboxGroupInput('dlist', 'Display List:', displayList, selected = displayList[1:2])
  ),

  mainPanel(
    h4("Display List"),

    conditionalPanel(condition = "length(intersect(input.dlist, displayList[1])) > 0",
      p("Some List 1 entries")
    ),

    conditionalPanel(condition = "length(intersect(input.dlist, displayList[2])) > 0",
      p("Some List 2 entries")
    ),

    conditionalPanel(condition = "length(intersect(input.dlist, displayList[3])) > 0",
      p("Some List 3 entries") #WASN'T SELECTED, SHOULD NOT DISPLAY INITIALLY
    )
  )
))

server.R

shinyServer(function(input, output) {
  observe({cat(input$dlist, "\n")})
  observe({cat(length(intersect(input$dlist, "list3")))})
})

To test if the condition was met, I ran observe in server.R and the output shows that indeed the condition was not met for panel 3 ("0" below).

list1 list2 
0

But, the app still displays "list3"

enter image description here

Any idea why? I did try different forms of the condition (instead of using intersect etc.) but with no success.

EDIT WITH ANSWER

As @nstjhp & @Julien Navarre point out, the conditionalPanel "condition" needs to be in Javascript. For the example above it works as follows:

conditionalPanel(condition = "input.dlist.indexOf('list1') > -1",
      p("Some List 1 entries")
    )
Community
  • 1
  • 1
harkmug
  • 2,725
  • 22
  • 26
  • The condition has to be in Javascript not R. – nstjhp Mar 10 '14 at 17:28
  • Thanks for the comment. I am trying variations of `'input.dlist in "list3"'`,`"input.dlist.indexOf('list3') > -1"` etc. but my JS-foo is weak. Any help? Is `input.dlist` even an array? Using: http://stackoverflow.com/questions/1181575/javascript-determine-whether-an-array-contains-a-value – harkmug Mar 10 '14 at 17:42

1 Answers1

9

As @nstjhp said the condition has to be in Javascript into a conditional panel, you can't insert R logic here.

If you want to control the inputs with a R syntax you can use renderUI :

For example :

output$panel = renderUI({
    if(input$dlist[1] == TRUE) {
        display something 
    } else if 
.....

Though the condition isn't very different in javascript in your case. It's juste something like : condition = "input.dlist[0]". Note that in javascript indexes start from 0 and not from 1 like in R.

Your main panel :

mainPanel(
  h4("Display List"),

  conditionalPanel(condition = "input.dlist[0]",
                   p("Some List 1 entries")
  ),

  conditionalPanel(condition = "input.dlist[1]",
                   p("Some List 2 entries")
  ),

  conditionalPanel(condition = "input.dlist[2]",
                   p("Some List 3 entries")
  )
)
Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
  • Thanks! I would prefer the second option for its more straightforward application. However, as seen from my response to @nstjhp's comment, can you tailor the JS condition to the OP above? I tried `condition = "input.dlist in displayList[0]"`. – harkmug Mar 10 '14 at 18:22
  • I don't understand sory. This main panel do you want, no ? – Julien Navarre Mar 10 '14 at 18:40
  • Yes, I am just trying to figure out the correct JS condition statement. Thanks! – harkmug Mar 10 '14 at 18:58