4

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

  • I don't see any inconsistent behavior. For example I never see the `output$code_ran` text sticking around (I do see it flash briefly though). And if I add a `print("initiailizing")` statement after the server declaration (before the `observeEvent`), I always get that at the end after clicking that button, indicating that the session reinitialized after the `observeEvent` ran its course. – Mike Wise Mar 28 '17 at 09:00
  • Maybe adding the output of a `sessionInfo()` would help us here too. – Mike Wise Mar 28 '17 at 09:00
  • My intention in using "session$reload" is to reset the app and to restart it again. Moreover, if the condition "aggg_result" is satisfied, i dont want the code to proceed any further. In my original code, the next line is throwing an error and the app is closing without refreshing. – vevek seetharaman Mar 28 '17 at 09:20
  • Well it is definitely not "inconsistent" in anyway that I see. What do you mean by that? I think it is working as designed. – Mike Wise Mar 28 '17 at 09:24
  • Maybe you need to to edit the question to "Need way to immediately reload session" and get rid of the inconsistency statement. – Mike Wise Mar 28 '17 at 09:25

1 Answers1

5

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?

Mike Wise
  • 22,131
  • 8
  • 81
  • 104