在Shiny中获取多个复选框值

时间:2022-02-10 20:35:59

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

我在侧面板中有一系列复选框,在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:

我想以编程方式将这些结果转换为字符向量(例如,如果我有一个名为'display_的checkboxInput,我希望它自动解析结果)。为此,我试过:

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. 如何将一系列checkboxInput转换为字符向量?

  3. What's up with the disallowing single bracket indexing. I could understand if I was trying to assign to input, but I'm not.
  4. 不允许的单括号索引是怎么回事。我能理解我是否正在尝试分配输入,但我不是。

1 个解决方案

#1


9  

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

如何将一系列checkboxInput转换为字符向量?

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.

您可以使用双括号索引。但在这种情况下,您可以考虑使用checkboxGroupInput函数,它返回所选值的字符向量。

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.)

单括号索引是子集,而双括号索引是元素检索(或类似的东西)。输入对象更像是map / hash / dict / environment而不是命名向量,因此子集化并不真正有意义。 (同样,你不能在R的环境对象上单括号索引,你只能双括号索引。)

#1


9  

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

如何将一系列checkboxInput转换为字符向量?

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.

您可以使用双括号索引。但在这种情况下,您可以考虑使用checkboxGroupInput函数,它返回所选值的字符向量。

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.)

单括号索引是子集,而双括号索引是元素检索(或类似的东西)。输入对象更像是map / hash / dict / environment而不是命名向量,因此子集化并不真正有意义。 (同样,你不能在R的环境对象上单括号索引,你只能双括号索引。)