创建闪亮的反应变量,指示最后修改哪个小部件 [英] Creating Shiny reactive variable that indicates which widget was last modified

查看:41
本文介绍了创建闪亮的反应变量,指示最后修改哪个小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一组小部件,每个小部件都有自己的输入标签.我们如何创建一个响应式对象,其值是代表最后修改的小部件的输入 ID 的字符?

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?

例如,如果我们有

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

想要的结果如下.如果用户修改了第一个文本框,则 last_updated_widget() 的值将是 "txt_a".如果他们修改第二个框,last_updated_widget() 的值变为 "txt_b".我正在寻找一个结果,该结果扩展到将值设置为最后调整的任何小部件的 ID 的明显概括.

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.

我希望它适用于任意数量的小部件输入,包括它们由 renderUI() 语句生成的情况.因此,为每个小部件制作单独的 reactive() 语句不是一种选择.但是,如果反应式语句需要对所有小部件名称(或类似名称)进行循环,我当然可以使用它.并且多个响应式语句是可以的,只要它是一个固定的数量,而不是小部件数量的函数.

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.

推荐答案

这里有一个可行的解决方案,尽管由于嵌套的 observe() 看起来有点尴尬.我不确定更好的方法是什么,但可能会有更好的方法.

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.

基本上,使用 observe() 来循环所有可用的输入,并且对于每个输入,使用另一个 observe() 只会在该输入被触发时触发更改并将变量设置为输入的 id.

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

这篇关于创建闪亮的反应变量,指示最后修改哪个小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆