Evaluate R Code

This article documents an experimental API which is 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 support the evaluation of R code.

Prerequisites

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

You can create an internal Pricefx helpdesk ticket for that, or if you have sufficient rights, you can directly modify the Kubernetes configuration of the corresponding cluster like 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.

Found an issue in documentation? Write to us.