【问题标题】:Shiny observeEvent on different actions不同动作的闪亮观察事件
【发布时间】:2019-10-03 16:09:52
【问题描述】:

我想做一些事情:有actionLink a和actionLink b。他们可以触发 UpdateNumericInput() 分配一些权重。我还可以手动更改 UI 中 numericInput() 的权重。我想实现的是,当单击 actionLink a 时,文本输出将是“单击 a”,而 actionLink b 发生的事情与“单击 b”相同。当我实际在 UI 上编辑权重时,我还希望文本输出显示为“手动更改权重”。有没有办法实现这一点?

#triggered by actionLink a
apply_a_weights = observeEvent(input$a_weight_link, {

v_a = get_a_weights()

v_a = setNames(as.double(unname(v_a)), names(v_a))

for (nm in names(v_a)) {
  updateNumericInput(session, nm, value = v_a[[nm]])
}
output$selected_weight <- renderUI({HTML("You have changed to <B>a weight</B>")}) }, priority = 1)

# triggered by actionLink b
apply_b_weights = observeEvent(input$b_weight_link, {

v_b = df_dmd$b_weights
names(v_b) = df_dmd$input_id

for (nm in names(v_b)) {
  updateNumericInput(session, nm, value = v_b[[nm]])
}
output$selected_weight <- renderUI({HTML("You have changed to <B>b weight</B>")})}, priority = 1)

我也试过把这部分放在server.R中作为默认的文本输出:

output$selected_weight <- renderUI({HTML("<B>manually</B> changed the weights")})

但是这些代码不起作用。当我自己更改权重时,“手动更改权重”不会出现。

非常感谢

【问题讨论】:

    标签: r shiny actionlink


    【解决方案1】:

    您没有提供可重现的代码,因此很难猜测。问题可能是重复的output$selected_weight

    定义一个reactiveVal来存储确实要显示的文字,例如server的开头:

    Text <- reactiveVal()
    

    然后在你的两个observeEvent中,删除output$selected_weight,然后:

    #triggered by actionLink a
    apply_a_weights = observeEvent(input$a_weight_link, {
    
    v_a = get_a_weights()
    
    v_a = setNames(as.double(unname(v_a)), names(v_a))
    
    for (nm in names(v_a)) {
      updateNumericInput(session, nm, value = v_a[[nm]])
    }
    
    Text("You have changed to <B>a weight</B>") 
    
    }, priority = 1)
    
    # triggered by actionLink b
    apply_b_weights = observeEvent(input$b_weight_link, {
    
    v_b = df_dmd$b_weights
    names(v_b) = df_dmd$input_id
    
    for (nm in names(v_b)) {
      updateNumericInput(session, nm, value = v_b[[nm]])
    }
    
    Text("You have changed to <B>b weight</B>") 
    
    }, priority = 1)
    

    最后,在observeEvents 之外,做

    output$selected_weight <- renderUI({HTML(Text()})
    

    同样,您没有提供最小的可重现代码,所以我不确定我的答案是否能满足您的要求。

    【讨论】:

    • 非常感谢您的回复。你给了我很好的建议。主要的是我需要 Shiny 可以区分 numericinput 值是通过调用 updateNumericInput 还是由人在 UI 上键入来更新的。很抱歉我不能分享太多代码。
    猜你喜欢
    • 2016-07-25
    • 2015-11-29
    • 2018-09-25
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2020-05-11
    • 2019-06-11
    • 2018-05-26
    相关资源
    最近更新 更多