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

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

问题描述

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

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?

推荐答案

我为您提供两个选择:

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

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

一个小示例文件:

# 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)
}

从R命令行:

> 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

Jug是R的小型Web开发框架,它严重依赖于 httpuv软件包.主要重点是为您的应用构建API 代码越简单越好.它不应该是 尤其是高性能的或超级稳定的Web框架.其他工具 (和语言)可能更适合于此.它的主要重点是 轻松地让您为R代码创建API.但是,那 水罐的灵活性意味着,从理论上讲,您可以建立一个广泛的 Web框架.

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.

Hello-World-示例:

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天全站免登陆