通过 rsconnect 部署管道工 API(未找到处理程序)

Deploying plumber API via rsconnect (Handler not found)

我有一个包含示例 API 函数的目录 (testAPI.R):

#* @get /mean
normalMean <- function(samples=10){
 library(plumber)
 data <- rnorm(samples)
 mean(data)
}

rsconnect::deployAPI() 函数想要指向一个目录,其中包含一个名为 plumber.R 的文件,returns 一个 plumb 对象。所以我在目录中有文件 plumber.R 作为:

library(plumber)
plumber::plumb("testAPI.R")

然后在目录上调用 rsconnect::deployAPI()...

它部署没有错误,但似乎没有找到处理程序: screenshot of the swagger page

我们这里仍然缺少好的文档,抱歉。这目前只有在部署到 RStudio Connect 服务器时才能正常工作;希望这就是您正在使用的。

在内部,RStudio Connect 使用 plumber::plumb(dir=___) 函数来调用您的 API。您可以在那里查看文档

The directory containing the plumber.R file to parse as the plumber router definition. Alternatively, if an entrypoint.R file is found, it will take precedence and be responsible for returning a runnable Plumber router.

所以你应该...

  1. 将您的主路由器重命名为 plumber.R,在这种情况下,RStudio Connect 会在执行您的 API 时找到并调用该文件。或者
  2. 创建一个 entrypoint.R 文件,其中 returns 您的主路由器。这种方法允许您在路由器上进行一些额外的自定义(或探测不同的文件)。如果您将它指定为 entrypoint.R 文件而不是 plumber.R 文件,那么您提供的代码片段看起来确实有效。