Understanding environments in shiny


This is my first post here, so sorry if something is wrong.

I am creating a gadget with shiny and miniUI and I have put a button that when pressing it launches another shiny app to the browser. To achieve this, I have used the rstudioapi :: jobRunScript function that launches a script that runs the app.

The problem is that the script depends on a variable called 'df' that is on the server and therefore it needs to be loaded with the importEnv parameter of the jobRunScript function. Here I have 2 cases:

  1. I assign the variable to the globalEnv with: assign('df', value = '.x', envir=globalenv()) and import the environment with: rstudioapi::jobRunScript('script.R', importEnv = environmentName(globalenv())).

In this case, everything runs correctly.

  1. I do exactly the same but with the current environment of the server:

assign('df', value='.x', envir=environment())
rstudioapi::jobRunScript('script.R', importEnv = environmentName(environment()))

This solution fails because can't find the 'df' value.

What is the difference?

Here is the code that doesn't run:

      gadgetTitleBar(title = 'Gadget', 
                   right = miniTitleBarButton("done", "Accept", primary = TRUE),
                   left = miniTitleBarButton("cancel", 'Cancel', primary = TRUE)),
        miniTabPanel('Example', icon = icon('table'),
                           switchInput(inputId = 'run',
                                       label = tags$b('example'),
                                       offStatus = 'danger',
                                       value = FALSE)
  server<-function(input, output, session, .x = .df){
    assign('df', value = .x, envir = environment())
                                  importEnv = environmentName(environment()))
  runGadget(ui, server, viewer = dialogViewer("recipesGadget", width = 750, height = 750))

the documentation says that importENV should be a TRUE or FALSE value, indicating whether the global environment should be imported or not.

  name = NULL,
  encoding = "unknown",
  workingDir = NULL,
  importEnv = FALSE,
  exportEnv = ""

Whether to import the global environment into the job.

Furthermore when I run this app snippet to snoop on the environment names, it is informative...


ui <- fluidPage(

server <- function(input, output, session) {

shinyApp(ui, server)


Listening on
[1] ""
[1] "R_GlobalEnv"

This question isn't really about shiny, but is about how to communicate data to the R process run by rstudioapi::jobRunScript(). Unfortunately there's currently no great way to communicate so you'll need to use some hack, like saving data in an environment variable or writing it to disk in a known location.

Thank you very much for your answers. Indeed, I misunderstood the documentation and thought that any environment could be imported and not only the global one (when I did the test that you show, I got value for the server environment and that confused me). In any case, I have managed to make it work without problem, so fixed.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.