More detailed documentation is available at https://pub.current.posit.team/public/shiny-remote-hpc/
This repository demonstrates how to offload compute-intensive calculations from Shiny applications to remote High Performance Computing (HPC) clusters. By leveraging HPC resources, you can maintain app responsiveness while handling demanding computational workloads.
Modern Shiny applications often need to perform compute-intensive calculations on demand. However, the hosting environment (local laptop, Posit Connect, Shiny Server) typically has limited resources. This project shows how to:
- Offload heavy computations to dedicated HPC clusters
- Maintain Shiny app interactivity and stability
- Manage package dependencies across different environments
- Implement both tight and loose integration patterns
.
├── app-0/ # Basic local parallel example using clustermq
├── app-1/ # Loose integration with SSH (clustermq)
├── app-2/ # Tight integration example (clustermq)
├── app-3/ # SSH integration using mirai
├── app-4/ # Additional mirai examples
├── Offload.qmd # Detailed documentation (Quarto)
└── README.md # This file
The Shiny app runs on an environment with direct access to the HPC scheduler (e.g., on a login or service node). This eliminates the need for SSH but requires more infrastructure setup.
The Shiny app interacts with the HPC cluster via SSH connections to login nodes and submits jobs remotely. More flexible but requires SSH configuration.
- R (version 4.5+ recommended)
- SSH client (for loose integration)
- Access to an HPC cluster with SLURM scheduler
shinyclustermqormirai(for parallel execution)renv(for package management)pak(installed automatically on HPC)
git clone https://github.com/sol-eng/shiny-hpc-offload.git
cd shiny-hpc-offload# Navigate to app-0 directory
setwd("app-0")
# Initialize renv
renv::init()
# Run the app
shiny::runApp()This example uses the Palmer Penguins dataset and runs GLM bootstrap calculations locally.
Generate SSH key:
ssh-keygen -N '' -t rsa -f ~/.ssh/my_hpcCopy public key to HPC:
ssh-copy-id -i ~/.ssh/my_hpc -p 443 username@your-hpc-hostnameTest connection:
ssh -i ~/.ssh/my_hpc -p 443 username@your-hpc-hostnameEdit the SSH template (app-1/ssh.tmpl) and app configuration to match your HPC environment:
options(
clustermq.scheduler = "ssh",
clustermq.ssh.host = "username@your-hpc-hostname",
clustermq.ssh.log = "~/cmq_ssh.log",
clustermq.template = "path/to/ssh.tmpl"
)On the remote HPC cluster:
install.packages("clustermq")The hpc_setup() and init() functions automatically:
- Detect locally installed packages and versions
- Create isolated package directories on HPC (
~/.shiny-<uuid>) - Install matching package versions using
pak - Prevent interference with other HPC workflows
Each app deployment gets a unique identifier, ensuring:
- Package version consistency
- No conflicts between different apps
- Clean separation of environments
library(clustermq)
# Configure SSH backend
options(
clustermq.scheduler = "ssh",
clustermq.ssh.host = "user@hpc-host"
)
# Run parallel computation
results <- Q(
fun = my_function,
x = 1:100,
n_jobs = 10
)library(mirai)
# Create SSH configuration
ssh_cfg <- ssh_config(
remotes = "ssh://hpc-host",
tunnel = TRUE,
rscript = "/path/to/Rscript"
)
# Launch daemons
daemons(n = 4, url = "tcp://127.0.0.1:0", remote = ssh_cfg)
# Execute remote computation
m <- mirai({ mean(rnorm(1e7)) })
m[]Check the cmd_ssh.log file in your home directory on the HPC cluster. Common issues:
- Missing
clustermqpackage on HPC - Incorrect SSH configuration
- Firewall blocking connections
Ensure R versions are similar on both Shiny server and HPC. The automatic package management handles version matching.
Check SLURM logs and ensure:
- SLURM binaries are in
$PATH - Resource requests are appropriate
- Queue/partition names are correct
For detailed explanations, architecture diagrams, and step-by-step tutorials, see the Quarto documentation.
To render the documentation:
quarto::quarto_render("Offload.qmd")