R Shiny conditionalPanel displays when condition is not met -
this follow-up detailed example of question (no answer came through): conditionalpanel in shiny (doesn't seem work)
example app: displays panels ("list1", "list2", etc.) based on user-selection. "list3" not selected , 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 ) ) ))
server.r
shinyserver(function(input, output) { observe({cat(input$dlist, "\n")}) observe({cat(length(intersect(input$dlist, "list3")))}) })
to test if condition met, ran observe
in server.r , output shows indeed condition not met panel 3 ("0" below).
list1 list2 0
but, app still displays "list3"
any idea why? did try different forms of condition (instead of using intersect
etc.) no success.
edit answer
as @nstjhp & @julien navarre point out, conditionalpanel
"condition" needs in javascript. example above works follows:
conditionalpanel(condition = "input.dlist.indexof('list1') > -1", p("some list 1 entries") )
as @nstjhp said condition has in javascript conditional panel, can't insert r logic here.
if want control inputs r syntax can use renderui
:
for example :
output$panel = renderui({ if(input$dlist[1] == true) { display } else if .....
though condition isn't different in javascript in case. it's juste : condition = "input.dlist[0]"
. note in javascript indexes start 0 , not 1 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") ) )
Comments
Post a Comment