我正在尝试使用Plumber(v0.4.6)构建API。我想使用多个.R文件(每个API的功能/端点一个),以避免使用所有功能制作一个巨大的.R文件。使用以下命令仅可以处理一个.R文件:
pr <- plumb("api/v1/plumber.R")
pr$run()
但是当我尝试将水管工文件拆分为两个单独的文件时,已安装的端点不显示:
root <- plumber$new("api/v1/plumber.R")
test <- plumber$new("api/v1/fct1.R")
root$mount("/test",test)
root$run()
这很奇怪,因为root$mounts
显示了所有端点,而API仅显示了根的端点(图和总和):
# Plumber router with 2 endpoints,5 filters,and 1 sub-router.
# Call run() on this object to start the API.
├──[queryString]
├──[postBody]
├──[cookieParser]
├──[sharedSecret]
├──[logger]
├──/plot (GET)
├──/sum (POST)
├──/test
│ │ # Plumber router with 1 endpoint,4 filters,and 0 sub-routers.
│ ├──[queryString]
│ ├──[postBody]
│ ├──[cookieParser]
│ ├──[sharedSecret]
│ └──/test8 (GET)
这是两个文件的代码:
- plumber.R
library(plumber)
#* @apiTitle Plumber Example API
#* Plot a histogram
#* @png
#* @get /plot
function() {
rand <- rnorm(100)
hist(rand)
}
#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @post /sum
function(a,b) {
as.numeric(a) + as.numeric(b)
}
- fct1.R:
#* Echo back the input
#* @param msg The message to echo
#* @get /test8
function(msg = "") {
list(msg = paste0("The message is: '",msg,"'"))
}
感谢您的帮助。