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

How to Resize a Video and Preserve Aspect Ratio in Java

Static CMS Tutorial: Creating an e-Commerce Site — Forge

Programmer Fables — MUST READ!

Bukkit Inventory Slot Numbers

Skyrim underwear slot

from F# to Scala : case class & object

Turning On an LED with a Single Touch and Observing Hall Effect using ESP-32

Building My First Web App From Scratch

100 Days of Code — #100DayOfCode — again?

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

How to connect Excel to ClickHouse within Kyligence Tiered Storage

Spatial Inequity in Private Transportation Access as Determined by Ride-share Demand…

Introduction to Dockstore

Translate text through Google Translate using Python