如何在httr中执行plumber :: plumb(file ='mypath')?

问题描述 投票:0回答:1

我正在努力寻找一个解决方案,使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!
r httr plumber
1个回答
0
投票

[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也有多种选择,RestRserveOpenCPU仅举几例。

根据您的用例,选择最适合您的方法。如果您需要任何管道工帮助,请随时询问。我看不到它已经集成到RStudio IDE中了,我认为它的修饰结构la roxygen2使它易于采用。

© www.soinside.com 2019 - 2024. All rights reserved.