Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Shiny reactive variable that indicates which widget was last modified

Let's say we have a set of widgets each with their own input label. How do we create a reactive object whose value is the character that represents the input ID of the last widget that was modified?

For example, if we have

ui.R

shinyUI(fluidPage(
   textInput('txt_a', 'Input Text A'),
   textInput('txt_b', 'Input Text B")
))

server.R

shinyServer(function(input, output) {
   last_updated_widget <- reactive({
      #hypothetical code that indicates ID value of last updated widget
   })
})

The desired result is as follows. If the user modifies the first text box, then the value of last_updated_widget() would be "txt_a". If they modify the second box, the value of last_updated_widget() becomes "txt_b". I'm in search of a result that extends to the obvious generalization of setting the value to be the ID of any of the widgets that was adjusted last.

I'd like this to work for an arbitrary number of widget inputs, including the case that they were generated by a renderUI() statement. So making a separate reactive() statement for each widget isn't an option. However, if the reactive statement requires a loop over all the widget names (or something like that) I can certainly work with that. And multiple reactive statements is okay, as long as it's a fixed amount, and not a function of the number of widgets.

It seems like a pretty simple problem, so I was surprised when it became a roadblock for me. I feel like the solution would be really obvious and I'm just not seeing, so if it is, I apologize for making it a new question. But any help would be greatly appreciated.

like image 204
Bridgeburners Avatar asked Jul 06 '15 16:07

Bridgeburners


People also ask

What does reactive mean in Shiny?

A reactive expression takes input values, or values from other reactive expressions, and returns a new value. Reactive expressions save their results, and will only re-calculate if their input has changed. Create reactive expressions with reactive({ })

What is reactive function?

Reactive functions are functions that can read reactive values and call other reactive functions. Whenever a reactive value changes, any reactive functions that depended on it are marked as "invalidated" and will automatically re-execute if necessary.


1 Answers

Here's a solution that works, though it looks a little awkward because of a nested observe(). I'm not sure what a better way would be, but there could be something nicer.

Basically, use an observe() to loop over all the available inputs, and for each input, use another observe() that will only trigger when that input is changed and set a variable to the id of the input.

runApp(shinyApp(
  ui = shinyUI(
    fluidPage(
      textInput('txt_a', 'Input Text A'),
      textInput('txt_b', 'Input Text B'),
      uiOutput('txt_c_out'),
      verbatimTextOutput("show_last")
    )
  ),
  server = function(input, output, session) {
    output$txt_c_out <- renderUI({
      textInput('txt_c', 'Input Text C')
    })

    values <- reactiveValues(
      lastUpdated = NULL
    )

    observe({
      lapply(names(input), function(x) {
        observe({
          input[[x]]
          values$lastUpdated <- x
        })
      })
    })

    output$show_last <- renderPrint({
      values$lastUpdated
    })
  }
))
like image 175
DeanAttali Avatar answered Sep 30 '22 12:09

DeanAttali