本文介绍了获取在Shiny中触发的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在下面的示例中知道在多个ObserveEvent()
中触发了哪个事件.
I would like to know in the example below which event is fired in the multiple ObserveEvent()
.
ui <- fluidPage(
numericInput("a", "a", 0),
textInput("b", "b")
)
server <- function(input, output, session) {
observeEvent({
input$a
input$b
},{
# If only input$a is fired, I want to know that is input$a
})
}
shinyApp(ui, server)
还是唯一的解决方案是像第二个链接一样具有两个ObserveEvent()
?这样吗?
Or the only solution is to have two ObserveEvent()
like the second link ? like that ?
ui <- fluidPage(
numericInput("a", "a", 0),
textInput("b", "b")
)
server <- function(input, output, session) {
observeEvent({
input$a
},{
my_function_or_reactive_function(input,1)
})
observeEvent({
input$b
},{
my_function_or_reactive_function(input,2)
})
}
shinyApp(ui, server)
链接:
- How to listen for more than one event expression within a Shiny eventReactive handler
- https://groups.google.com/forum/#!topic/shiny-discuss/vd_nB-BH8sw
推荐答案
您可以使用Shiny的 JS事件 shiny:inputchanged
检查哪个输入已更改:
You can use shiny's JS event shiny:inputchanged
to check which input changed:
ui <- fluidPage(
tags$head(
tags$script(
"$(document).on('shiny:inputchanged', function(event) {
if (event.name != 'changed') {
Shiny.setInputValue('changed', event.name);
}
});"
)
),
numericInput("a", "a", 0),
textInput("b", "b"),
textInput("c", "c"),
textOutput("changedInputs"),
textOutput("aFired")
)
server <- function(input, output, session) {
output$changedInputs <- renderText({
paste("Outside observer: Latest input fired:", paste(input$changed, collapse = ", "))
})
observeEvent({
c(input$a,
input$b)
}, {
req(input$changed)
if (input$changed == "a") {
output$aFired <- renderText("Inside observer: input$a was fired")
} else if (input$changed == "b") {
output$aFired <- renderText("Inside observer: input$b was fired")
} else if (input$changed == "c") {
output$aFired <- renderText("Inside observer: input$c was fired")
}
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
:
:
编辑-使用renderUI
来自@TristanTran的请求:
Edit - request from @TristanTran using renderUI
:
library(shiny)
ui <- fluidPage(
tags$head(
tags$script(
"$(document).on('shiny:inputchanged', function(event) {
if (event.name != 'changed') {
Shiny.setInputValue('changed', event.name);
}
});"
)
),
uiOutput("serverside"),
textOutput("changedInputs"),
textOutput("aFired")
)
server <- function(input, output, session) {
output$changedInputs <- renderText({
paste("Outside observer: Latest input fired:", paste(input$changed, collapse = ", "))
})
output$serverside <- renderUI({
tagList(
numericInput("a", "a", 0),
textInput("b", "b"),
textInput("c", "c")
)
})
observeEvent({
c(input$a,
input$b)
}, {
req(input$changed)
if (input$changed == "a") {
output$aFired <- renderText("Inside observer: input$a was fired")
} else if (input$changed == "b") {
output$aFired <- renderText("Inside observer: input$b was fired")
} else if (input$changed == "c") {
output$aFired <- renderText("Inside observer: input$c was fired")
}
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
这篇关于获取在Shiny中触发的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!