Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

⚠ This How to documents an experimental API subject to changes or deletion in future versions of MO API. Use it at your own risks. ⚠

Exactly as Model Types, Model Objects evaluations and calculations do support the evaluation of R code.

Prerequisites

A R server instance must be configured on the cluster running your Pricefx partition.

You can create an Helpdesk ticket for that, or if you have the sufficient rights, you can directly modify the kubernetes configuration of the corresponding cluster like it has been done here: https://gitlab.pricefx.eu/dev-ops/infra/core-qa-1/core_qa_1_eu-west-1_app_clusters/-/merge_requests/419.

Logic

Once the R server is configured, you can interact with it by using the RContext object reference obtained using model.rContext().

Here is an example that computes a Gaussian curve:

def min = 0
def max = 10
def mean = 5
def std = 2

// 1. get the reference to the RContext object
def rCtx = model.rContext() 

// 2. assign variables to the RContext object
rCtx.assign("fmin", [min] as double[])
rCtx.assign("fmean", [mean] as double[])
rCtx.assign("fmax", [max] as double[])
rCtx.assign("fstd", [std] as double[])

// 3. evaluate some R expressions
rCtx.eval("x <- seq(fmin, fmax, by = .1)")
rCtx.eval("y <- dnorm(x, mean = fmean, sd = fstd)")

// 4. fetch the result of evaluated expressions
def x = rCtx.get("x").asDoubles() as List<BigDecimal>
def y = rCtx.get("y").asDoubles() as List<BigDecimal>

// 5. returns a nice Guaussion curve chart
return api.buildHighchart([
        title : [text: "A Gaussian from $min to $max with mean=$mean and std=$std"],
        series: [
                [data: [x, y].transpose()]
        ]
])

See the API documentation for a complete overview of the RContext.

  • No labels