Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny execute order

Tags:

r

shiny

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?

like image 435
gmarais Avatar asked Jan 27 '16 14:01

gmarais


Video Answer


1 Answers

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.

ui.R

VarDefinedInSec1UI <- 1

print("* section 1 of UI")
cat(ls(), "\n\n")

shinyUI(fluidPage(
  VarDefinedInSec2UI <- 2,

  print("* section 2 of UI"),
  cat(ls(), "\n\n")
))

server.R

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 
like image 127
Kota Mori Avatar answered Nov 03 '22 08:11

Kota Mori