Reputation: 6668
I have a checkboxGroupInput, but I want to have checkboxInput behaviour. I want to have independent reactive functions, that are only activated when you (un)tick a
or b
. This is easily possible with seperate checkboxInput
s, but not within a 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)
What I tried in the above example is store the boolean whether a
is in the checkboxGroupInput
. Even if the value stays TRUE
(i.e., clicking b
repeatedly), thingy()
is still getting activated.
Upvotes: 1
Views: 1030
Reputation: 2775
You can store each checkbox in a reactiveValue
. See code.
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)
Upvotes: 1