R Shiny execute order

偶尔善良 提交于 2020-01-03 09:20:22

问题


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?


回答1:


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 


来源:https://stackoverflow.com/questions/35039736/r-shiny-execute-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!