Skip to content

Instantly share code, notes, and snippets.

@trestletech
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trestletech/1482334aa00a192c73ac to your computer and use it in GitHub Desktop.
Save trestletech/1482334aa00a192c73ac to your computer and use it in GitHub Desktop.
Configure Shiny Server (Pro) to use different libraries for different locations/applications.

Below are two approaches to configuring applications to use different sets of libraries in Shiny Server. The envir file only works in Shiny Server Pro, as it relies on the exec_supervisor argument. The other will work in either.

A modification of the default /etc/shiny-server/shiny-server.conf file that ships with Shiny Server.

Uses exec_supervisor to partition the applications up by setting the R_LIBS_USER environment variable. This option is only available in Shiny Server Pro.

In this case, you could set up as many different libraries as you want and specify a different library for each location, or even each application you want to deploy. This would give you fine-grained control over each of your applications.

# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

# Specify the authentication method to be used.
# Initially, a flat-file database stored at the path below.
auth_passwd_file /etc/shiny-server/passwd;

# Define a server that listens on port 3838
server {
  listen 3838;

  # Define a location at the base URL
  location / {
    # Define a default library for applications
    exec_supervisor "R_LIBS_USER=/usr/lib/LibraryA";

    # Only up tp 20 connections per Shiny process and at most 3 Shiny processes
    # per application. Proactively spawn a new process when our processes reach
    # 90% capacity.
    utilization_scheduler 20 .9 3;

    # Host the directory of Shiny Apps stored in this directory
    site_dir /srv/shiny-server;

    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;

    # When a user visits the base URL rather than a particular application,
    # an index of the applications available in this directory will be shown.
    directory_index on;

    # Now define a sub-location at /finance
    location /finance {
      # Define a library that should be used by the finance department
      exec_supervisor "R_LIBS_USER=/usr/lib/FinanceLibrary";

      
      # Further, define another sub-location that happens to correspond to a particular app.
      location /app1 {
        #Define a specific library to be used by this application
        exec_supervisor "R_LIBS_USER=/usr/lib/LibraryC";
      }
    }
  }
}

# Provide the admin interface on port 4151
admin 4151 {
  
  # Restrict the admin interface to the usernames listed here. Currently
  # just one user named "admin"
  required_user admin;
}

A modification of the default /etc/shiny-server/shiny-server.conf file that ships with Shiny Server. Uses run_as to partition the applications up by user -- each of which presumably has different libraries set in e.g. a ~/.Rprofile file.

In this use-case, you would probably have only a handful of users that you create that each maintain their own separate libraries. In this case, we have one user for the Finance department, but likely wouldn't create distinct users for each individual application.

# Specify the authentication method to be used.
# Initially, a flat-file database stored at the path below.
auth_passwd_file /etc/shiny-server/passwd;

# Define a server that listens on port 3838
server {
  listen 3838;

  # Define a location at the base URL
  location / {
    # Instruct Shiny Server to run applications as the user "shiny" by default
    run_as shiny;

    # Only up tp 20 connections per Shiny process and at most 3 Shiny processes
    # per application. Proactively spawn a new process when our processes reach
    # 90% capacity.
    utilization_scheduler 20 .9 3;

    # Host the directory of Shiny Apps stored in this directory
    site_dir /srv/shiny-server;

    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;

    # When a user visits the base URL rather than a particular application,
    # an index of the applications available in this directory will be shown.
    directory_index on;
    
    location /finance {
      # Run as a different user for this location
      run_as shinyFinance;
    }
  }
}

# Provide the admin interface on port 4151
admin 4151 {
  
  # Restrict the admin interface to the usernames listed here. Currently
  # just one user named "admin"
  required_user admin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment