vevek seetharaman
vevek seetharaman

Reputation: 75

Need a way to reload the session immediately without finishing the code sequence in Shiny

Am using shiny package to build a web version of a tool. It is mandatory to perform a validation on user inputs before furnishing the output. One of the validations requires the tool to be refreshed.
To get this done, am trying to use "session$reload" inside a action button called "Display results".
While session$reload works perfectly well in reactive buttons, i need a way to reset the session without completing the code sequence.

Working session reload

server <- function(input, output, session) {

        { #Test Door Identification Server

          #Refresh button
          observeEvent(input$refresh, {
            session$reload()
          })

piece of code where session$code needs to reset immediately

    library(shiny)

ui <- fluidPage(

  actionButton('switchtab',"Click this"),
  textOutput('code_ran')

)

server <- function(input, output, session){

  observeEvent(input$switchtab,{
    aggg_result = -1
    if(aggg_result == -1)
    {
      session$reload()
      print("session reload not working")
    }

    print("Code running this line")

    output$code_ran <- renderText("code Ran this line without refreshing")

  })

}

  shinyApp(ui = ui, server = server)

output of this function: Prints "reload doesnt work" and proceeds with the rest of the statements.

I understand that is how session$reload works, but is there a way to reset without printing "Code running this line"?

Any directions/suggestions will be most welcome.

Regards, Vevek S

Upvotes: 4

Views: 7804

Answers (1)

Mike Wise
Mike Wise

Reputation: 22847

Okay, how about doing simply a return() after the session$reload() this?

library(shiny)

ui <- fluidPage(

  actionButton('switchtab',"Click this"),
  textOutput('code_ran')

)

server <- function(input, output, session){

  print("Initializing")

  observeEvent(input$switchtab,{
    aggg_result = -1
    if(aggg_result == -1)
    {
      session$reload()
      return()
      print("session reload not working")
    }

    print("Code running this line")

    output$code_ran <- renderText("code Ran this line without refreshing")

  })

}
shinyApp(ui = ui, server = server)

It accomplishes what you want, right?

Upvotes: 5

Related Questions