Hosting an R Plumber API using Azure App Service

Introduction

Prerequisites

  • You need an Azure subscription to set up the required services.
  • You are able to access the Azure Command Line Interface (CLI).
  • Docker is (or can be) installed on your local machine or the server you’re intending to build the image from.
  • To execute certain commands, you need sudo or admin rights.

Project overview

plumber-to-azure-demo 
|__ Dockerfile
|__ execute_plumber.R
|__ plumber.R

Creating a simple R Plumber API

# plumber.R# libraries 
library(plumber)
#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg="") {
list(msg = paste0("The message is: '", msg, "'"))
}
#* Plot a histogram
#* @serializer png
#* @get /plot
function() {
rand <- rnorm(100) hist(rand)
}
#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @post /sum
function(a, b) {
as.numeric(a) + as.numeric(b)
}
# execute_plumber.R# Execute plumber 
plumber::plumb(file='plumber.R')$run(host = '0.0.0.0', port = 8080)

Setting up Azure Container Registry

Creating, building and pushing a Docker image

# DockerfileFROM rocker/r-base:4.0.3 # install the linux libraries needed for plumber 
RUN apt-get update -qq && apt-get install -y \
libssl-dev \
libcurl4-gnutls-dev \
libsodium-dev
# create the application folder
RUN mkdir -p ~/application
# copy everything from the current directory into the container
COPY "/" "application/"
WORKDIR "application/"
# open port 8080 to traffic
EXPOSE 8080
# install plumber
RUN R -e "install.packages('plumber')"
# when the container starts, start the main R script ENTRYPOINT ["Rscript", "execute_plumber.R"]

Using Azure App Service to host your Plumber API

Note that you won’t be able to access this actual URL- IP access restrictions were added
Note that you won’t be able to access this actual URL- IP access restrictions were added

Next steps

--

--

--

🚀 Entrepreneur and Data Scientist who gets excited about data and programming | Vue, Node.js, R, Shiny, Azure

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Dogmatic Software Development

The Unquantifiable Heart & Soul of Community

Migrating an IBM i LPAR to Skytap

Test-driven development an exiersice for the university

A Peek Under the Ruby Hood to Understand Hoisting

Creating A Reload Animation in Unity

How to create and apply a patch with Git Diff and Git Apply commands for your Drupal website

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Veerle van Leemput

Veerle van Leemput

🚀 Entrepreneur and Data Scientist who gets excited about data and programming | Vue, Node.js, R, Shiny, Azure

More from Medium

Index Geo Flight Data Using Azure Search Push API (Python version)

What is (and what isn’t) a Digital Twin

Book Review: Hands-On Azure Digital Twins

Develop Machine Learning Models with Zero Coding in Azure Machine Learning Studio