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

Time:06-24

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?

CodePudding user response:

There are multiple options:

Put .Renviron file to the expected location inside the container

You can add a COPY command to the Dockefile 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:

CodePudding user response:

Edit: Have a look at @Max's solution. We posted at nearly the same time but his instructions are clearer.


After lots of trial and error I finally got a solution.


First, starting the container outside shinyproxy to check if the shiny app runs normally? Use docker's --env-file flag to specify the .Renviron filepath. In my case since both the Dockerfile and .Renviron are in the same folder so I'd do:

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

The app will now recognize env vars defined in the .Renviron file and no errors!


I then changed into the directory where I had shinyproxy-2.6.1.jar file and ran it again using java -jar shinyproxy-2.6.1.jar. There was an error when I tried to start my shinyapp. It couldn't find the env vars.

So I resorted to adding them directly in the application.yml which is in the same location as shinyproxy-2.6.1.jar:

  - id: app-folder
    display-name: My App
    description: My App's title
    container-cmd: ["R", "-e", "shiny::runApp('/root/app-folder')"]
    container-image: openanalytics/shinyproxy-template
    container-env:
        ENV1: ENV1-VALUE
        ENV2: ENV2-VALUE
        ENV3: ENV3-VALUE
    access-groups: scientists

Replace the necessary parts of the yml section with the corresponding one's on your side depending on your case. The same applies to the env vars.


In fact let me just provide a prototype of my whole application.yml file, have a look at the last app I added "wca":

proxy:
  title: Open Analytics Shiny Proxy
  logo-url: https://www.openanalytics.eu/shinyproxy/logo.png
  landing-page: /
  # hide nav bar:
  hide-navbar: true
  heartbeat-rate: 10000
  heartbeat-timeout: 60000
  port: 8080
  authentication: ldap
  admin-groups: scientists
  # Example: 'simple' authentication configuration
  users:
  - name: jack
    password: password
    groups: scientists
  - name: jeff
    password: password
    groups: mathematicians
  # Example: 'ldap' authentication configuration
  ldap:
    url: ldap://ldap.forumsys.com:389/dc=example,dc=com
    user-dn-pattern: uid={0}
    group-search-base:
    group-search-filter: (uniqueMember={0})
    manager-dn: cn=read-only-admin,dc=example,dc=com
    manager-password: password
    # Docker configuration
  docker:
    url: http://localhost:2375
    port-range-start: 20000
  specs:
  - id: 01_hello
    display-name: Hello Application
    description: Application which demonstrates the basics of a Shiny app
    container-cmd: ["R", "-e", "shinyproxy::run_01_hello()"]
    container-image: openanalytics/shinyproxy-demo
    access-groups: [scientists, mathematicians]
  - id: 06_tabsets
    container-cmd: ["R", "-e", "shinyproxy::run_06_tabsets()"]
    container-image: openanalytics/shinyproxy-demo
    access-groups: scientists
  - id: euler
    display-name: Euler's number
    description: Adding another app to shinyproxy
    container-cmd: ["R", "-e", "shiny::runApp('/root/euler')"]
    container-image: openanalytics/shinyproxy-template
    access-groups: scientists
  - id: wca
    display-name: Wasanii
    description: WhatsApp Chat Analysis
    container-cmd: ["R", "-e", "shiny::runApp('/root/wca')"]
    container-image: wca
    container-env:
        FIREBASE_API_KEY: myfirebaseapikey
        FIREBASE_PROJECT_ID: myfirebaseprojectid
        FIREBASE_AUTH_DOMAIN: myfirebaseauthdomain
        FIREBASE_STORAGE_BUCKET: myfirebasestoragebucket
        FIREBASE_APP_ID: myfirebaseappid
        FIREBASE_DATABASE_URL: myfirebasedatabaseurl
    access-groups: scientists

logging:
  file:
    shinyproxy.log


There's obviously a better solution to refer to the .Renviron file directly but since I can't figure it out this will do.

  • Related