I am fairly new to Shiny (and R for that matter) but I have managed to get an app up and running.
I am however quite confused regarding the "execution order" that takes place when RStudio actually runs the two scripts server.R and ui.R
To my mind there are 4 sections of code (2 for server.R script and 2 for ui.R script):
server.R:
###### SECTION 1
shinyServer(function(input, output, session) {
###### SECTION 2
})
ui.R:
###### SECTION 1
shinyUI(fluidPage(
###### SECTION 2
)
)
My question is, assuming I have the above correct, which sections are run first, second, third, etc?
Add print
statement in each section and run from RStudio. The message is displayed in your console. I got
[1] "section 1 of UI"
[1] "section 2 of UI"
[1] "section 1 of server"
[1] "section 2 of server"
As to the object access, I tried the following and see the variables in each environment.
VarDefinedInSec1UI <- 1
print("* section 1 of UI")
cat(ls(), "\n\n")
shinyUI(fluidPage(
VarDefinedInSec2UI <- 2,
print("* section 2 of UI"),
cat(ls(), "\n\n")
))
VarDefinedInSec1Server <- 3
print("* section 1 of server")
cat(ls(), "\n\n")
shinyServer(function(input, output, session) {
VarDefinedInSec2Server <- 4
print("* section 2 of server")
cat(ls(), "\n\n")
})
I got:
[1] "* section 1 of UI"
VarDefinedInSec1UI
[1] "* section 2 of UI"
VarDefinedInSec1UI VarDefinedInSec2UI
[1] "* section 1 of server"
VarDefinedInSec1Server
[1] "* section 2 of server"
input output session VarDefinedInSec2Server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With