Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add October 28, 2020 Plumber webinar #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,694 changes: 1,694 additions & 0 deletions 78-integrating-r-with-plumber/R/manifest.json

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions 78-integrating-r-with-plumber/R/model-fit.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: "Model Training"
output: html_notebook
---

```{r setup}
# Packages ----
library(palmerpenguins)
library(tidymodels)
```
## Data
```{r}
penguins
```

```{r}
(filtered_penguins <- penguins %>%
drop_na(ends_with("mm")))
```
## Exploration
```{r}
filtered_penguins %>%
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, col = species)) +
geom_point()
```

```{r}
filtered_penguins %>%
select(species, ends_with("mm"), body_mass_g)
```
## Model Fit
```{r}
model <- rand_forest() %>%
set_engine("ranger") %>%
set_mode("classification") %>%
fit(species ~ bill_length_mm + bill_depth_mm + flipper_length_mm + body_mass_g, data = filtered_penguins)
```

```{r}
model
```

```{r}
predict(model, new_data = head(filtered_penguins), type = "prob")
```

## Model Performance
```{r}
filtered_penguins %>%
bind_cols(predict(model, new_data = filtered_penguins)) %>%
count(species, .pred_class)
```

## Model Export
```{r}
readr::write_rds(model, here::here("R", "model.rds"))
```

1,985 changes: 1,985 additions & 0 deletions 78-integrating-r-with-plumber/R/model-fit.nb.html

Large diffs are not rendered by default.

Binary file added 78-integrating-r-with-plumber/R/model.rds
Binary file not shown.
46 changes: 46 additions & 0 deletions 78-integrating-r-with-plumber/R/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
servers:
- url: ''
openapi: 3.0.3
info:
description: API Description
title: Penguin Predictions
version: 1.0.0
paths:
/health-check:
get:
summary: ' Determine if the API is running and listening as expected'
responses:
default:
description: Default response.
parameters: []
/predict:
post:
summary: ' Predict penguin species based on input data'
responses:
default:
description: Default response.
parameters: []
requestBody:
description: Penguin Data
required: true
content:
application/json:
schema:
type: object
properties:
bill_length_mm:
type: number
title: "Penguin bill length"
example: 46.8
bill_depth_mm:
type: number
title: "Penguin bill depth"
example: 16.1
flipper_length_mm:
type: number
title: "Penguin flipper length"
example: 0
body_mass_g:
type: number
title: "Penguin body mass"
example: 5500
1 change: 1 addition & 0 deletions 78-integrating-r-with-plumber/R/penguins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"bill_length_mm":46.8,"bill_depth_mm":16.1,"flipper_length_mm":215,"body_mass_g":5500},{"bill_length_mm":39.2,"bill_depth_mm":18.6,"flipper_length_mm":190,"body_mass_g":4250},{"bill_length_mm":59.6,"bill_depth_mm":17,"flipper_length_mm":230,"body_mass_g":6050},{"bill_length_mm":38.2,"bill_depth_mm":18.1,"flipper_length_mm":185,"body_mass_g":3950},{"bill_length_mm":36.7,"bill_depth_mm":18.8,"flipper_length_mm":187,"body_mass_g":3800},{"bill_length_mm":50,"bill_depth_mm":16.3,"flipper_length_mm":230,"body_mass_g":5700},{"bill_length_mm":43.2,"bill_depth_mm":16.6,"flipper_length_mm":187,"body_mass_g":2900},{"bill_length_mm":46.4,"bill_depth_mm":15,"flipper_length_mm":216,"body_mass_g":4700},{"bill_length_mm":38.8,"bill_depth_mm":17.6,"flipper_length_mm":191,"body_mass_g":3275},{"bill_length_mm":46.2,"bill_depth_mm":17.5,"flipper_length_mm":187,"body_mass_g":3650}]
42 changes: 42 additions & 0 deletions 78-integrating-r-with-plumber/R/plumber.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Packages ----
# For API
library(plumber)
library(rapidoc)
# For model predictions
library(parsnip)
library(ranger)

# Load model ----
model <- readr::read_rds("model.rds")

# Goal ----
# predict(model,
# new_data = jsonlite::read_json("penguins.json",
# simplifyVector = TRUE),
# type = "prob")

#* @apiTitle Penguin Predictions

#* Determine if the API is running and listening as expected
#* @get /health-check
function() {
list(status = "All Good",
time = Sys.time())
}

#* Predict penguin species based on input data
#* @parser json
#* @serializer csv
#* @post /predict
function(req, res) {
# req$body is the parsed input
predict(model, new_data = as.data.frame(req$body), type = "prob")
}

# Update UI
#* @plumber
function(pr) {
pr %>%
pr_set_api_spec(yaml::read_yaml("openapi.yaml")) %>%
pr_set_docs(docs = "rapidoc")
}
26 changes: 26 additions & 0 deletions 78-integrating-r-with-plumber/README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

# Plumber Webinar

[![](img/title-slide.jpg)](slides/slides.pdf)

This webinar highlights using [Plumber](https://www.rplumber.io/) to solve a common set of data science problems while also highlighting features added in version 1.0.0 of the Plumber package.

## Environment

This project uses [`renv`](https://rstudio.github.io/renv/index.html) for package management. In order to install and use required packages, execute the following:

``` r
renv::restore()
```
20 changes: 20 additions & 0 deletions 78-integrating-r-with-plumber/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# Plumber Webinar

[![](img/title-slide.jpg)](slides/slides.pdf)

This webinar highlights using [Plumber](https://www.rplumber.io/) to
solve a common set of data science problems while also highlighting
features added in version 1.0.0 of the Plumber package.

## Environment

This project uses [`renv`](https://rstudio.github.io/renv/index.html)
for package management. In order to install and use required packages,
execute the following:

``` r
renv::restore()
```
Binary file added 78-integrating-r-with-plumber/img/title-slide.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions 78-integrating-r-with-plumber/plumber-webinar.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
Loading