我正在努力寻找一个解决方案,使httr可以与R中的管道工做同样的事情。在管道工中,下面的代码有效:
1)。创建脚本hello_world_plumber.R
#' @post /hello_world
hi <- function(name) {
return(paste0('Hello ', name, '!'))
}
2)。在另一个脚本中:
pr <- plumb(file = '~/hello_world_plumber.R')
pr$run(swagger = F)
Starting server to listen on port 5852
Running the swagger UI at http://127.0.0.1:5852/__swagger__/
3)。在终端
curl -s --data 'Matt' http://127.0.0.1:5852/hello_world
Out: Hello Matt!
[plumber
建立在httpuv
库的顶部,可帮助您通过一些修饰来快速定义API。
[httr
包将通过curl命令发送http查询。
所以httr
就像一个客户端,plumber
是您的服务器。这就是为什么会有更多的下载。 Chrome / Firefox的下载量要比Nginx / Apache的下载量还多,这只是这些工具的本质。
如果您不关心修饰,异步,查询字符串和正文解析,openapi和其他优点,则只需使用httpuv
就可以实现相同的功能而无需使用管道工。它几乎是光秃秃的。
runServer("127.0.0.1", 8477,
list(
call = function(req) {
list(
status = 200L,
headers = list(
'Content-Type' = 'text/html'
),
body = "Hello world!"
)
}
)
)
plumber
也有多种选择,RestRserve
,OpenCPU
仅举几例。
根据您的用例,选择最适合您的方法。如果您需要任何管道工帮助,请随时询问。我看不到它已经集成到RStudio IDE中了,我认为它的修饰结构la roxygen2
使它易于采用。