Modify and Read Global variable after session in R Shiny

I am trying to read a global variable which is declared before UI. In a scenario I am modifying the global variable during the session and in observer function. But when I want to print that modified variable after session kill, it prints old values itself. I want to keep this modified variable(in my requirement every time it changes on each session) for each session.

library(shiny)    

timeoutSeconds <- 5
varA <- 3 #Declaring Global variable    

#Trigger the function after the 5 seconds 
inactivity <- sprintf("
function idleTimer() {
var t = setTimeout(logout, %s);
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer;     // catches mouse clicks
window.onscroll = resetTimer;    // catches scrolling
window.onkeypress = resetTimer;  //catches keyboard actions

function logout() {
   Shiny.setInputValue('timeOut', '%ss')
  }

function resetTimer() {
   clearTimeout(t);
   t = setTimeout(logout, %s);  // time is in milliseconds (1000 is 1 second)
   }
}
idleTimer();", 
timeoutSeconds*1000, 
timeoutSeconds, 
timeoutSeconds*1000)

#UI 
ui <- fluidPage(
  tags$script(inactivity)  

)

#Server
server <- shinyServer(function(input,output,session){


  observeEvent(input$timeOut, {
    varA <-varA + 1 #Modifing this global variable for each session

    print(paste0("Session (", session$token, ") timed out at: ", Sys.time()))
    showModal(modalDialog(
      title = "Timeout",
      paste("Session timeout due to", input$timeOut, "inactivity -", Sys.time()),
      footer = NULL
    ))
    session$close()
  })

  session$onSessionEnded(function() {

    #Should print 4
    print(varA) #Printing the modified  variable  during sessions

  })

})

runApp(ui,server)

I want to print 4 in the above code. I have tried multiple times, but may be it doesn't work like that. can you please help me anyone on this.

Hi,

You have to make the variable a reactive Shiny variable:
Within your server function first declare it, then modify.

Run this dummy app to see in action which variables update

library(shiny)

ui <- fluidPage(
  actionButton("myButton", "Click me"),
  uiOutput("myText")
)

server <- function(input, output, session) {
  
  var1 = 0
  var2 = reactiveVal(0)
  
  observeEvent(input$myButton, {
    
    #Try and update values
    var1 = var1 + 1
    var2(var2() + 1) #reactive variables are called/updated like a function
    
    #Print values on UI 
    output$myText = renderUI({
      HTML(paste("<h2>Static R variable var1 =", var1, 
            "<br>and Reactive Shiny Variable var2 =", var2(), "</h2>"))
    })
    
    
  })
  
}

shinyApp(ui, server)

is it no possible to update within observeEvent function?

Hi,

I updated my previous post, as it was a bit confusing.

It shows you how static R variables can only be declared once in Shiny and will always reset to the original value when called again in a function. Shiny (reactive) variables on the other hand can get updated and will retain their value. It becomes important to learn when to use which but it can cause a lot of confusion in the beginning, but there are some good reasons (long story) why this is the case.

Hope this helps,
PJ

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.