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.