具有checkboxInput行为的checkboxGroupInput

具有checkboxInput行为的checkboxGroupInput,r,shiny,R,Shiny,我有一个checkboxGroupInput,但我想有一个checkboxInput行为。我希望有独立的反应式功能,这些功能仅在您(取消)勾选a或b时激活。这在单独的checkboxInputs中很容易实现,但在checkboxGroupInput中不可能实现 ui <- shinyUI(pageWithSidebar( headerPanel("checkboxGroupInput"), sidebarPanel( checkboxGroupInput('

我有一个checkboxGroupInput,但我想有一个checkboxInput行为。我希望有独立的反应式功能,这些功能仅在您(取消)勾选
a
b
时激活。这在单独的
checkboxInput
s中很容易实现,但在
checkboxGroupInput
中不可能实现

ui <- shinyUI(pageWithSidebar(
    headerPanel("checkboxGroupInput"),
    sidebarPanel(
        checkboxGroupInput('sources',
                           label='Sources',
                           choices=list('a'='a',
                                        'b'='b')),
        checkboxInput('a',
                      'a'),
        checkboxInput('b',
                      'b')
    ),
    mainPanel(
        # empty
    )
))

server <- shinyServer(function(input, output) {
    thingy <- reactive({
        return('a' %in% input$sources)
    })
    observeEvent(thingy(), {
        print("I'm here (a+b)")
    })
    observeEvent(input$a, {
        print("I'm here (a only)")
    })
    observeEvent(input$b, {
        print("I'm here (b only)")
    })

})

shinyApp(ui=ui,server=server) 

ui您可以将每个复选框存储在
reactiveValue
中。参见代码

 ui <- shinyUI(pageWithSidebar(
  headerPanel("checkboxGroupInput"),
  sidebarPanel(
    checkboxGroupInput('sources',
                       label='Sources',
                       choices=list('a'='a',
                                    'b'='b')),
    checkboxInput('a',
                  'a'),
    checkboxInput('b',
                  'b')
  ),
  mainPanel(
    # empty
  )
))

server <- shinyServer(function(input, output) {

  rv <- reactiveValues(a=FALSE, b=FALSE) 

  observe( {
    is.a <- 'a' %in% input$sources 
    if (  rv$a != is.a){
      rv$a <- is.a
    } 
    is.b <- 'b' %in% input$sources 
    if (  rv$b != is.b){
      rv$b <- is.b
    }  
  })   

  # thingy <- reactive({
  #     return('a' %in% input$sources)
  # })
  # observeEvent(thingy(), {
  #     print("I'm here (a+b)")
  # })
  # 

  observeEvent(rv$a, {
    print("a only")
  })

  observeEvent(rv$b, {
    print("b only")
  }) 

  observeEvent(input$a, {
    print("I'm here (a only)")
  })

  observeEvent(input$b, {
    print("I'm here (b only)")
  })

})

shinyApp(ui=ui,server=server) 

ui我认为
observeEvent
的第一个参数就是它应该响应什么事件,而不管它的布尔值如何。因此,每次输入$sources更改时,它都会触发。您可以添加一个
if
来检查
observe
@NicE中
thingy
的值。问题是
observeEvent
中有多个反应源,来自
checkboxGroupInput
的每个值都需要自己的函数,从而导致多个ifs。我觉得这很麻烦(需要存储“旧”设置、比较、存储“新”设置),而且可以更容易地实现。使用预期的解决方案(为每个输入创建一个
thingy()
),我可以依赖反应函数,我非常喜欢。谢谢你的回答。我也有这个想法,但没有实现它,因为我有一些关于反应价值观的麻烦经验。然而,我现在看到,这个选项非常复杂,并且真正实现了组和无组输入之间的转换。