我可以从外部应用调用构建R服务器REST API的建议吗?
我已经看过很多关于从其他RESTful API服务中使用R中的数据的文章,但是我真的很难找到与之相反的任何文章.我对R作为服务器而不是客户端很感兴趣.我想要一个Node.js应用程序来调用R服务器的RESTful API,这样我就可以利用特定的分析功能,例如多季节预测.有人有什么想法吗?
I've seen lots of articles about consuming data in R from other RESTful API services, but I have really struggled to find any articles about the reverse. I'm interested in R being the server, and not the client. I'd like a Node.js app to call a RESTful API of an R-server so I can leverage specific analytical functions such as multi-seasonality forecasting. Anyone have any ideas?
您可以使用httpuv
启动基本服务器,然后处理GET
/POST
请求.以下内容本身不是"REST",但应提供基本框架:
You can use httpuv
to fire up a basic server then handle the GET
/POST
requests. The following isn't "REST" per se, but it should provide the basic framework:
library(httpuv)
library(RCurl)
library(httr)
app <- list(call=function(req) {
query <- req$QUERY_STRING
qs <- httr:::parse_query(gsub("^\\?", "", query))
status <- 200L
headers <- list('Content-Type' = 'text/html')
if (!is.character(query) || identical(query, "")) {
body <- "\r\n<html><body></body></html>"
} else {
body <- sprintf("\r\n<html><body>a=%s</body></html>", qs$a)
}
ret <- list(status=status,
headers=headers,
body=body)
return(ret)
})
message("Starting server...")
server <- startServer("127.0.0.1", 8000, app=app)
on.exit(stopServer(server))
while(TRUE) {
service()
Sys.sleep(0.001)
}
stopServer(server)
我在其中有httr
和RCurl
软件包,因为您可能最终需要使用两者的某些位来解析/格式化/etc请求&回应.
I have the httr
and RCurl
packages in there since you'll probably end up needing to use some bits of both to parse/format/etc requests & responses.