Running R on Cloud Run (https://cloud.run). Cloud Run lets you run any code within a Docker container, and will scale to billions of hits and down to 0, so you only pay for what you need.
A demo app is available in the
plumber folder of this repo. It copies the "Hello World" examples from plumber ( https://www.rplumber.io/).
You can deploy it on your own GCP project via this button:
The end result should be similar to these Cloud Run URLs:
Adapt the R script in
plumber/api.R for your own uses.
Quickstart: Build and Deploy for R
The procedure below follows the examples for other languages given here but modifies it to run R workloads. In this example an R API created by the wonderful
You need to run the plumber script on a port defined by the system environment variable,
PORT, reachable in R via
Sys.getenv('PORT') The key line in the Dockerfile that achieves this for plumber is this one:
ENTRYPOINT ["R", "-e", "pr <- plumber::plumb(commandArgs()); pr$run(host='0.0.0.0', port=as.numeric(Sys.getenv('PORT')))"]
pr$run must have a numeric value passed to it for the port.
R by default is single-threaded, so only 1 hit per container will be immediately served, other hits will queue until the previous hit has completed. For multi-threading, use
library(future) to serve up to 80 threads (Python uses gunicorn). A demo of combining future and plumber is here
The effect of concurrency on scalability of the Cloud Run app is here
Set up Google Cloud Build Trigger to turn the Dockerfile into a Docker image to call.
- Connect GitHub to Cloud Build
- Configure a trigger to point to the Dockerfile
- Push change to GitHub
Cloud Run deploy
Once the Cloud Build has finished it will give you a Docker URI such as
- Go to https://console.cloud.google.com/run/
- Create a new service, name it something cool
- Put the Docker URI into the Cloud Run field.
- Select public endpoint, and limit concurrency to what your app is configured to handle per instance (I chose 8)
And thats it. A deployed R API.
Use the API
You will then get a URL for the API you can use. For this demo app the endpoints are
/echo?msg="my message" and
/plot (or filter the plot via