I am cutting my teeth on exposing an R machine learning classification model as a web service using Plumber and Swagger. I have trained a model and saved it as "j48.model.rda". I am now loading the model in a file called "myFile.R". This file contains the following R code:
library(rJava)
jsDirData <- "C:/AA Research/Playpen/Data"
setwd(jsDirData)
#Load the saved model
load(file="j48.model.rda", envir = parent.frame(), verbose = FALSE)
#' @param naasra90th:numeric The 90th Percentile Naasra value for the segment
#' @param rut90th:numeric The 90th Percentile Rut Depth for the segment
#' @param surfAge:numeric The surface age of the segment, in years (fractions are OK)
#' @param rutRate90th:numeric The rut rate on the 90th Percentile Rut depth (mm/year)
#' @param maintCount:int The number of maintenance acions
#' @get /getTreatment
#' @html
#' @response 200 Returns the treatment class (ThinAC or none) prediction from the j48 model;
#' @default Bonk!
getTreatment <- function(naasra90th, rut90th, surfAge, rutRate90th, maintCount) {
xVals <- list(naasra90th = naasra90th, rut90th = rut90th, surfAge = surfAge,
rutRate90th = rutRate90th, maintCount = maintCount)
nData <- as.data.frame(xVals)
pred <- predict(j48.model,newdata = nData)
res <- as.character(pred)
return(res)
}
t <- getTreatment(50,8.8,5,0.3,0) #should return "none"
t #"none" Correct!
t <- getTreatment(888,888,888,888,888) #should return "ThinAC"
t #"ThinAC" Correct!
As you can see from the last lines, when I call the function directly in R-Studio, it gives the correct classification. But now I try to call this method via a Plumber/Swagger web service, as follows:
library(plumber)
jsDirData <- "C:/AA Research/Playpen/Data"
setwd(jsDirData)
r <- plumb("myfile.R")
r$run(port=8000)
When I run this code, Swagger opens a browser and shows the API correctly. However, when I use the "Try It" button to test the API, then it always shows the result as "none", no matter what parameters I pass into the method. For example, if I enter the same set of parameters as in the second method call above (i.e. 888 for all parameters), then it returns "none" when it should return "ThinAC".
What am I doing wrong?