如何从另一个脚本读取R Plumber函数参数数据?

如何从另一个脚本读取R Plumber函数参数数据?,r,plumber,R,Plumber,假设有一个RESTAPI的R代码,它基于使用“水管工”包。 这里是它的一个函数 #' Date of sale #' @get /date_of_sale #' @param auth_key Auth key #' @param user_id User ID #' @example https://company.com/date_of_sale?auth_key=12345&user_id=6789 function(auth_key, user_id) { # ... }

假设有一个RESTAPI的R代码,它基于使用“水管工”包。 这里是它的一个函数

#' Date of sale
#' @get /date_of_sale
#' @param auth_key Auth key
#' @param user_id User ID
#' @example https://company.com/date_of_sale?auth_key=12345&user_id=6789
function(auth_key, user_id) {
   # ...
}
假设有另一个R脚本使用对该服务器的API请求,如

  api_string <- "https://company.com/date_of_sale?auth_key=12345&user_id=6789"
  date_of_sale <- jsonlite::fromJSON(api_string)

api_string由于这是一个api GET请求,因此除非在api响应中明确包含该变量,否则无法访问该变量的描述

我学习了一些R脚本,完全是为了回答这个问题,我猜这就是您准备JSON API响应的方式

您可以在JSON请求中执行类似的操作

library(rjson)
auth_key <- "some_key";
user_id <- "340";
x <- list(auth_key = list(
        type = typeof(auth_key),
        lenght = length(auth_key),
        attributes = attributes(auth_key),
        string = auth_key
      ),
      user_id = list(
        type = typeof(user_id),
        lenght = length(user_id),
        attributes = attributes(user_id),
        string = user_id
      ),
      data = "your_data"
    );

#x
json <- toJSON(x, indent=0, method="C" )

fromJSON( json )
库(rjson)

使用文件
plumber.R
和您提供的内容验证密钥。假设它在工作目录中

在R


这同样遵循OpenAPI文档标准。

有趣的想法。但是我在“pr”对象中看不到“getApiSpec()”,您使用的是哪个版本的水管工?也许可以检查
pr\u-read
对象的类,确保
class(pr\u-read)
返回
plumber
Hookable
R6
对不起,这个答案只会使事情复杂化,所以我不喜欢它
pr_read <- plumber::pr("plumber.R")
spec <- pr_read$getApiSpec()
spec$paths$`/date_of_sale`$get$parameters
spec <- jsonlite::fromJSON("{api_server}/openapi.json", simplifyDataFrame = FALSE)
spec$paths$`/date_of_sale`$get$parameters