如何在 Shiny eventReactive 处理程序中侦听多个事件表达式

我希望两个不同的事件触发更新的数据正在使用的各种图/输出在我的应用程序。一个是被点击的按钮(input$spec_button) ,另一个是被点击的点(mainplot.click$click)。

基本上,我希望同时列出两者,但我不确定如何编写代码。这是我现在拥有的:

在服务器端 R:

data <- eventReactive({mainplot.click$click | input$spec_button}, {
if(input$spec_button){
# get data relevant to the button
} else {
# get data relevant to the point clicked
}
})

但是 if-else 条款不起作用

在 mainplot.click $click | input $spec _ ton 中出现错误: 只有数字、逻辑或复杂类型

可以进行操作

对于 mainplot.click$click | input$spec_button子句,我是否可以使用某种动作组合函数?

42229 次浏览

Here's the solution I came up with: basically, create an empty reactiveValues data holder, and then modify its values based on two separate observeEvent instances.

  data <- reactiveValues()
observeEvent(input$spec_button, {
data$data <- get.focus.spec(input=input, premise=premise,
itemname=input$dropdown.itemname, spec.info=spec.info)
})
observeEvent(mainplot.click$click, {
data$data <- get.focus.spec(input=input, premise=premise, mainplot=mainplot(),
mainplot.click_focus=mainplot.click_focus(),
spec.info=spec.info)
})

I know this is old, but I had the same question. I finally figured it out. You include an expression in braces and simply list the events / reactive objects. My (unsubstantiated) guess is that shiny simply performs the same reactive pointer analysis to this expression block as to a standard reactive block.

observeEvent({
input$spec_button
mainplot.click$click
1
}, { ... } )

EDIT

Updated to handle the case where the last line in the expression returns NULL. Simply return a constant value.

Also:

observeEvent(c(
input$spec_button,
mainplot.click$click
), { ... } )

I've solved this issue with creating a reactive object and use it in event change expression. As below:

xxchange <- reactive({
paste(input$filter , input$term)
})


output$mypotput <- eventReactive( xxchange(), {
...
...
...
} )

This can still be done with eventReactive by putting your actions in a vector.

eventReactive(
c(input$spec_button, mainplot.click$click),
{ ... } )

The idea here is to create a reactive function which will execute the condition you want to pass in observeEvent and then you can pass this reactive function to check the validity of the statement. For instance:

validate_event <- reactive({
# I have used OR condition here, you can use AND also
req(input$spec_button) || req(mainplot.click$click)
})


observeEvent(validate_event(),
{ ... }
)

Keep Coding!