Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将curl变量动态地发送到plumber函数?_R_Api_Machine Learning_Deployment_Plumber - Fatal编程技术网

如何将curl变量动态地发送到plumber函数?

如何将curl变量动态地发送到plumber函数?,r,api,machine-learning,deployment,plumber,R,Api,Machine Learning,Deployment,Plumber,我想基于任意数量的输入变量动态调用PlumberAPI。我需要将curl输入映射到函数名的输入。例如,如果函数有一个输入hi,那么curl-s-data“hi=2”表示hi=2应该作为输入参数传递给函数。这可以通过match.call直接在R中完成,但是通过PlumberAPI调用它时失败了 担任职务 #' @post /API #' @serializer unboxedJSON tmp <- function(hi) { out <- list(hi=hi) out

我想基于任意数量的输入变量动态调用PlumberAPI。我需要将curl输入映射到函数名的输入。例如,如果函数有一个输入hi,那么curl-s-data“hi=2”表示hi=2应该作为输入参数传递给函数。这可以通过match.call直接在R中完成,但是通过PlumberAPI调用它时失败了

担任职务

#' @post /API
#' @serializer unboxedJSON
tmp <- function(hi) {

  out <- list(hi=hi)

  out <- toJSON(out, pretty = TRUE, auto_unbox = TRUE)

  return(out)

}

tmp(hi=2)
out: {hi:2}
#' @post /API
#' @serializer unboxedJSON
tmp <- function(...) {

  out <- match.call() %>%
         as.list() %>%
         .[2:length(.)] # %>%

  out <- toJSON(out, pretty = TRUE, auto_unbox = TRUE)

  return(out)

}
tmp(hi=2)
out: {hi:2}
一切看起来都很好。但是,以函数为例

#' @post /API
#' @serializer unboxedJSON
tmp <- function(hi) {

  out <- list(hi=hi)

  out <- toJSON(out, pretty = TRUE, auto_unbox = TRUE)

  return(out)

}

tmp(hi=2)
out: {hi:2}
#' @post /API
#' @serializer unboxedJSON
tmp <- function(...) {

  out <- match.call() %>%
         as.list() %>%
         .[2:length(.)] # %>%

  out <- toJSON(out, pretty = TRUE, auto_unbox = TRUE)

  return(out)

}
tmp(hi=2)
out: {hi:2}
实际上,我真正想做的是加载我的ML模型,用管道工API预测分数。比如说

当在本地运行时,此函数会按预期工作,但通过curl-s-data'var1=1&var2=2…等运行APIhttp://listen_address


我得到以下错误:{error:500-Internal server error,message:as.data.frame.defaultx[[I]]中的错误,optional=TRUE:cannot强制class\c\plumperresponse\,\R6\\to a data.frame\n}

Internal plumper将请求中的参数与函数中的参数名称相匹配。您可以使用一些特殊参数来研究请求中的所有参数。如果您有一个名为req的参数,它将为您提供一个包含整个请求元数据的环境,其中一个是req$args。然后可以对其进行解析。前两个参数是特殊参数req和res的自引用。它们是环境参数,不应序列化。我不建议执行任何生产代码中显示的操作,因为这会导致api被滥用


model有没有一种方法可以在不更改curl请求的情况下执行此操作?理想情况下,我会更改代码来处理curl-s-data'myvar1=2&myvar2=3'listen_addressyes,使用'req'参数并从req$args检索所有参数。此外,您不需要执行JSON,因为序列化程序将为您执行此操作。感谢您的帮助。第二个功能有效,但第一个功能无效。这是我的函数“@post/API”@serializer unbxedjson predict_score在returnreq$args行之前放置一个浏览器调用。运行它并用curl调用它。它应该进入调试模式,并允许您在执行时探索可用的内容。正如我所说,req$args包含环境,因此您将无法序列化它们。我仍然不建议这样做,因为您将API暴露于注入和不受保护的行为中。
model <- readRDS('model.rds') # Load model as a global variable

predict_score <- function(...) {

    df_in <- match.call() %>%
        as.list() %>%
        .[2:length(.)] %>%
        as.data.frame()

    json_out <- list(
        score_out = predict(model, df_in) %>%
        toJSON(., pretty = T, auto_unbox = T)

    return(json_out)
}

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"df_in":{"hi":2, "othercrap":4}}' \
  http://listen_address