Last active
November 15, 2024 14:03
-
-
Save andrewheiss/5117691f5963a07c295b2863b491c34c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(analogsea) | |
library(furrr) | |
# R on local machine → DigitalOcean Linux image → Docker on that → R | |
# Install analogsea, log into DigitalOcean, generate an API key with write | |
# permissions, and add this line to ~/.Rprofile: | |
# | |
# Sys.setenv(DO_PAT = "KEY_GOES_HERE") | |
# Create a droplet with Docker pre-installed | |
d <- docklet_create(region = "sfo2") | |
# Pull the latest tidyverse image onto the droplet | |
droplet(d$id) %>% docklet_pull("rocker/tidyverse") | |
# Get the public IP address for the droplet | |
ip <- d$networks$v4[[1]]$ip_address | |
# Path to your private key (make sure your key is in your DigitalOcean account too) | |
ssh_private_key_file <- "/Users/andrew/.ssh/id_rsa" | |
# R script that needs to run a future command | |
# It will run the tidyverse image; --net=host allows it to communicate back to this computer | |
rscript <- c("sudo", "docker", "run", "--net=host", | |
"rocker/tidyverse", "Rscript") | |
# Connect and create a cluster | |
cl <- makeClusterPSOCK( | |
# Public IP for droplet(s); this can be a vector of IP addresses | |
ip, | |
# User name; DO droplets use root by default | |
user = "root", | |
# Use private SSH key registered with DO | |
# (I actually have no idea if StrictHostKeyChecking and IdentitiesOnly are | |
# necessary—they're from a different tutorial about doing this with AWS.) | |
# ¯\_(ツ)_/¯ | |
rshopts = c( | |
"-o", "StrictHostKeyChecking=no", | |
"-o", "IdentitiesOnly=yes", | |
"-i", ssh_private_key_file | |
), | |
# Script to run | |
rscript = rscript, | |
# Set up .libPaths() for the root user and install future/purrr/furrr packages | |
rscript_args = c( | |
"-e", shQuote("local({p <- Sys.getenv('R_LIBS_USER'); dir.create(p, recursive = TRUE, showWarnings = FALSE); .libPaths(p)})"), | |
"-e", shQuote("if (!requireNamespace('furrr', quietly = TRUE)) install.packages('furrr')") | |
), | |
dryrun = FALSE | |
) | |
# Use the cluster! | |
plan(cluster, workers = cl) | |
future_map(1:5, ~.x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment