英文:
Equivalent observers in Shiny apps but different behaviors
问题
以下是翻译好的部分:
第一个Shiny应用与第二个Shiny应用之间的区别在于,第一个应用包括以下代码:
observeEvent(
input$alert,
{
flag(TRUE)
}
)
而在第二个应用中,这段代码被以下代码替代:
observeEvent(
list(input$alert, input$X),
{
flag(TRUE)
}, ignoreInit = TRUE
)
其中 input$X
始终为 NULL
。
然而,这两个应用的行为不同(多次单击按钮会产生不同效果)。为什么?我不理解第二个应用的行为。
英文:
The difference between the two Shiny apps is that the first one includes
observeEvent(
input$alert,
{
flag(TRUE)
}
)
while this code is replaced with the following code in the second one:
observeEvent(
list(input$alert, input$X),
{
flag(TRUE)
}, ignoreInit = TRUE
)
where input$X
is always NULL
.
However the two apps have a different behavior (click several times on the button). Why? I don't understand the behavior of the second one.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
mainPanel(
actionButton(
inputId = "button",
label = "ALERT"
)
)
)
server1 <- function(input, output, session) {
flag <- reactiveVal(NULL)
observeEvent(
input$button,
{
confirmSweetAlert(
inputId = "alert",
title = "ALERT",
type = "error"
)
}
)
observeEvent(
input$alert,
{
flag(TRUE)
}
)
observeEvent(
flag(),
{
flag(NULL)
sendSweetAlert(
title = "ALERT 2",
type = "error"
)
}
)
}
server2 <- function(input, output, session) {
flag <- reactiveVal(NULL)
observeEvent(
input$button,
{
confirmSweetAlert(
inputId = "alert",
title = "ALERT",
type = "error"
)
}
)
observeEvent(
list(input$alert, input$X),
{
flag(TRUE)
}, ignoreInit = TRUE
)
observeEvent(
flag(),
{
flag(NULL)
sendSweetAlert(
title = "ALERT 2",
type = "error"
)
}
)
}
shinyApp(ui = ui, server = server1)
shinyApp(ui = ui, server = server2)
答案1
得分: 1
以下是您要翻译的部分:
"The different behaviour is caused by the fact, that list(NULL, NULL)
is not NULL
(resulting in flag
being set to TRUE
):
> is.null(list(NULL, NULL))
[1] FALSE
and observeEvent
uses ignoreNULL = TRUE
by default.
Setting ignoreNULL = FALSE
in server1
results in the same behaviour as shown for server2
:
server1 <- function(input, output, session) {
flag <- reactiveVal(NULL)
observeEvent(
input$button,
{
confirmSweetAlert(
inputId = "alert",
title = "ALERT",
type = "error"
)
}
)
observeEvent(
input$alert,
{
flag(TRUE)
}, ignoreNULL = FALSE
)
observeEvent(
flag(),
{
flag(NULL)
sendSweetAlert(
title = "ALERT 2",
type = "error"
)
}
)
}
As an alternative use c(input$alert, input$X)
instead of list(input$alert, input$X)
in server2:
> is.null(c(NULL, NULL))
[1] TRUE
server2 <- function(input, output, session) {
flag <- reactiveVal(NULL)
observeEvent(input$button,
{
confirmSweetAlert(
inputId = "alert",
title = "ALERT",
type = "error"
)
}
)
observeEvent({c(input$alert, input$X)},
{
flag(TRUE)
}, ignoreInit = TRUE
)
observeEvent(
flag(),
{
flag(NULL)
sendSweetAlert(
title = "ALERT 2",
type = "error"
)
}
)
}
英文:
The different behaviour is caused by the fact, that list(NULL, NULL)
is not NULL
(resulting in flag
being set to TRUE
):
> is.null(list(NULL, NULL))
[1] FALSE
and observeEvent
uses ignoreNULL = TRUE
by default.
Setting ignoreNULL = FALSE
in server1
results in the same behaviour as shown for server2
:
server1 <- function(input, output, session) {
flag <- reactiveVal(NULL)
observeEvent(
input$button,
{
confirmSweetAlert(
inputId = "alert",
title = "ALERT",
type = "error"
)
}
)
observeEvent(
input$alert,
{
flag(TRUE)
}, ignoreNULL = FALSE
)
observeEvent(
flag(),
{
flag(NULL)
sendSweetAlert(
title = "ALERT 2",
type = "error"
)
}
)
}
As an alternative use c(input$alert, input$X)
instead of list(input$alert, input$X)
in server2:
> is.null(c(NULL, NULL))
[1] TRUE
server2 <- function(input, output, session) {
flag <- reactiveVal(NULL)
observeEvent(input$button,
{
confirmSweetAlert(
inputId = "alert",
title = "ALERT",
type = "error"
)
}
)
observeEvent({c(input$alert, input$X)},
{
flag(TRUE)
}, ignoreInit = TRUE
)
observeEvent(
flag(),
{
flag(NULL)
sendSweetAlert(
title = "ALERT 2",
type = "error"
)
}
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论