0

我的 Plumber API 中有一个简单的函数,如下所示:

library(methods)
library(plumber)

# Other functions...

#' @param elist The list of events to process as a string
#' @get /process
process_events <- function(elist=""){
  setClass("EventPattern", representation(sequence="character", probability="numeric", endProbs="data.frame"))

  q <- new("EventPattern", sequence=elist, probability=1, endProbs=data.frame(None=0))
  # Further code that should make use of q
}

我启动 Plumber(本地)并将其指向包含 api(以上)的脚本,如下所示:

r <- plumb('/path/to/script/forecast.R')
r$run(port=8000, swagger = TRUE)

并调用地址上的函数(使用 PostMan):

http://localhost:8000/process?elist="abcd"

但我最终得到的是“发生异常”,R 控制台说:

<simpleError: No method for S4 class:EventPattern>

我意识到该错误表明需要一个方法(泛型),但是当我键入时:

q <- new("EventPattern", sequence=elist, probability=1, endProbs=data.frame(None=0))

在我的机器上本地(在 R 控制台中)它工作正常。它向我表明某些东西没有完全加载或对 Plumber 可用,但我不知道如何修复它。有任何想法吗?

4

1 回答 1

1

我以前没setClass用过new。但我去年和管道工一起工作过。我发现使用您的示例它正在尝试返回q,并因此引发错误。

添加打印语句似乎可以防止错误:

library(methods)
library(plumber)

# Other functions...

#' @param elist The list of events to process as a string
#' @get /process
process_events <- function(elist=""){
  setClass("EventPattern", representation(sequence="character", probability="numeric", endProbs="data.frame"))

  q <- new("EventPattern", sequence=elist, probability=1, endProbs=data.frame(None=0))
  print("Not returning 'q'")
  # Further code that should make use of q
}

于 2019-01-14T15:59:11.127 回答