gpt4 book ai didi

r - 使用R构建RESTful API

转载 作者:行者123 更新时间:2023-12-03 09:23:10 25 4
gpt4 key购买 nike

我正在考虑使用编程语言 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com