GithubHelp home page GithubHelp logo

analythium / openfaas-rstats-templates Goto Github PK

View Code? Open in Web Editor NEW
19.0 3.0 1.0 269 KB

OpenFaaS templates for R

License: MIT License

Dockerfile 39.40% R 55.53% Shell 5.07%
faas openfaas r rstats functions functions-as-a-service plumber templates

openfaas-rstats-templates's People

Contributors

mrchypark avatar psolymos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

axcelhq

openfaas-rstats-templates's Issues

ROADMAP

This is the roadmap for revising the R/rstats templates for OpenFaaS.

Base images

We need the following base image options (BASEIMAGE below):

  • Debian based rocker/r-base: consistency for most adopted base images, call this base;
  • Ubuntu based rocker/r-ubuntu: long term support can be important in corporate context and it jives with RSPM, call this ubuntu;
  • Alpine based r-hub/r-minimal: small size is always a good thing, call this minimal.

Watchdog version

  • Classic watchdog with stdin/stdout: call these images rstats-BASEIMAGE;
  • of-watchdog with http support: call these images rstats-BASEIMAGE-FRAMEWORK.

Existing server frameworks

The following are server frameworks capable of running R based APIs:

  • Rserve: very bare-bones implementation (RestRserve wraps this), rstats-BASEIMAGE-rserve;
    • RestRserve: a full-fledged API framework based on Rserve, rstats-BASEIMAGE-restserve;
  • httpuv: the most popular server option behind other frameworks, rstats-BASEIMAGE-httpuv;
    • plumber: rstats-BASEIMAGE-plumber;
    • opencpu: rstats-BASEIMAGE-opencpu;
    • fiery: rstats-BASEIMAGE-fiery;
    • beakr: rstats-BASEIMAGE-beakr.

Template structure and workflows

  • template.yml: yaml file for OpenFaaS, docker user, function name etc might have to be edited, see OpenFaaS config.
  • Dockerfile: usually does not need editing.
  • index.R: this file abstracts the server framework. Templates are set up for JSON in JSON out re/res types. This file only needs to be edited if the mime type depends on different midlewear, headers need to be changed etc.
  • function/PACKAGES: file listing required packages and remotes to be installed for the function/handler.R file, plus additional sysreqs.
  • function/handler.R: R script implementing the handler:
    • load libraries;
    • load data;
    • define functions/methods as needed;
    • it should include the handle = function(req, res) { ... } function called by index.R.

I really like how the r-minimal image implements remotes::install_deps() with the _R_SHLIB_STRIP_=true envvar. This behavior should be implemented for other templates through the Dockerfile as added by @mrchypark for the r-minimal template.

Add R function examples

Add examples for the 9 core templates. Examples 1-3 are JSON based ones. Examples 4-5 require mime type modifications.

Example 1: Hello

Simple Hello World example.

library(jsonlite)

hello <- function(x) {
    toJSON(paste0("Hello ", fromJSON(x), "!"))
}

hello('["World"]')
# ["Hello World!"]

Example 2: PCA

Calculate principal coordinates from an input matrix. This will demonstrate how to add dependency vegan.

library(vegan)
data(dune)
x <- dune
o <- rda(x)
s <- scores(o, 1:3)$sites

Example 3: classification

Train model and deploy scoring engine for multinomial classification using the iris data.
Needs dependency and loading data (trained model)

library(e1071)
library(jsonlite)
data(iris)

s <- svm(Species ~ ., iris)

table(iris$Species, fitted(s))

# expect setosa
v <- '{"Sepal.Length":5.2,"Sepal.Width":3.4,"Petal.Length":1.5,"Petal.Width":0.2}'
z <- as.data.frame(fromJSON(v))
toJSON(predict(s, z))

Explore this from of-watchdog readme:

Files or models can be fetched and stored in /tmp/ as a one-off initialization task and used for all requests after that

Example 4: report

Report generation based on rmarkdown and whisker.

library(rmarkdown)
library(whisker)


template <-
'---
title: Template
output: {{format}}
---

# Hello {{name}}!

This document was auto generated on {{date}}.

{{#signature}}
xoxo
{{/signature}}'

data <- list( name = "Brett",
            date = as.character(Sys.Date()),
            signature=TRUE,
            filename="out",
            format="pdf_document")

v <- whisker.render(template, data)
cat(v)

f <- paste0(data$filename, ".Rmd")
#fout <- paste0(data$filename, ".", data$format)
writeLines(v, f)

render(f)

unlink(f)
unlink(paste0(data$filename, ".pdf"))

Example 5: ETL

Extract-Transform-Load example.

r-minimal build dependencies

Add BuildRequirements to DESCRIPTION file (similar to r-hub's install script) to identify dependencies that can be removed after R package installation. gcc musl-dev g++ can be added to Dockerfile, maybe even gfortran linux-headers:

# Istall SystemRequirements for handler.R from DESCRIPTION
RUN R -q -e 'source("/usr/local/bin/install.R"); install$sysreqs()'
RUN echo requirements.txt
RUN apk update
RUN apk add --no-cache gcc musl-dev g++ gfortran linux-headers
RUN xargs -a requirements.txt apk add

# Install packages (incl. remotes) for handler.R from DESCRIPTION
RUN R -q -e 'remotes::install_deps()'

# Install VersionedPackages for handler.R from DESCRIPTION
RUN R -q -e 'source("/usr/local/bin/install.R"); install$versioned()'

# Clean up
RUN apk del gcc musl-dev g++ gfortran linux-headers
RUN rm -rf /var/cache/apk/*
RUN rm requirements.txt

Add FaaS template based on httpuv

Should modify plumber based template.

# index.R

#!/usr/bin/env Rscript

suppressMessages(library(httpuv))

source("function/handler.R")

# curl http://localhost:5000 -H "Content-Type: application/json" -d '["Friend"]'

httpuv::runServer(
  host = "0.0.0.0",
  port = 5000,
  app = list(
    call = function(req) {
      list(
        status = 200L,
        headers = list(
          'Content-Type' = 'application/json'
        ),
        body = handle(req)
      )
    }
  )
)
# handler.R

handle <- function(req) {
  input <- req[["rook.input"]]
  postdata <- input$read_lines()
  jsonlite::toJSON(
    paste0("Hello ", jsonlite::fromJSON(
      paste(postdata)), "!"))
}

DESCRIPTION to list jsonlite (not a dependency for httpuv)

Explore multi-arch images

This post and this PR.

The faas-cli help publish says:

Builds and pushes multi-arch OpenFaaS container images using Docker buildx.
Most users will want faas-cli build or faas-cli up for development and testing.
This command is designed to make releasing and publishing multi-arch container
images easier.

A stack.yaml file is required, and any images that are built will not be
available in the local Docker library. This is due to technical constraints in
Docker and buildx. You must use a multi-arch template to use this command with
correctly configured TARGETPLATFORM and BUILDPLATFORM arguments.

Dependency management for rstats faas

Options:

  1. requirements.txt file. This is recognized as Python dependency by GitHub and is misleading. But the idea (1 dependency per line) is a good one, should be called PACKAGES or DEPENDENCIES
  2. Use DESCRIPTION file instead: this is again misleading, this is not a package, and distinctions such as Depends, Imports, Remotes does not make much sense.

The install.R installs dependencies as specified in, one dependency per line, separator is new line.

CRAN packages can be specified by their names, or as name@version.

Remotes can be defined according to specs in the
{remotes} package.

Add CICD examples

GitHub actions + ghcr or Docker Hub, GitLab pipelines + GitLab registry.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.