none
R function that depends on other function RRS feed

  • Question

  • Hi,

    how can I easily deploy a R function that depends on another function...like

    functionA <- function(x,y){

         output <- functionB(x)

    }

    Monday, April 23, 2018 4:58 PM

All replies

  • Hello,

    Not all publish `code=fn` needs to be a simple function handle. For situations like this you can:::

    1. Put all your `code` into a single R script and treat it like a "block of code" that invokes `functionB`

    Example:

    ```R
    # -- my-code-block.R --
    functionB <- function(x) {
      x
    }

    # myInput is your input (you can have 0 or more inputs)
    # myOutput is your output (you can have 0 or more outputs)
    myOutput <- functionB(myInput)
    ```

    Now, from the command-line or a separate script deploy it by pointing to your script

    via the `code='my-code-block.R`:::

    ```R
    api <- publishService(
      name = 'test-code-block-by-file-path',
      code = 'my-code-block.R',   # defaults to cwd otherwise provide fullpath to script
      inputs = list(myInput='integer'),
      outouts = list(myOutput='integer'),
      v = '1.0.0',
      alias = 'functionA'         # Optional, defaults to `consume` however you can name it
    )

    myInput <- 5
    response <- api$functionA(myInput) # notice same name as `alias` above (e.g. treat like a simple RPC)
    print(response$output('myOutput')) # prints 5

    ```

    2. OR  put code as a character code block:::

    Example:

    ```R

    codeBlock<-"All your code in a character code block\n"  # do it inline or read it from file (1. above is more useful)

    api <- publishService(
      name = 'test-code-block-by-file-path',
      code = codeBlock,     defaults to cwd otherwise provide fullpath to script
      inputs = list(myInput='integer'),
      outouts = list(myOutput='integer'),
      v = '1.0.0',
      alias = 'functionA'         # Optional, defaults to `consume` however you can name it
    )

    ```

    3. OR put all your named objects, functions, ect... in an .Rdata file and asign it to `model=my-objs.RData`:::

    Example::

    ```R

    # -- my-objs.R

    ```

    functionB <- function(x) {

       x

    }

    ```R

    functionA <- functionA(myInput) {

       myOutput <- functionB(myInput)

    }

    api <- publishService(
      name = 'test-code-by-named-objs-in-rdata-file',
      code = functionA,  

      model='my-objs.RData', #  defaults to cwd otherwise provide fullpath to .Rdata

      inputs = list(myInput='integer'),
      outouts = list(myOutput='integer'),
      v = '1.0.0'
    )

    ```

    I would vote for (1) as it allows you to keep your prediction/service code in a separate .R file 

    and your deployment code some place else based on your workflow. It also lets you test your script in isolation.

    Hope this helps?

    Regards,

    Sean



    Tuesday, April 24, 2018 2:36 AM