OnStop function in shiny via shiny server pro

Hi all,

The article Shiny - onStop tells that , A function that will be called after the app has finished running. But this works only locally i guess. Because when I deploy the application on shiny server and then open the application and close it, this function (onstop) does not trigger . Is my understanding correct?

Interesting, how are you determining whether it triggers or not?

I am writing a csv file inside onstop function. So since it is not triggering , there is no csv file. But when opened internally there is a csv file written

On shinyapps.io for example. Even if the on stop function ran and a csv file was written , it would not show in next session because the per session storage is not permanent. Could it be that?

I am deploying on shiny server pro. Below is the sample. I am trying to append the csv file if it exists.

library(shiny)
if (interactive()) {
  # Open this application in multiple browsers, then close the browsers.
  shinyApp(
    ui = basicPage("onStop demo"),
    
    server = function(input, output, session) {
      # Sys.sleep(10)
      # print(Sys.time())
      # onStop(function() print(Sys.time()))
    },
    
    onStart = function() {
      user_name <- Sys.getenv("RSTUDIO_USER_IDENTITY")
      # Start_date <- format(Sys.time(), "%d-%m-%Y")
      Start_time <- format(Sys.time(), "%d-%m-%Y %H:%M:%S")
      
      onStop(function() {
        # End_date <- format(Sys.time(), "%d-%m-%Y")
        End_time <- format(Sys.time(), "%d-%m-%Y %H:%M:%S")
        log_data <- cbind(user_name,Start_time,End_time,
                          difftime(Start_time,End_time,units = "secs"))
        # write.csv(log_data,"log_data.csv")
        if (file.exists("log_data.csv")) {
          write.table(log_data,  
                      file="log_data.csv",
                      append = T,
                      sep=',',
                      row.names=T,
                      col.names=F )
        } else {
          write.csv(log_data,"log_data.csv")
        }
        
      })
    }
  )
}

Do you have evidence that the code runs at all? I would have thought it would fail the if interactive() check, if you arent running from console interactively.

yes i just tried again. It runs locally. Is it not working for you?

I'm not doubting that it runs locally, I'm doubting that it runs remotely. If you are running on server you.wouldnt have interactive access on server console,...
Why not remove

Oh Yes. Sorry my bad. I remove interactive() when running remotely (shiny server pro).

I dont use shiny server pro so, if that's necessary part of this issue, I can't directly help.
I will edit your title and tags to mention shiny server pro

What exactly do you mean by closing the application? If you are closing the browser-tab, this will only end the session but won't stop the server-function from running.

If you want to run code after the session ends you need place the onStop function call inside the server function or you may register a session$onSessionEnded callback function. Please see the following example, which shows the difference to a global onStop:

library(shiny)

ui <- fluidPage(
  actionButton("stopSession", "Stop session"),
  actionButton("stopApp", "Stop app")
)

server <- function(input, output, session) {
  
  onStop(function() {
    cat(sprintf("Session onStop: Session %s was closed\n", session$token))
  })
  
  session$onSessionEnded(function(){
    cat(sprintf("onSessionEnded callback: Session %s was closed\n", session$token))
  })
  
  observeEvent(input$stopSession, {
    cat(sprintf("Closing session %s\n", session$token))
    session$close()
  })
  
  observeEvent(input$stopApp, {
    cat("Stopping shiny app%s\n")
    stopApp()
  })
}

shinyApp(
  ui,
  server,
  onStart = function() {
    cat("Doing application setup\n")
    onStop(function() {
      cat("Global onStop: Doing application cleanup\n")
    })
  }
)
1 Like