Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting most recently changed reactive expressions in Shiny

Tags:

r

shiny

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.

like image 334
MarkH Avatar asked Sep 04 '14 18:09

MarkH


1 Answers

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

enter image description here

like image 64
jdharrison Avatar answered Oct 13 '22 21:10

jdharrison