r - 使用R构建RESTful API

标签 r rest api plumber

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

我看了一下servr / shiny,但我真的不认为RESTful是他们设计的。 R 中是否有更易于使用的更好的解决方案?

最佳答案

我有两种选择供您选择:

plumber

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)

这样,您将得到如下结果:
$ 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.



它很容易学习,并且带有nice vignette

一个Hello World示例:
library(jug)

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

关于r - 使用R构建RESTful API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38079580/

相关文章:

html - 如何将 URL 链接添加到顶部导航栏

r -//parent::* 在 XPath 中?

c# - 如何将复杂类作为参数传递给 httpget

api - Tridion 2011中的StructureGroup IsPublishable属性

java - 我们如何在 Eclipse 中安装 Java Api 帮助?

r - 以概率选择特定数字

r - 向量化包含循环和 if 子句的搜索函数

json - 请求正文中的 JSON 数据无效 : Syntax error POST Call Rest API YII2

wordpress - Flutter如何在一个屏幕上调用多个api(使用futurebuilders)?

windows - 如何在不出现临时中断的情况下扩展我的 Azure 应用程序?