analythium / openfaas-rstats-templates Goto Github PK
View Code? Open in Web Editor NEWOpenFaaS templates for R
License: MIT License
OpenFaaS templates for R
License: MIT License
This is the roadmap for revising the R/rstats templates for OpenFaaS.
We need the following base image options (BASEIMAGE below):
rocker/r-base
: consistency for most adopted base images, call this base;rocker/r-ubuntu
: long term support can be important in corporate context and it jives with RSPM, call this ubuntu;r-hub/r-minimal
: small size is always a good thing, call this minimal.The following are server frameworks capable of running R based APIs:
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:
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 examples for the 9 core templates. Examples 1-3 are JSON based ones. Examples 4-5 require mime type modifications.
Simple Hello World example.
library(jsonlite)
hello <- function(x) {
toJSON(paste0("Hello ", fromJSON(x), "!"))
}
hello('["World"]')
# ["Hello World!"]
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
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
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"))
Extract-Transform-Load example.
Add support for the httpuc based fiery framework. @mrchypark 's PR #17 addresses this.
System dependencies should be added to the YAML file as build option
or passed as build argument.
For this, add ADDITIONAL_PACKAGE
ARG
to the Dockerfile.
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
Wait until 0.8.4 release and update/test templates.
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)
This should work: https://www.opencpu.org/api.html#api-json
Only Get requests are getting through, but not POST.
See this branch.
beakr is another minimalist web framework based on httpuv.
Check out golang, node12 (alpine based), csharp (debian based) templates, https://github.com/openfaas/templates
Need to understand why remotes::install_deps() does not install packages from Remotes field in the DESCRIPTION.
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.
I hope to add template using RestRserve package for template.
Options:
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
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 name
s, or as name@version
.
Remotes can be defined according to specs in the
{remotes} package.
The httpuv based ambiorix is the new kid on the block. Let's add to the list of templates.
GitHub actions + ghcr or Docker Hub, GitLab pipelines + GitLab registry.
Pin R version instead of using the latest tag.
Use rstudio/r-base:4.0.4-focal instead of rocker/r-ubuntu:
Also update watchdog version.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.