Shiny应用程序中的等效观察者,但行为不同

huangapple go评论71阅读模式
英文:

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):

&gt; 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 &lt;- function(input, output, session) {
flag &lt;- reactiveVal(NULL)
observeEvent(
input$button,
{
confirmSweetAlert(
inputId = &quot;alert&quot;,
title = &quot;ALERT&quot;,
type = &quot;error&quot;
)
}
)
observeEvent(
input$alert,
{
flag(TRUE)
}, ignoreNULL = FALSE
)
observeEvent(
flag(),
{
flag(NULL)
sendSweetAlert(
title = &quot;ALERT 2&quot;,
type = &quot;error&quot;
)
}
)
}

As an alternative use c(input$alert, input$X) instead of list(input$alert, input$X) in server2:

&gt; is.null(c(NULL, NULL))
[1] TRUE

server2 &lt;- function(input, output, session) {
flag &lt;- reactiveVal(NULL)
observeEvent(input$button,
{
confirmSweetAlert(
inputId = &quot;alert&quot;,
title = &quot;ALERT&quot;,
type = &quot;error&quot;
)
}
)
observeEvent({c(input$alert, input$X)},
{
flag(TRUE)
}, ignoreInit = TRUE
)
observeEvent(
flag(),
{
flag(NULL)
sendSweetAlert(
title = &quot;ALERT 2&quot;,
type = &quot;error&quot;
)
}
)
}

huangapple
  • 本文由 发表于 2023年4月11日 15:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定