none
R Web service returning NULL values as output RRS feed

  • Question

  • Hi Team

    I am creating a web service using R studio(R server).Below is the code that i am using to create a web service.

    The model works well locally for the test data.But i am getting Null as the output for the web service published in ML server.

    The output of the model will be 1s and 0s.

    Below is the code sample used.

    webservice<- paste0("webservice", round(as.numeric(Sys.time()), 0))

    #SVM_fit is the model that is created and its predicting the test data locally

    #Function created
    churnpredictorsvm<-function(Test){
      predict(SVM_fit,newdata = Test[,-48])
      }

    api <- publishService(
      webservice,
      code = predictorsvm,
      model = SVM_fit,
      inputs = list(Test = "data.frame"),
      outputs = list(answer = "numeric"),
      v = "v1.0.0"
    )

    result <- api$churnpredictorsvm(head(Test,2))

    print(result$output("answer"))  ---This is resulting Null as output.
    I tried for xgboost model as well and i got the same issue.

    #Error message:
    result$errorMessage
    [1] "Error in UseMethod(\"predict\") : \n  no applicable method for 'predict' applied to an object of class \"c('svm.formula', 'svm')\"\nanswer could not be returned.\n"

    Please help at the earliest.Thanks in advance.


    shankar

    Tuesday, December 4, 2018 7:12 AM

All replies

  • Hi Shankar,

    How did you create your SVM model?  What R function did you use?

    The error message seems to indicate that 'predict()' does not work on the 'SVM_fit' object you created...

    Did you use the 'svm()' function from the R package 'e1071', or a different R package/function to create the SVM model fit object?

    Wednesday, December 5, 2018 5:29 PM
  • Does the predict() function return a factor type in your code?  That could be the problem, as we currently don't support factor types in the publishService() function....
    Wednesday, December 5, 2018 5:45 PM
  • Hi Steve

    Thanks for your response. I have used the package e1071. What is the solution for this problem.How can we solve this .

    Regards

    Shankar


    shankar

    Thursday, December 6, 2018 5:30 AM
  • Hi Steve,

    the return data type of SVM predict is a factor.What is the equivalent data type that can we use in the function so that the publishwebservice accepts it.

    Thanks

    Shankar


    shankar

    Thursday, December 6, 2018 6:17 AM
  • Hi Shankar,

    Right now we don't support factors in the publishservice web function.

    I will log a bug to fix this and support them in the future.

    Unfortunately, I can't think of a  workaround for the issue.

    Tuesday, December 11, 2018 7:14 PM
  • Hi Steve

    We are getting the same issue for xgboost model where the output of the predict is numeric data type.

    Please explain.

    Thanks


    shankar

    Friday, December 14, 2018 8:00 AM
  • If the output return type is numeric it should work fine with xgboost model...

    Can you paste your R code below?

    Thanks.

    Monday, December 17, 2018 7:28 PM
  • Hi Steve 

    PFB code(xgboost model) and function used to create the web service.

    Function for predict:

    predictorfunction<-function(dtest){
      dtest<-data.matrix(dtest)
      pred<-predict(xgbfit,newdata=dtest)
     }

    After doing the remote login,we create the web service and api:

    churnwebservic2 <- paste0("webservice", round(as.numeric(Sys.time()), 0))

    api <- publishService(
      churnwebservic2,
      code = predictorfunction,
      model = xgbfit,
      inputs = list(dtest = "data.frame"),
      outputs = list(answer = "numeric"),
      v = "v4.0.0"
    )

    print(result$output("answer")) ---This is returning null.

    When i run the function and try it with local test data, i am able to get the output.

    print(predictorfunction(head(dtest,20)))

    Please help!!.

    Regards

    Shankar


    shankar

    Tuesday, December 18, 2018 11:00 AM
  • Hi Shankar,

    I have also tried something similar. Try loading the library 'e1071' or the libraries of the models used (xgboost,svm) inside predictorfunction.

    This error should be fixed by this:

    result$errorMessage
    [1] "Error in UseMethod(\"predict\") : \n  no applicable method for 'predict' applied to an object of class \"c('svm.formula', 'svm')\"\nanswer could not be returned.\n"

    Regards,

    Veda

    Thursday, February 14, 2019 2:41 AM