Data types changed??- From Numeric to Integer RRS feed

  • Question

  • I have the following issue with data types on MLServer 9.3

    I'm trying to deploy a web service that receives some imputs as numeric types and then do some operations with it.

    In order to simplify this post, i'm going to show a dummie example that shows the issue i'm having.

    My web service imputs are defined as:

    multiply <- function(n1,n2){
      n1 = number1
      n2 = number2
      print(paste("TypeOf N1:",typeof(n1)))
      print(paste("TypeOf N2:",typeof(n2)))

    Print statements are included to evaluate the console output after service execution


    ###Deploy Web Service
      api <- publishService(
          code = multiply,
          inputs = list(number1 = "numeric",number2 = "numeric")
          outputs = list(answer = "numeric"),
          v = "v1",
          alias = "consumeService"
    ###Consume Web Service
    api <- getService("multiply2numbers")
    result <- api$consumeService(
      number1 = 4,
      number2 = 8

    ###Checking results
    print(paste("Console Output:",result$consoleOutput))

    This print statements give the following messages:

    [1] "Output: 32"
    [1] "Console Output: [1] \"TypeOf N1: integer\"\r\n[1] \"TypeOf N2: integer\"\r\n"

    Now, n1 and n2 are "integer", but they were defined as "numeric".

    What's happen behind web service execution that original data type is changed to an integer type?

    Thursday, May 17, 2018 3:01 PM

All replies

  • The R function typeof has a bias to give you the type at maximum depth, for example.

    The R function class has a bias to give you the type of container or structure encapsulating your types, for example.

    You will get "numeric" if you use class(n1) rather than typeof(n1) 

    you can read more about it here :

    Thursday, May 17, 2018 7:11 PM
  • Thanks for your repply!

    I see what you say.

    I test it in my local environment and that's it.

    typeof(123)  --> "double"

    class(123) -----> "numeric"

    Anyway, in my previous code, i replaced typeof() with class() in that way:

    multiply <- function(n1,n2){
      n1 = number1
      n2 = number2
      print(paste("Class N1:",class(n1)))
      print(paste("Class N2:",class(n2)))

    And after execute the consumeservice() method, the console output has the following message:

    Show in New WindowClear OutputExpand/Collapse Output
    [1] "Console Output: [1] \"Class N1: integer\"\r\n[1] \"Class N2: integer\"\r\n"

    what's happening here?

    Friday, May 18, 2018 2:05 PM