Search code examples
rdockershinyshinyproxy

Where to place .Renviron file when deploying shiny app with shinyproxy?


I'm learning how to use shinyproxy to deploy R shiny applications but I can't figure out where to place my .Renviron file which contains global variables used to access a database.

The docker image builds without any errors but when I start the container using:

docker run -it -p 3838:3838 shinyproxy-template .

It doesn't find the env variables in the .Renviron file and I end up getting an error on the part of the R code that requires the global variables.

My current folder structure is as follows:

shinyproxy-template/
                   |- app-folder/
                   |- .gitignore
                   |- Dockerfile
                   |- README.md
                   |- app.Rproj
                   |- Rprofile.site
                   |- .Renviron

I tried placing the .Renviron file inside the app-folder/ then built the docker image again but the global variables were still inaccessible.

Where should I place the .Renviron so that the global variables are accessed by the app?


Solution

  • There are multiple options:

    Put .Renviron file to the expected location inside the container

    You can add a COPY command to the Dockerfile to copy your .Renviron file to the expected location - i.e. either a home directory of the user or the WORKDIR location if defined in the Dockerfile. In case of the root user it would be:

    COPY .Renviron /root/
    

    Add environment variables from .Renviron to the Dockerfile

    Add lines like:

    ENV VAR1="value1"
    ENV VAR2="value2"
    

    to your Dockerfile

    Add environment variables from .Renviron to the shinyproxy configuration

    You can define environment variables in the application.yaml configuration file by either using

    container-env:
      VAR1: VALUE1
      VAR2: VALUE2
    

    or

    container-env-file: /path/to/.Renviron
    

    for your app specification. Note that the path here is on the host and not inside the container.

    For docker run

    When you do a docker run outside of shinyproxy you can use argument --env-file with something like:

    docker run -it -p 3838:3838 shinyproxy-template --env-file /path/to/shinyproxy-template/.Renviron
    

    Releant documentation links: