为什么 Shiny reactiveValues() 没有按预期更改?

Why does Shiny reactiveValues() is not change as expected?

我已经在以下位置声明了一个 R Shiny 代码:

################# Security Login, part of server.R in R Shiny App
USER <- reactiveValues()
USER$Logged <- FALSE

## Some logic for login which changes USER$Logged to TRUE, eg. USER$Logged <- TRUE, this works well in changing to TRUE

## Logout handler fails because it can change to FALSE, but then resets back to TRUE
logoutHandler <- observeEvent(input$Logout,
  USER$Logged <- FALSE
}

#This is the trace result.  The number 1 or 0 is the value of input$Logout
#"USER$Logged:  FALSE 1"
#"USER$Logged outside Handler:  FALSE 1"
#"USER$Logged outside Handler:  TRUE 1" #Proof of reset
#"USER$Logged outside Handler:  TRUE 0" #Proof of reset

最后代码将不再有效。连接重置发生或什么都没发生只是重置回 TRUE 并丢弃 FALSE 分配。

如何解决这个问题?

我无法重现你的错误,这段代码对我有用,所以错误可能出在你的其他一些服务器逻辑中。

library(shiny)

ui <- shinyUI(fluidPage(
  actionButton('btn','go')
))

server <- shinyServer(function(input, output){
  a <- reactiveValues(l=FALSE)
  b <- reactiveValues()
  b$l <- FALSE

  observe({
    print(sprintf('Outisde, A: %s, B: %s',a$l,b$l))
  })

  observeEvent(input$btn,{
    print(sprintf('Inside [before], A: %s, B: %s',a$l,b$l))

    a$l <- TRUE
    b$l <- TRUE

    print(sprintf('Inside [after], A: %s, B: %s',a$l,b$l))
  })
})

shinyApp(ui=ui,server=server)