从 .globals 获取 R Shiny Server

Get an R Shiny Server from .globals

如果我们查看 shinyServer 函数的实现,不难看出它只是将传递的函数插入到我假设的全局环境中。但是,我以前没有看到全局环境被称为“.globals”,只有“.GlobalEnv”或"globalenv()"。

library(shiny)
shinyServer
#> function (func) 
#> {
#>     .globals$server <- list(func)
#>     invisible(func)
#> }
#> <environment: namespace:shiny>

我希望能够从分配给它的任何地方检索隐式传递给 shinyServer 函数的函数。我一直在查看全局环境,但在使用 shinyServer 函数后我没有看到 server 对象。 .globals 在哪里?如何访问它及其内容,包括 .globals$server

.globals 是一个单独的环境。您可以在 github here.

上查看它的代码

如果您想知道其中的内容,请尝试: ls(shiny:::.globals, all.names=T)

你得到:

ls(shiny:::.globals)
[1] "clients" "domain" "IncludeWWW" "lastPort" "options" "ownSeed" "resources"
[8] "reterror" "retval" "running" "serverInfo" "showcaseDefault" "showcaseOverride" "stopped"
[15] "testMode"

实际值是动态的。这是一个小应用程序,可以显示 .globals 中当前的值。

runApp(list(
  ui = bootstrapPage(
    h3("What's in globals?"),
    selectInput(inputId="globin",label="Parts of .globals", choices=ls(shiny:::.globals)),
    textOutput('glob')
  ),
  server = function(input, output) {
    x<-sys.frame(1)
    output$glob <- renderPrint(mget(input$globin, env=x$.globals))
  }
))

我使用 sys.frame(1) 将所有环境放入 x,然后只是从那里子集 .globals