Stan on the cloud

I put together an IR jupyter notebook for use on Google Colab here:

Google’s Colab gives you a VM - here’s someone’s notebook that you can run to see what you get: https://colab.research.google.com/drive/151805XTDg--dgHb3-AXJCpnWaqRhop_2
the VM goes away after 12 hours. so for the price of a Google account (free), you get a cloud VM that is useful for demos, teaching, etc.

Colab can run IR jupyter notebooks - I started from here: R Jupyter Notebook + RStan on Google Colab

to get minimal install, what I did was create a “bootstrap” IPython notebook on Colab, where I used package rpy2.ipython - this let me install a bunch of R packages to a local directory on the VM - here’s the ipython notebook code block:

%%R
# LIBRARY CONFIGURATION
install.packages('StanHeaders', lib='RStanLibs')
install.packages('bayesplot', lib='RStanLibs')
install.packages('rstan', lib='RStanLibs')

then I tar-gzip’d everything up:

!tar cf - RStanLibs | gzip > RStanLibs.tgz

and then put that onto a Google Cloud Services bucket. as long as the Google Colab notebooks are all running the same VM, this minimizes the install process - in the IR notebook, the first code block downloads the pre-compiled RStan libraries:

# Install pre-compiled R packages for StanHeaders, bayesplot, and rstan
if (!file.exists("RStanLibs.tgz")) {
  system("wget https://storage.googleapis.com/rlibs-rstan-plus/RStanLibs.tgz", intern=T)
  system("tar zxf RStanLibs.tgz", intern=T)
  system("mv RStanLibs/* /usr/lib/R/site-library")
}

it’s a hack, but it works.

next up, similar demo for CmdStanPy on Colab.

3 Likes