具有checkboxInput行为的checkboxGroupInput
我有一个checkboxGroupInput,但我想有一个checkboxInput行为。我希望有独立的反应式功能,这些功能仅在您(取消)勾选具有checkboxInput行为的checkboxGroupInput,r,shiny,R,Shiny,我有一个checkboxGroupInput,但我想有一个checkboxInput行为。我希望有独立的反应式功能,这些功能仅在您(取消)勾选a或b时激活。这在单独的checkboxInputs中很容易实现,但在checkboxGroupInput中不可能实现 ui <- shinyUI(pageWithSidebar( headerPanel("checkboxGroupInput"), sidebarPanel( checkboxGroupInput('
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()
),我可以依赖反应函数,我非常喜欢。谢谢你的回答。我也有这个想法,但没有实现它,因为我有一些关于反应价值观的麻烦经验。然而,我现在看到,这个选项非常复杂,并且真正实现了组和无组输入之间的转换。