使用 R 构建 RESTful API [英] Building RESTful API using R

查看:34
本文介绍了使用 R 构建 RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用编程语言 R 构建 RESTful API,主要是为了以 API 格式向用户公开我的机器学习模型.我知道有一些选项,比如导出到 PMML、PFA 和使用其他语言来处理 API 部分.但是,我想坚持使用相同的编程语言,并且想知道 R 中是否有类似 Flask/Django/Springbook 的框架?

我查看了 servr/闪亮 但我真的不认为 RESTful 是它们的设计目的.R 中是否有更易于使用的更好解决方案?

解决方案

我有两个选择:

管道工

<块引用>

plumber 允许您通过装饰现有的 R 源来创建 REST API带有特殊注释的代码.

一个小示例文件:

# myfile.R#* @get/meannormalMean <- 函数(样本=10){数据 <- 范数(样本)平均值(数据)}#* @post/sumaddTwo <- function(a, b){as.numeric(a) + as.numeric(b)}

从 R 命令行:

<代码>>图书馆(水管工)>r <- plumb("myfile.R") # 其中 'myfile.R' 是上面显示的文件的位置>r$run(端口=8000)

这样你会得到这样的结果:

$ curl "http://localhost:8000/mean"[-0.254]$ curl "http://localhost:8000/mean?samples=10000"[-0.0038]

<小时>

水壶

<块引用>

Jug 是一个用于 R 的小型 Web 开发框架,它严重依赖于httpuv 包.它的主要重点是为您构建 API代码尽可能简单.它不应该是性能特别好,也不是超级稳定的网络框架.其他工具(和语言)可能更适合这种情况.它的主要重点是轻松地允许您为 R 代码创建 API.然而Jug 的灵活性意味着,理论上,您可以构建一个广泛的带有它的 Web 框架.

它非常容易学习,并且有一个漂亮的小插图.

Hello-World 示例:

图书馆(壶)壶()%>%get("/", function(req, res, err){你好,世界!"})%>%simple_error_handler_json() %>%serve_it()

I am thinking about building a RESTful API using the programming language R, mainly to expose my machine learning model to the user in an API format. I know there are some options like export to PMML, PFA and use other languages to take care of the API part. However, I want to stick to the same programming language and was wondering if there is anything like Flask/Django/Springbook framework in R?

I took a look at servr/shiny but I really don't think RESTful is what they are designed for. Is there any better solution within R that is more easy to use?

解决方案

I have two options for you:

plumber

plumber allows you to create a REST API by decorating your existing R source code with special comments.

A small example file:

# myfile.R

#* @get /mean
normalMean <- function(samples=10){
  data <- rnorm(samples)
  mean(data)
}

#* @post /sum
addTwo <- function(a, b){
  as.numeric(a) + as.numeric(b)
}

From the R command line:

> library(plumber)
> r <- plumb("myfile.R")  # Where 'myfile.R' is the location of the file shown above
> r$run(port=8000)

With this you would get results like this:

$ curl "http://localhost:8000/mean"
 [-0.254]
$ curl "http://localhost:8000/mean?samples=10000"
 [-0.0038]


Jug

Jug is a small web development framework for R which relies heavily upon the httpuv package. It’s main focus is to make building APIs for your code as easy as possible. It is not supposed to be either an especially performant nor an uber stable web framework. Other tools (and languages) might be more suited for that. It’s main focus is to easily allow you to create APIs for your R code. However, the flexibility of Jug means that, in theory, you could built an extensive web framework with it.

It very easy to learn and has a nice vignette.

An Hello-World-example:

library(jug)

jug() %>%
  get("/", function(req, res, err){
    "Hello World!"
  }) %>%
  simple_error_handler_json() %>%
  serve_it()

这篇关于使用 R 构建 RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆