can environment be passed to apps running via shiny-server?

3,248 views
Skip to first unread message

Steven Pav

unread,
Jul 21, 2015, 8:00:42 PM7/21/15
to Shiny - Web Framework for R

hi;

I am running some applications via shiny-server within a docker container. It appears that the R code does not see environment variables of a shell process in the container, rather a limited set of variables are available. Of these some seem to be set by the shell (PATH, LD_LIBRARY_PATH), and some I believe are set in .Renviron (?) (R_UNZIPCMD, R_LIBS_SITE and so on). Is it possible to force shiny-server to pass on the parent environment? This is important in the context of docker (or docker-compose), where e.g. the location of a database in a linked container is set by environment variables. Is there any way to do this without resorting to something ugly?

thanks,

Joe Cheng

unread,
Jul 22, 2015, 1:42:53 PM7/22/15
to Steven Pav, Shiny - Web Framework for R
Sorry, this isn't possible today. We scrub most environment variables before launching R, in case there is sensitive information in them. I think the easiest thing to do today would be to have the command you run in Docker be a bash script that persists the relevant environment variables to disk inside the container.

--
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/ddc23bfd-74f5-46ab-a130-078502428a5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steven Pav

unread,
Jul 22, 2015, 3:54:16 PM7/22/15
to Shiny - Web Framework for R

I had suspect as much. For those who stumble on to this thread via google, one Docker-friendly way to write such configurations is via Tiller ( https://github.com/markround/tiller ), though this ended up being a bit too complicated for my use case. I ended up ditching shiny-server, and am using R to directly call shiny::runApp within the docker container.

thanks for the definitive reply.

Steven Pav

unread,
Jul 22, 2015, 5:57:46 PM7/22/15
to Shiny - Web Framework for R

I spoke too soon. I had been using nginx to redirect traffic to a site running via shiny-server because there is no way to nginx proxy without shiny-server. (see https://groups.google.com/forum/#!topic/shiny-discuss/3XN-RFH60u8 ). Thus it appears I will have to use something like Tiller to write environment. 

Joe Cheng

unread,
Jul 22, 2015, 6:20:52 PM7/22/15
to Steven Pav, Shiny - Web Framework for R
Do you really need Tiller? I was just thinking you could do something like "env > ~/env.txt", then you could parse that file in R with something like:

env <- readLines("~/env.txt")
env.vars <- do.call(rbind, strsplit(sub("=", "\n", env), "\n"))
env.vars <- as.list(structure(env.vars[,2], names = env.vars[,1]))

Steven Pav

unread,
Jul 22, 2015, 6:23:52 PM7/22/15
to Shiny - Web Framework for R, j...@rstudio.com

yes, I backed away from tiller. Since I am quicker in shell than in R, I wrote the following in a shell script that will call shiny-server:

env | grep -v 'IGNORE' | grep -v -P '^_' | perl -pe 's/^([^=]+)=(.+)$/Sys.setenv($1='"'"'$2'"'"')/' >> ~/env.R

then in R code:
if (file.exists('~/env.R')) { source('~/env.R') }

inelegant but quick, and no tiller required.

Joe Cheng

unread,
Jul 22, 2015, 6:31:03 PM7/22/15
to Steven Pav, Shiny - Web Framework for R
Cool. Just watch out for USER, HOME, etc. that already get set correctly, maybe you don't want to pound over those (or maybe you don't care in your case).

Gerry M

unread,
Jan 26, 2016, 12:12:43 PM1/26/16
to Shiny - Web Framework for R, ste...@cerebellumcapital.com
Is there any way to stop shiny from scrubbing the environment variables? 

We're using shiny server internally to try to host some applications. Unfortunately, the R process really needs the environment variables from the parent to work correctly (USER/LD_LIBRARY_PATH/etc). Just setting the environment variables in app.R doesn't seem to do the trick. 

We can pass the correct variables to main.js, but when an R process is launched, it won't grab any of the required variables. 

Fereshteh Karimeddini

unread,
Jan 28, 2016, 10:06:28 AM1/28/16
to Shiny - Web Framework for R, ste...@cerebellumcapital.com
Gerry,

Please take a look at this section of Shiny Server Admin Guide:


which describes what files are loaded when the R process starts up. Hope this helps, but let us know otherwise.

Fereshteh

Gerry M

unread,
Jan 28, 2016, 12:58:32 PM1/28/16
to Shiny - Web Framework for R, ste...@cerebellumcapital.com
Unfortunately not. 

I've setup a shiny application that verbatimText displays:

- Sys.getenv("LD_LIBRARY_PATH")
- Sys.getenv("TEST_VARIABLE")
- Sys.getenv("USER")

And setup shiny server to run as a specific user through the configuration. Under that user's home directory, .bash_profile (which should be hit first) has an export statement that sets LD_LIBRARY_PATH and TEST_VARIABLE. Running ./shiny-server and checking the application, it seems that TEST_VARIABLE and USER are blank, while LD_LIBRARY_PATH seems to be getting set in /R-3.2.2/lib64/R/etc/ldpaths. 

Running the same application using runApp in a separate R Session has all these environment sessions set correctly. 

Running R 3.2.2, Shiny Server 1.4, shiny 0.13.0. 

Joe Cheng

unread,
Jan 28, 2016, 1:11:43 PM1/28/16
to Gerry M, Shiny - Web Framework for R, ste...@cerebellumcapital.com
If you're running Shiny Server Pro then exec_supervisor could help: http://shiny.rstudio.com/articles/libraries.html

It's surprising that the bash_profile trick didn't work though. Can you try upgrading to the latest version of Shiny Server?

Gerry M

unread,
Jan 28, 2016, 1:33:37 PM1/28/16
to Shiny - Web Framework for R, gerry...@gmail.com, ste...@cerebellumcapital.com
Apologies - I am on the latest version and am not currently running shiny server. 

If it matters at all I'm on RHEL 6.4.

As an extremely hackish solution I attempted just exporting variables in  /R-3.2.2/lib64/R/etc/ldpaths which actually worked, but caused me to run into as.environment(NULL) errors. 

Is there something fundamentally different between launching an R process through shiny server and opening an R session and using runApp?

Gerry M

unread,
Jan 28, 2016, 1:52:54 PM1/28/16
to Shiny - Web Framework for R, gerry...@gmail.com, ste...@cerebellumcapital.com
Not currently running shiny server pro*
Reply all
Reply to author
Forward
0 new messages