Skip to content

Commit 966c98c

Browse files
committed
Go live with Peter's blog post
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent 002b913 commit 966c98c

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

_posts/2021-05-14-r-templates.md renamed to _posts/2021-05-12-r-templates.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
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
55
image: /images/2021-05-r/background.jpg
66
categories:
77
- kubernetes
@@ -11,11 +11,13 @@ author_staff_member: peter
1111
dark_background: true
1212
---
1313

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.
1515

1616
## Introduction
1717

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.
1921

2022
> 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/)
2123
@@ -32,7 +34,8 @@ Besides the interactive data wrangling use cases, R is also a capable scripting
3234
Use the [`faas-cli`](https://github.com/openfaas/faas-cli) and pull R templates:
3335

3436
```bash
35-
faas-cli template pull https://github.com/analythium/openfaas-rstats-templates
37+
faas-cli template pull \
38+
https://github.com/analythium/openfaas-rstats-templates
3639
```
3740

3841
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
5659
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:
5760

5861
```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"
6167

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
6370
```
6471

6572
Your folder now should contain the following files:
6673

6774
```bash
75+
# ls
76+
6877
covid-forecast/handler.R
6978
covid-forecast/DESCRIPTION
79+
7080
covid-forecast.yml
7181
```
7282

@@ -96,7 +106,7 @@ VersionedPackages:
96106
97107
Change the `covid-forecast/handler.R` file:
98108

99-
```R
109+
```
100110
library(forecast)
101111

102112
covid_forecast <- function(region, cases, window, last) {
@@ -152,7 +162,7 @@ The R script loads the forecast package, defines the `covid_forecast` function w
152162
153163
The function gives the following output in R:
154164
155-
```R
165+
```
156166
covid_forecast("canada-combined", cases="confirmed", window=4, last="2021-02-18")
157167
# $Date
158168
# [1] "2021-02-19" "2021-02-20" "2021-02-21" "2021-02-22"
@@ -176,7 +186,7 @@ The following plot combines the historical daily case counts and the 30-day fore
176186
177187
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.
178188
179-
```R
189+
```
180190
#* COVID
181191
#* @get /
182192
function(region, cases, window, last) {
@@ -207,7 +217,9 @@ curl -X GET -G \
207217
- last=2021-02-18
208218
```
209219

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`.
221+
222+
The output should be something like this:
211223

212224
```bash
213225
{

0 commit comments

Comments
 (0)