You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2021-05-12-r-templates.md
+24-12Lines changed: 24 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Functions for data science with R templates for OpenFaaS"
3
-
description: "Let's bring R to the cloud! Use the power of R for data science serverless-style."
4
-
date: 2021-05-14
3
+
description: "Learn how to combine the power of R with Serverless for data science."
4
+
date: 2021-05-12
5
5
image: /images/2021-05-r/background.jpg
6
6
categories:
7
7
- kubernetes
@@ -11,11 +11,13 @@ author_staff_member: peter
11
11
dark_background: true
12
12
---
13
13
14
-
Let's bring R to the cloud! Use the power of R for data science serverless-style.
14
+
Learn how to combine the power of R with Serverless for data science.
15
15
16
16
## Introduction
17
17
18
-
In this post first I will introduce you to the [R](https://www.r-project.org/) templates for OpenFaaS, then I will build a function that pulls data from a COVID-19 API, fits a time series model to the data, and makes a forecast for the future case counts.
18
+
> This is a guest post by Peter Solymos. Peter reached out to me after becoming an OpenFaaS Sponsor. He wanted to help other data-scientists like himself understand the value of combining R with functions in the cloud. Hope you enjoy the post and feel free to reach out to us via [OpenFaaS Slack](https://slack.openfaas.io/) with comments, questions and suggestions.
19
+
20
+
In this post I will introduce you to the [R](https://www.r-project.org/) templates for OpenFaaS, then I will build a function that pulls data from a COVID-19 API, fits a time series model to the data, and makes a forecast for the future case counts.
19
21
20
22
> This post is written for existing OpenFaaS users, if you're new then you should [try deploying OpenFaaS](https://docs.openfaas.com/deployment/) and following a tutorial to get a feel for how everything works. Why not start with this course? [Introduction to Serverless course by the LinuxFoundation](https://www.openfaas.com/blog/introduction-to-serverless-linuxfoundation/)
21
23
@@ -32,7 +34,8 @@ Besides the interactive data wrangling use cases, R is also a capable scripting
32
34
Use the [`faas-cli`](https://github.com/openfaas/faas-cli) and pull R templates:
Now `faas-cli new --list` should give you a list with the available R/rstats templates to choose from (rstats refers to the #rstats Twitter hashtag used for R related posts). The templates differ with respect to the Docker base image, the OpenFaaS watchdog type, and the server framework used.
@@ -56,17 +59,24 @@ In this post I focus on the [plumber](https://www.rplumber.io/) R package and th
56
59
Let's define a few variables then use `faas-cli new` to create a new function called `covid-forecast` based on the `rstats-base-plumber` template:
57
60
58
61
```bash
59
-
export OPENFAAS_PREFIX=""# Populate with your Docker Hub username
60
-
export OPENFAAS_URL="http://174.138.114.98:8080"# Populate with your OpenFaaS URL
62
+
# Populate with your registry prefix or Docker Hub username
63
+
export OPENFAAS_PREFIX=""
64
+
65
+
# Populate with your OpenFaaS URL
66
+
export OPENFAAS_URL="http://174.138.114.98:8080"
61
67
62
-
faas-cli new --lang rstats-base-plumber covid-forecast --prefix=$OPENFAAS_PREFIX
68
+
faas-cli new --lang rstats-base-plumber \
69
+
covid-forecast --prefix=$OPENFAAS_PREFIX
63
70
```
64
71
65
72
Your folder now should contain the following files:
@@ -176,7 +186,7 @@ The following plot combines the historical daily case counts and the 30-day fore
176
186
177
187
The last part of the script defines the Plumber endpoint `/` for a GET request. One of the nicest features of Plumber is that you can create a web API by [decorating the R source code](https://www.rplumber.io/articles/quickstart.html) with special `#*` comments. These annotations will tell Plumber how to handle the requests, what kind of parsers and formatters to use, etc. The current setup will treat the function arguments as URL parameters. The default content type for the response is JSON, thus we do not need to specify it.
178
188
179
-
```R
189
+
```
180
190
#* COVID
181
191
#*@get /
182
192
function(region, cases, window, last) {
@@ -207,7 +217,9 @@ curl -X GET -G \
207
217
- last=2021-02-18
208
218
```
209
219
210
-
Or simply by visiting the URL `$OPENFAAS_URL/function/covid-forecast?region=canada-combined&window=4&last=2021-02-18`. The output should be something like this:
220
+
Or simply by visiting the URL `$OPENFAAS_URL/function/covid-forecast?region=canada-combined&window=4&last=2021-02-18`.
0 commit comments