How to use an element of checkboxGroupInput as condition within a conditionalPanel

4,609 views
Skip to first unread message

Andres Fonseca

unread,
Nov 12, 2013, 2:52:45 PM11/12/13
to shiny-...@googlegroups.com
Does anyone know how to do this? How do I access whether something is checked within the group?
If it were just one checkbox (we'll call) testBox, I could do something like

conditionalPanel(condition = "input.testBox == true", ...)

but what is the input.testBox equivalent for an element within a group?

On a side note, is there an input$testBox equivalent for an element within a group that can be accessed from server.r?

Thanks!

Stéphane Laurent

unread,
Nov 12, 2013, 3:14:02 PM11/12/13
to shiny-...@googlegroups.com
About your second (R) question, the answer is given in the help page :

?checkboxGroupInput
Create a group of checkboxes that can be used to toggle multiple choices independently. The server will receive the input as a character vector of the selected values.

Now what is exactly your condition in conditionalPanel() ? It could be for example : 

- only one particular element selected : condition = 'input.testBox =="A"' 
- testing if the selected elements belong to a specified list of elements : see https://groups.google.com/d/topic/shiny-discuss/9sMPLfX_LxA/discussion
- for other conditions, I don't know the Javascript syntax

Andres Fonseca

unread,
Nov 12, 2013, 3:47:05 PM11/12/13
to shiny-...@googlegroups.com
Great thread! My condition should be whether something within the checkboxGroupInput is checked or not. But because input.myCheckboxGroup is a list (I think),
the syntax described in the thread doesn't quite work. Wouldn't it be checking to see if the list itself has an index in the array (which of course it won't)

conditionalPanel(
  condition = "['Same Faclity Type','Same Class of Trade','Same Tax Status','Specify Population Served','Specify Bed Availability'].indexOf(input.peerParameters) != -1", 
  numericInput("msa_k", "Census Variability", value = .3, min = 0, max = Inf, step = 0.1))

But what goes in indexOf()? I don't think input.peerParameters does.

Stéphane Laurent

unread,
Nov 12, 2013, 4:05:24 PM11/12/13
to shiny-...@googlegroups.com
If I understand (not sure),  this should be like : 

 "input.peerParameters.indexOf('Same Faclity Type') != -1" AND  "input.peerParameters.indexOf('Same Class of Trade') != -1" AND ...  

but I don't know how to do "AND", and there should be an easier way.

Stéphane Laurent

unread,
Nov 12, 2013, 4:06:55 PM11/12/13
to shiny-...@googlegroups.com
Or rather OR (not AND), but I don't know.

Andres Fonseca

unread,
Nov 12, 2013, 4:11:25 PM11/12/13
to shiny-...@googlegroups.com
that works! thank you!

Jeff Bruce

unread,
Jul 31, 2015, 4:37:05 PM7/31/15
to Shiny - Web Framework for R, clubaf...@gmail.com
I am trying to accomplish the same thing as the OP, but unfortunately Stephane's suggestion does not work.

I have the following inside ui.R:

shinyUI(
  navbarPage
(title='Menu',
    tabPanel
('Overview',
      fluidPage
(
        sidebarPanel
(
          checkboxGroupInput
(inputId='sidebarOptions',
                             label
=h3('Show:'),
                             choices
=c('Legend Options', 'Clustering Options', 'Filtering Options'),
                             selected
='Clustering Options'),
          conditionalPanel
(condition="input.sidebarOptions.indexOf('Legend Options') > -1)",
            stuff
         
)
       
)
     
)
   
)
 
)
)

I've also tried != -1 but to no success.  No idea how to start debugging this because I am not well-versed in Javascript.  Hopefully someone can help ._.

Winston Chang

unread,
Jul 31, 2015, 5:11:22 PM7/31/15
to Jeff Bruce, Shiny - Web Framework for R, clubaf...@gmail.com
You had an extra closing paren after the -1. This works for me (as a single-file app):

library(shiny)

server <- function(input, output) {}

ui <- navbarPage(title='Menu',
  tabPanel('Overview',
    fluidPage(
      sidebarPanel(
        checkboxGroupInput(inputId='sidebarOptions', 
          label=h3('Show:'),
          choices=c('Legend Options', 'Clustering Options', 'Filtering Options'),
          selected='Clustering Options'
        ),
        conditionalPanel(condition="input.sidebarOptions.indexOf('Legend Options') != -1",
          "stuff"
        )
      )
    )
  )
)

shinyApp(ui = ui, server = server)



--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/6d27c3b2-0da6-4d74-aa90-ab7b3317e405%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages