【问题标题】:Is there a way to restrict reactions to user updated elements?有没有办法限制对用户更新元素的反应?
【发布时间】:2015-10-28 17:59:05
【问题描述】:

在我的 RStudio 闪亮应用中,我有更新 selectInputs 的观察者。有没有办法避免一连串的反应?如果用户更改 input1,我希望它更新 input2。但我不希望这触发对 input3 的更新,如下所示。

例如,如果我可以将等效的 isolate 放在周围并更新如下:

isolate(updateSelectInput())

这样它会进行初始更新,但不会触发任何其他更改。

这可能吗?

server<-function(session, input, output) {


  observeEvent(input$input1, {
    updateSelectInput(session, "input2", selected="3")

  })


  observeEvent(input$input2, {
    updateNumericInput(session, "input3",value=round(rnorm(1),2))
    # resetting input 2
    updateSelectInput(session, "input2", selected="1")
  })


}

ui<-fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
        selectInput("input1", label="Input1", choices=c("a", "b", "c"), selected="a"),
        selectInput("input2", label="Input2", choices=c("1", "2", "3"), selected="1"),
        numericInput("input3", label="Input3", value=3)
    ),

    mainPanel(

    )
  )
)

shinyApp(ui=ui, server=server)

【问题讨论】:

  • 你的应用对我来说没有意义,你正在更新input1,因此它会更新input2,因此input3也会更新。现在,如果您更新 input2,那么 input3 就会更新。

标签: r shiny rstudio


【解决方案1】:

我有点同意 Pork Chop 的观点,您可能最好重新考虑它的设计并以其他方式触发更新(但我猜您最了解您的应用程序)。有一个简单而丑陋的方法可以解决这个问题,并且仍然有相同的观察者:

server<-function(session, input, output) {
  run.once <<- FALSE

  observeEvent(input$input1, {
    run.once <<- TRUE
    updateSelectInput(session, "input2", selected="3")
  })


  observeEvent(input$input2, {
    if (run.once==TRUE){
      run.once <<- FALSE
      return(NULL) 
    } 
    updateNumericInput(session, "input3",value=round(rnorm(1),2))
    # resetting input 2
    updateSelectInput(session, "input2", selected="1")
  })


}

ui<-fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput("input1", label="Input1", choices=c("a", "b", "c"), selected="a"),
      selectInput("input2", label="Input2", choices=c("1", "2", "3"), selected="1"),
      numericInput("input3", label="Input3", value=3)
    ),

    mainPanel(

    )
  )
)

shinyApp(ui=ui, server=server)

编辑 1 一个更通用的解决方案,但仍然很丑陋。

server<-function(session, input, output) {
  triggerd.by <<- NULL

  observeEvent(input$input1, {
    if (is.null(triggerd.by)) triggerd.by <<- "input1"
    if (triggerd.by != "input1") return(NULL)
    updateSelectInput(session, "input2", selected="3")
    reactiveTimer(10,{triggerd.by<<-NULL})
  })


  observeEvent(input$input2, {
    print(triggerd.by)
    if (is.null(triggerd.by)) triggerd.by <<- "input2"
    if (triggerd.by != "input2") return(NULL)
    updateNumericInput(session, "input3",value=round(rnorm(1),2))
    updateSelectInput(session, "input2", selected="1")
    print(triggerd.by)
    reactiveTimer(10,{triggerd.by<<-NULL})
  })

}

ui<-fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput("input1", label="Input1", choices=c("a", "b", "c"), selected="a"),
      selectInput("input2", label="Input2", choices=c("1", "2", "3"), selected="1"),
      numericInput("input3", label="Input3", value=3)
    ),

    mainPanel(

    )
  )
)

shinyApp(ui=ui, server=server)

希望对你有帮助

【讨论】:

  • 感谢您的回复。同意这个例子非常笨拙,但你们都看到了这个问题。您的建议可能有效,但在现实世界的环境中,您会有很多观察者,并且拥有许多“run.once”等价物会很麻烦。这实际上正是我在完整应用程序中的情况,我有一个全局初始设置列表。也许没有办法解决这个问题?
  • 我看到了您的问题,我认为除了可能阻止事件从 Javascript 传播(将函数绑定到闪亮并在更改后阻止事件 n 毫秒)之外,没有什么好的方法。我更新了我在 R 中完成的答案,解决方案更通用,但它仍然非常难看。
猜你喜欢
  • 1970-01-01
  • 2021-03-22
  • 2020-03-15
  • 1970-01-01
  • 2021-10-26
  • 2011-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多