How can I access the user's global environment?

I would like to create an RStudio addin that would use the content of the project's global environment (dataframes created by the user for example) but I don't know how to access it through {rstudioapi}.

I have tried to understand how the {esquisse} addin works since it has this functionality but it is my first package with R and it is very difficult for me to analyse how other packages are built. I don't know what are the "base" files, i.e the files that do not rely on other files. There are several modules that use created functions, that use other created functions, etc. and I am quickly lost.

This article says that I can use getActiveDocumentContext() to access the content of the current RStudio document but printing getActiveDocumentContext()$content in the console just gives an empty result. How can I access the user's global environment?

Hi @bretauv,

When the users instantiates the RStudio addin, it is running in their local R session. So this means that the app has access to anything in the R global environment. However, you don't know what they have named their data frames, for example. So you could have some code that scans the users GE to find any data.frames (by detecting classes perhaps).

Check out and run this hacky example I made to quickly demonstrate how this can work. I define two data frames in my GE, then the app finds all the data frames and allows the user to choose which to display. Very simple app, but shows how the gadget can be aware of what is in the GE. Let me know if you have any questions.

library(shiny)
library(miniUI)
library(purrr)

# Define in global environment
x <- iris
y <- mtcars

test_app <- function() {
  # Get contents of GlobalEnv
  get_ge <- ls(envir = globalenv())
  dd <- map(get_ge, get) # get object contents
  names(dd) <- get_ge
  # Keep only data.frames
  dd <- dd[map_lgl(dd, inherits, what = 'data.frame')]
  
  ui <- miniPage(
    gadgetTitleBar("Can you hear me?"),
    miniContentPanel(
      selectInput('choose', 'Choose data to display',
                  choices = names(dd)),
      tableOutput('show_me')
    )
  )
  
  server <- function(input, output, session) {
    output$show_me <- renderTable({
      req(input$choose)
      
      head(dd[[input$choose]])
    })
  }
  
  viewer <- dialogViewer('Test')
  runGadget(ui, server, viewer = viewer)
}

test_app()

getActiveDocumentContext() will return the document ID, path to the document on disk, contents of the document, and any document selections. However, if you just run code in the console, since there is no document context the contents are empty.

If you put the code below inside an R script and ran the code, you will see what happens.

rstudioapi::getActiveDocumentContext()$contents

# Here is some text

Then copy and paste rstudioapi::getActiveDocumentContext()$contents into the console and see what changes.

1 Like

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

@mattwarkentin Great answer, it is very helpful. Thanks a lot!