ctesta01
ctesta01

Reputation: 959

Get an R Shiny Server from .globals

If we look at the implementation of the shinyServer function, it's not too hard to see that it just inserts the passed function into what I presume is the global environment. However, I haven't seen the global environment referred to as ".globals" before and only as ".GlobalEnv" or "globalenv()".

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

I would like to be able to retrieve the function implicitly passed to the shinyServer function from wherever it is being assigned to. I've been looking in the global environment, but I don't see a server object there after using the shinyServer function. Where is .globals and how do I access it and its contents including .globals$server?

Upvotes: 2

Views: 168

Answers (1)

John Paul
John Paul

Reputation: 12654

.globals is a separate environment. You can see the code for it on github here.

If you want to know what goes in it try: ls(shiny:::.globals, all.names=T)

You get:

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

The actual values are dynamic. Here is a little app which will show you what values are currently in the .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))
  }
))

I got all of the environments into x using sys.frame(1) and then just subset .globals from there.

Upvotes: 2

Related Questions