Shiny thread / async model / contexts / closures

library(shiny)

ui <- fluidPage(

 wellPanel(sliderInput("number","input number" , value=10,min=1,max=20)),
 wellPanel(textOutput("out1")),
  )
  
server <- function(input, output) {
  
   X <- 42
   Y <- 11123
    print(c("    Hello X = ",X," Y = ",Y))
    
        output$out1 <-  renderText(  {
          
            X <- X+1
            print(c("input = ",input$number))
            print(c(" X = ",X))
            print(c(" Y = ",Y))
        })  
        X <- X+1
        Y <- Y+1
      print(c("Good Bye, X = ",X, " Y = ",Y))
}    

shinyApp(ui = ui, server = server)

The output:

[1] " Hello X = " "42" " Y = " "11123"
[1] "Good Bye, X = " "43" " Y = " "11124"
[1] "input = " "10"
[1] " X = " "44"
[1] " Y = " "11124"

together with the expected slider and a text box enclosing "Y = 11124".

When I slide the slider the output looks like:

"input = " "16"
[1] " X = " "44"
[1] " Y = " "11124"

So the correct input value is read from the slider but X and Y aren't modified.

Questions:

  1. the "Good Bye" output makes sense because the global context can be accessed by the asynchronous code., increasing X by 1. That's why X is 44 and not 43. True?

  2. It looks like the asynchronous renderText is executed after the server has already run from beginning to end. Why?? Is there a way to prevent that? If it is going to be executed, why isn't it run inline, not at the end?
    My mental model must be wrong that when execution finds "renderXYZ" a new thread is started with the XYZ code. But I guess not? What is the right conceptual model??

  3. why does the text box only print "Y = 11124" but not also "X=44"??? (I see similar behavior in my real code and can't explain it there either.

TIA

Carl

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.