0

I have a simple function in my Plumber API that looks like the following:

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
}

I start Plumber (locally) and point it to the script containing the api (the above) as:

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

And call the function on the address (using PostMan):

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

But what I end up getting is 'An Exception Occurred' with the R console saying that:

<simpleError: No method for S4 class:EventPattern>

I realize that the error suggests that a method (a generic) is required, but when I type:

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

locally on my machine (in the R console) it works fine. It suggests to me that something is not fully loaded or available to Plumber, but I have no idea how to fix it. Any ideas?

user1938803
  • 189
  • 2
  • 10

1 Answers1

1

I have not used setClass and new before. But I've worked with plumber last year. I found using your example that it is trying to return q, and throwing an error because of it.

Adding a print statement seems to prevent an error:

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
}

MrH
  • 101
  • 6