I have a reactive expression, the value of which I want to take from whichever of two other reactive expressions has been changed most recently. I've made the following example:
ui.r:
shinyUI(bootstrapPage(
column(4, wellPanel(
actionButton("button", "Button"),
checkboxGroupInput("check", "Check", choices = c("a", "b", "c"))
)),
column(8,
textOutput("test")
)
))
And server.r:
shinyServer(function(input, output) {
output$test <- renderText({
# Solution goes here
})
})
I would like for the output to show the value of either button
(the number of times the button has been clicked) or check
(a character vector showing which boxes are checked) depending on which was changed most recently.
You can achieve this using reactiveValues
to keep track of the current state of button presses:
library(shiny)
runApp(list(ui = shinyUI(bootstrapPage(
column(4, wellPanel(
actionButton("button", "Button"),
checkboxGroupInput("check", "Check", choices = c("a", "b", "c"))
)),
column(8,
textOutput("test")
)
))
, server = function(input, output, session){
myReactives <- reactiveValues(reactInd = 0)
observe({
input$button
myReactives$reactInd <- 1
})
observe({
input$check
myReactives$reactInd <- 2
})
output$test <- renderText({
if(myReactives$reactInd == 1){
return(input$button)
}
if(myReactives$reactInd == 2){
return(input$check)
}
})
}
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With