Slide 1

Slide 1 text

1 / 17

Slide 2

Slide 2 text

Feature selection or penalised regression? 2 / 17

Slide 3

Slide 3 text

Are loops really ? 3 / 17

Slide 4

Slide 4 text

How do I get Hadley to follow me? 4 / 17

Slide 5

Slide 5 text

How fast can I run a marathon? 5 / 17

Slide 6

Slide 6 text

Building an API with Plumber & Docker Andrew Collier  www.exegetic.biz  [email protected]  @datawookie  @datawookie 6 / 17

Slide 7

Slide 7 text

Riegel's Formula If distance took , then distance should take . Slowing with distance characterised by (normally ). d1 t1 d2 t2 t 2 = t 1 × ( ) α d 2 d 1 α α = 1.06 [1] Peter Riegel, "Time Predicting", Runner's World Magazine, 1977. [2] Peter Riegel, "Athletic Records and Human Endurance", American Scientist. 1981. 7 / 17

Slide 8

Slide 8 text

Test: Marathon time based on 25:00 for 5 km. riegel(25) [1] 239.8076 Put this in a package? Nah! Just one function. Only accessible from R. There must be a better way. Riegel's Function riegel <- function(time) { time * (42.2 / 5) ** 1.06 } 8 / 17

Slide 9

Slide 9 text

Plumber Turn functions into API. # From CRAN install.packages("plumber") # From GitHub devtools::install_github("trestletech/plumber") 9 / 17

Slide 10

Slide 10 text

Riegel's API Decorate the function. #* @get /riegel function(time, exponent = 1.06) { # API inputs are character. time = as.numeric(time) exponent = as.numeric(exponent) # Fixed distances. distance = 5 goal = 42.2 # Calculate time for goal distance. time * (goal / distance) ** exponent } Launch the API. library(plumber) plumb("riegel-api.R")$run(port=8000) API running on port 8000 on localhost. 10 / 17

Slide 11

Slide 11 text

"Have you heard about the Riegel API?" 11 / 17

Slide 12

Slide 12 text

Docker Create a portable image (API + execution environment) which can be run from anywhere. FROM rocker/r-ver:3.5.2 RUN apt-get update -qq && \ apt-get install -y \ pandoc \ libssl-dev \ libcurl4-gnutls-dev \ libxml2-dev RUN R -e "install.packages(c('plumber', 'dplyr', 'plotly'))" COPY riegel-api.R riegel-api.R EXPOSE 8000 ENTRYPOINT ["R", "-e", "library(plumber); plumb('riegel-api.R')$run(port=8000, host='0.0.0.0')"] Notes on host: 127.0.0.1 — only accept connections from localhost (loopback device) and 0.0.0.0 — accept connections from anywhere. 12 / 17

Slide 13

Slide 13 text

Build the image. $ docker build -t riegel-api . Create a container. $ docker run --rm -p 8000:8000 riegel-api Container port 8000 mapped to host port 8000. Run locally or on AWS or Azure. Runs on any platform that supports Docker Windows Mac Linux No other installs required. No version conflicts. 13 / 17

Slide 14

Slide 14 text

Access Programmatically from Anywhere! # BASH $ curl http://3.84.115.105:8000/riegel?time=25 [239.8076] # R > library(httr) > response = GET("http://3.84.115.105:8000/riegel?time=25") > response Response [http://52.23.233.245:8000/load-shedding] Date: 2019-04-01 07:55 Status: 200 Content-Type: application/json Size: 7 B > content(response, as = "parsed") [[1]] [1] 239.8076 # Python >>> import json, requests >>> response = requests.get("http://3.84.115.105:8000/riegel?time=25") >>> response.status_code 200 >>> json.loads(response.content.decode('utf-8')) [239.8076] 14 / 17

Slide 15

Slide 15 text

Even your Browser 15 / 17

Slide 16

Slide 16 text

Other Serialisers API can return more than just JSON: variety of results from PDF to interactive graphics. 16 / 17

Slide 17

Slide 17 text

Give these tools a try! Give these tools a try! Try these: Try these: Plumber Plumber and and Docker Docker. . Deploying your own API will make you feel like a Deploying your own API will make you feel like a  . . Slides and code available from Slides and code available from http://bit.ly/satrday-joburg-api http://bit.ly/satrday-joburg-api. .   www.exegetic.biz www.exegetic.biz   [email protected] [email protected]   @datawookie @datawookie   @datawookie @datawookie 17 / 17 17 / 17