Understanding environments in shiny

Hi!

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:

Gadget<-function(.df){
  
  ui<-miniPage(
      gadgetTitleBar(title = 'Gadget', 
                   right = miniTitleBarButton("done", "Accept", primary = TRUE),
                   left = miniTitleBarButton("cancel", 'Cancel', primary = TRUE)),
      
      miniTabstripPanel(
        
        miniTabPanel('Example', icon = icon('table'),
                     
                     miniContentPanel(
                       
                           switchInput(inputId = 'run',
                                       label = tags$b('example'),
                                       offStatus = 'danger',
                                       value = FALSE)
                     ))))
  
  
  server<-function(input, output, session, .x = .df){
    
    assign('df', value = .x, envir = environment())
    
    observeEvent(input$run,{
      if(isTRUE(input$run)){
        rstudioapi::jobRunScript('script.R',
                                  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.

jobRunScript(
  path,
  name = NULL,
  encoding = "unknown",
  workingDir = NULL,
  importEnv = FALSE,
  exportEnv = ""
)

importEnv
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...

library(shiny)

ui <- fluidPage(
  
)

server <- function(input, output, session) {
  print(environmentName(environment()))
  print(environmentName(globalenv()))
}

shinyApp(ui, server)

result

Listening on http://127.0.0.1:5231
[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.