Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting multiple checkbox values in Shiny

Tags:

r

shiny

I have a series of checkboxes in a side panel, defined as follows inside a sidebarPanel:

sliderInput("days", 
                "Days to monitor:", 
                min = 1, 
                max = 50, 
                value = 5),
    checkboxInput("display_CMC", "Carolinas Medical Center", TRUE),
    checkboxInput("display_MariaParham", "Maria Parham", TRUE),
    checkboxInput("display_WakeMed", "Wake Med", TRUE)

I'd like to transform those results into a character vector in a programmatic way (e.g. if I had a checkboxInput with a name 'display_ I want it to automatically parse the results). To do that, I tried:

displayIdx <- grep( "display_", names(input) )
facilityCode_keep <- names(input)[ displayIdx ][ input[ displayIdx ] ]

Unfortunately, that results in:

Error: Single-bracket indexing of reactivevalues object is not allowed.

Two questions:

  1. How do I transform a series of checkboxInputs into a character vector?
  2. What's up with the disallowing single bracket indexing. I could understand if I was trying to assign to input, but I'm not.
like image 530
Ari B. Friedman Avatar asked May 20 '13 17:05

Ari B. Friedman


1 Answers

How do I transform a series of checkboxInputs into a character vector?

You can use double-bracket indexing. But in this case you might consider using checkboxGroupInput function instead, which returns a character vector of the selected values.

Why not single-bracket indexing?

Single-bracket indexing is subsetting, while double-bracket indexing is element retrieval (or something like that). The input object is more like a map/hash/dict/environment than like a named vector, so subsetting doesn't really make sense. (Similarly, you can't single-bracket index on R's environment objects, you can only double-bracket index.)

like image 70
Joe Cheng Avatar answered Oct 22 '22 11:10

Joe Cheng