access shiny environment from functions in an R-package

748 views
Skip to first unread message

Vincent

unread,
Nov 23, 2015, 6:22:58 PM11/23/15
to Shiny - Web Framework for R
The main functions in my application are in an R package in the R/ directory. These functions are intended to work both inside and outside of a shiny-app. When calling a function a string is passed with the name of the dataset. Inside shiny that string is used to look-up a dataframe in a reactiveValues object (e.g., r_data). 

What is the best way to give functions, that , by default, are in their own package environment, access to reactiveValues etc. when called from a Shiny environment? Is it possible to set the shiny environment as the parent environment for functions in a package somehow?

I discussed this with @smbache a while back (see link below) and got a working solution. Unfortunately, when running the app locally, I have to make the shiny environment accessible in the global environment which is not ideal.

Joe Cheng

unread,
Nov 23, 2015, 6:51:50 PM11/23/15
to Vincent, Shiny - Web Framework for R
What you're asking for is essentially dynamic scoping and if you want that, you may as well just use a global variable. It doesn't necessarily have to be in the global environment, you can create an environment in your package called e.g. "global" and store global-ish values in it. We do this in a number of places in Shiny.

Here's an example of how we make it easy to get at the current "session" (a.k.a. "reactive domain") from anywhere in Shiny without having to pass it around manually:
?domains

In your case though, I can't help but wonder whether you should just pass the data around using normal function arguments. The global stuff is not that easy to get right when you have multiple clients simultaneously connected.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/1a42d35d-e1d4-48b4-a6e7-56a6f8e433f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vincent

unread,
Nov 23, 2015, 7:32:39 PM11/23/15
to Shiny - Web Framework for R, vincen...@gmail.com
Thanks for the suggestions Joe. I don't think that passing the data around would work well the report feature in Radiant (see https://youtu.be/ioHopyfD2f0?t=117 for an illustration). 

I'm not sure how to use your suggestions about 'reactive domain' and where to put them (in the package function code or in server.R?). How this this allow me to connect a users environment to the package environment? As a simple example of what I'm looking for, I'd like `state_init`, which is a function exported by the R-package, to have access to `r_state` which is tied to a specific shiny user. 

state_init <- function(inputvar, init = "") {
  if (is.null(r_state[[inputvar]])) init else r_state[[inputvar]]
}

When the app is run on a server I make this work by sourcing all of the package R-code inside server.R. However when the package is loaded and in the search path this doesn't work well.
Reply all
Reply to author
Forward
0 new messages