闪亮:observeEvent 和 eventReactive 有什么区别?
Shiny: what is the difference between observeEvent and eventReactive?
我现在读了几次有关响应式编程的 Shiny 文档,但我无法正确理解 observeEvent
和 eventReactive
之间的区别。
文档说:
Use observeEvent whenever you want to perform an action in response to an event. (Note that "recalculate a value" does not generally count as performing an action–see eventReactive for that.)
....
Use eventReactive to create a calculated value that only updates in response to an event. This is just like a normal reactive expression except it ignores all the usual invalidations that come from its reactive dependencies;
在我尝试过的所有情况下,我发现使用 observeEvent
和 eventReactive
之间没有区别(无论我使用什么函数,代码都可以正常工作,对性能没有明显影响)。
你能帮我弄清楚两者之间的真正区别是什么吗?理想情况下,我想要一些示例来说明它们何时可以互换,其中一个示例 observeEvent
有效但 eventReactive
无效,反之亦然。
就像observe
和reactive
的区别。一个是 运行 当一些反应变量是 "triggered" 并且意味着有副作用(observeEvent
),另一个是 returns 一个反应值并且是为了用作变量 (eventReactive
)。即使在这些函数的文档中,前者也没有被分配给变量(因为它只是为了产生副作用),而后者被分配给变量并在以后使用。
正如@daatali 所说,这两个函数用于不同的目的。
ui <- shinyUI(pageWithSidebar(
headerPanel("eventReactive and observeEvent"),
sidebarPanel(
actionButton("evReactiveButton", "eventReactive"),
br(),
actionButton("obsEventButton", "observeEvent"),
br(),
actionButton("evReactiveButton2", "eventReactive2")
),
mainPanel(
verbatimTextOutput("eText"),
verbatimTextOutput("oText")
)
))
server <- shinyServer(function(input, output) {
etext <- eventReactive(input$evReactiveButton, {
runif(1)
})
observeEvent(input$obsEventButton,{
output$oText <- renderText({ runif(1) })
})
eventReactive(input$evReactiveButton2,{
print("Will not print")
output$oText <- renderText({ runif(1) })
})
output$eText <- renderText({
etext()
})
})
shinyApp(ui=ui,server=server)
eventReactive
创建一个根据 eventExpr
变化的反应值,而 observeEvent
只是根据 eventExpr
触发
我认为这里需要强调顶层实践方面。
eventReactive
创建一个对象,您 定义为
reactive
会,但是如果没有通常的连锁反应行为,您会得到
来自 reactive
。然而,它像
其他 reactives
.
observeEvent
无法创建您定义的对象(它
创造其他东西)。它立即被评估而不被缓存。
这是为了引起副作用。
因此,如果您需要数据框、矢量、绘图或其他东西,但又想与通常的反应性连锁反应分离,请使用 eventReactive
。
如果您只想立即产生副作用,observeEvent
是您的选择。
提供我的理解方式,纠正我并根据需要添加更多信息。大部分信息来自https://shiny.rstudio.com/articles/action-buttons.html
- 也可能很久以前有人问过这个问题,我在经历 eventReactive() 和 observeEvent() 时遇到了同样的问题
- ObeserveEvent 更像是事件的触发器,而 eventReactive 更像是延迟
- 下面我尝试使用相同的代码,同时使用反应函数
To build several action buttons that control the same object, combine observeEvent() calls with reactiveValues() , Here I can use two actionButtons which are working at the same time in the same code.
Code.1 Gives the effect of observeElement()
Code.2 Uses eventReactive() , but if I try to use two different actionButtons, only the latest one works the earlier button is null and did not react
代码 1
library(shiny)
ui<-fluidPage(
actionButton("runif", "uniform"),
actionButton("rnorm", "Normal"),
hr(),
plotOutput("plot")
)
server<-function(input, output){
v<-reactiveValues(data=NULL)
observeEvent(
input$runif,
{
v$data<-runif(100)
}
)
observeEvent(
input$rnorm,
{
v$data<-rnorm(100)
}
)
output$plot <- renderPlot(
{
if (is.null(v$data)) return()
hist(v$data)
}
)
}
shinyApp(ui, server)
code2
library(shiny)
ui<-fluidPage(
actionButton(inputId = "norm", label = "Normal"),
actionButton(inputId = "unif", label = "Uniform"),
#Normal
plotOutput("hist")
)
server <- function(input, output) {
dnorm <- eventReactive(input$norm, {rnorm(100)})
dunif <- eventReactive(input$unif, {runif(100)})
output$hist <- renderPlot({
hist(dfnorm())
})
output$hist <- renderPlot({
hist(dunif())
})
}
shinyApp(ui, server)
我发现 this 有助于理解 eventReactive
:
eventReactives are similar to reactives, they are constructed as
follows:
eventReactive( event {
code to run
})
eventReactives are not dependent on all reactive expressions in their body ('code to
run' in the snippet above). Instead, they are only dependent on the
expressions specified in the event section.
我现在读了几次有关响应式编程的 Shiny 文档,但我无法正确理解 observeEvent
和 eventReactive
之间的区别。
文档说:
Use observeEvent whenever you want to perform an action in response to an event. (Note that "recalculate a value" does not generally count as performing an action–see eventReactive for that.)
....
Use eventReactive to create a calculated value that only updates in response to an event. This is just like a normal reactive expression except it ignores all the usual invalidations that come from its reactive dependencies;
在我尝试过的所有情况下,我发现使用 observeEvent
和 eventReactive
之间没有区别(无论我使用什么函数,代码都可以正常工作,对性能没有明显影响)。
你能帮我弄清楚两者之间的真正区别是什么吗?理想情况下,我想要一些示例来说明它们何时可以互换,其中一个示例 observeEvent
有效但 eventReactive
无效,反之亦然。
就像observe
和reactive
的区别。一个是 运行 当一些反应变量是 "triggered" 并且意味着有副作用(observeEvent
),另一个是 returns 一个反应值并且是为了用作变量 (eventReactive
)。即使在这些函数的文档中,前者也没有被分配给变量(因为它只是为了产生副作用),而后者被分配给变量并在以后使用。
正如@daatali 所说,这两个函数用于不同的目的。
ui <- shinyUI(pageWithSidebar(
headerPanel("eventReactive and observeEvent"),
sidebarPanel(
actionButton("evReactiveButton", "eventReactive"),
br(),
actionButton("obsEventButton", "observeEvent"),
br(),
actionButton("evReactiveButton2", "eventReactive2")
),
mainPanel(
verbatimTextOutput("eText"),
verbatimTextOutput("oText")
)
))
server <- shinyServer(function(input, output) {
etext <- eventReactive(input$evReactiveButton, {
runif(1)
})
observeEvent(input$obsEventButton,{
output$oText <- renderText({ runif(1) })
})
eventReactive(input$evReactiveButton2,{
print("Will not print")
output$oText <- renderText({ runif(1) })
})
output$eText <- renderText({
etext()
})
})
shinyApp(ui=ui,server=server)
eventReactive
创建一个根据 eventExpr
变化的反应值,而 observeEvent
只是根据 eventExpr
我认为这里需要强调顶层实践方面。
eventReactive
创建一个对象,您 定义为reactive
会,但是如果没有通常的连锁反应行为,您会得到 来自reactive
。然而,它像 其他reactives
.observeEvent
无法创建您定义的对象(它 创造其他东西)。它立即被评估而不被缓存。 这是为了引起副作用。
因此,如果您需要数据框、矢量、绘图或其他东西,但又想与通常的反应性连锁反应分离,请使用 eventReactive
。
如果您只想立即产生副作用,observeEvent
是您的选择。
提供我的理解方式,纠正我并根据需要添加更多信息。大部分信息来自https://shiny.rstudio.com/articles/action-buttons.html
- 也可能很久以前有人问过这个问题,我在经历 eventReactive() 和 observeEvent() 时遇到了同样的问题
- ObeserveEvent 更像是事件的触发器,而 eventReactive 更像是延迟
- 下面我尝试使用相同的代码,同时使用反应函数
To build several action buttons that control the same object, combine observeEvent() calls with reactiveValues() , Here I can use two actionButtons which are working at the same time in the same code.
Code.1 Gives the effect of observeElement()
Code.2 Uses eventReactive() , but if I try to use two different actionButtons, only the latest one works the earlier button is null and did not react
代码 1
library(shiny) ui<-fluidPage( actionButton("runif", "uniform"), actionButton("rnorm", "Normal"), hr(), plotOutput("plot") ) server<-function(input, output){ v<-reactiveValues(data=NULL) observeEvent( input$runif, { v$data<-runif(100) } ) observeEvent( input$rnorm, { v$data<-rnorm(100) } ) output$plot <- renderPlot( { if (is.null(v$data)) return() hist(v$data) } ) } shinyApp(ui, server)
code2
library(shiny) ui<-fluidPage( actionButton(inputId = "norm", label = "Normal"), actionButton(inputId = "unif", label = "Uniform"), #Normal plotOutput("hist") ) server <- function(input, output) { dnorm <- eventReactive(input$norm, {rnorm(100)}) dunif <- eventReactive(input$unif, {runif(100)}) output$hist <- renderPlot({ hist(dfnorm()) }) output$hist <- renderPlot({ hist(dunif()) }) } shinyApp(ui, server)
我发现 this 有助于理解 eventReactive
:
eventReactives are similar to reactives, they are constructed as follows:
eventReactive( event { code to run })
eventReactives are not dependent on all reactive expressions in their body ('code to run' in the snippet above). Instead, they are only dependent on the expressions specified in the event section.