在水管工中使用“新”

时间:2019-01-14 15:08:23

标签: r plumber

我的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,但我不知道如何解决。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我以前没有使用过setClassnew。但去年我曾与水管工合作。我发现使用您的示例正在尝试返回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
}