为什么Shiny reactiveValues()没有像预期的那样改变?

时间:2015-11-11 02:11:30

标签: r logout shiny

我在:

中声明了一个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分配。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我无法重现您的错误,此代码适用于我,因此错误可能出现在您的其他一些服务器逻辑中。

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)