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"
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")
)
'input.dlist in "list3"'
,"input.dlist.indexOf('list3') > -1"
etc. but my JS-foo is weak. Any help? Isinput.dlist
even an array? Using: stackoverflow.com/questions/1181575/… – harkmug