【问题标题】:R Shiny execute orderR Shiny 执行命令
【发布时间】:2016-01-27 14:14:41
【问题描述】:

我对 Shiny(以及 R 语言)还很陌生,但我已经设法启动并运行了一个应用程序。

然而,当 RStudio 实际运行 server.Rui.R 两个脚本时发生的“执行顺序”,我感到很困惑

在我看来,有 4 段代码(2 段用于 server.R 脚本,2 段用于 ui.R 脚本):

server.R:

###### SECTION 1

shinyServer(function(input, output, session) {


  ###### SECTION 2


})

ui.R:

###### SECTION 1

shinyUI(fluidPage(

  ###### SECTION 2

)
)

我的问题是,假设我以上正确,首先运行哪些部分,第二个,第三个等?

【问题讨论】:

  • 你可以在一个名为global.R的文件中定义你的全局变量。
  • 请注意,与您相关的不是编译顺序(无论如何,它在 R 中定义不明确)而是 执行顺序
  • 只是为了扩展我的评论:server.Rui.R 的内容在不同的环境中被解析和评估,并且它们不相互交谈(不管执行顺序)。您可以通过在其中一个中定义一个对象来证明这一点,看看您是否可以从另一个中获取它(您不能)。公共对象可以在global.R 文件中定义。在那里定义的对象可以在server.Rui.R 中使用。
  • 感谢@KonradRudolph,我将更新问题以使用“执行”而不是“编译”。我还是想知道答案..

标签: r shiny


【解决方案1】:

在每个部分中添加 print 语句并从 RStudio 运行。该消息将显示在您的控制台中。我得到了

[1] "section 1 of UI"
[1] "section 2 of UI"
[1] "section 1 of server"
[1] "section 2 of server"

关于对象访问,我尝试了以下,查看了每个环境中的变量。

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")
))

服务器.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")
})

我明白了:

[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 

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 2019-03-05
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多