The functor 'Calculate R Expression' allows Dinamica EGO to call R externally and process the script outputs as if they were part of Dinamica itself. For an overview of how Dinamica and R can be linked together check the documentation about R Coupling.
Name | Type | Description |
---|---|---|
Expression | String Type | The expression that will run on R. |
Treat Warning As Errors | Boolean Value Type | Warnings on R script will be considered errors. |
None.
Name | Type | Description |
---|---|---|
result | Struct Type | A struct containing the output values generated by the expression. |
There are two ways to setup the 'Calculate R Expression' functor:
Simply download and install the Dinamica EGO Enhancement Plugin. It contains everything you need to run R scripts inside Dinamica EGO.
Additionally, users can specify and use their own local R installation for execution. The pre-requisites are:
Rscript.exe
must be valid and located at the <installation folder>/bin
sub-folder.To specify your custom R installation, go to Tools –> Options –> Integration tab and select Use alternative R installation for Calculate R Expression:
The functor is available under the Integration tab at the library.
To pass input parameters from Dinamica EGO to R, hooks must be placed to collect data. You can use the Create a hook button on the selected functor bar or drag the hooks individually. The valid hook types are:
To access the passed inputs on your R script, follow Dinamica EGO naming conventions and use t[0-99]
, v[0-99]
, s[0-99]
for each type respectively. For example:
# Will store the 5th key from the passed Lookup Table in the variable 'testing' testing <- t1$Keys[ 5 ]; # Store the second passed String. secondString <- s2; # Store the 10th passed Value. tempNumber <- v10;
To output data back to Dinamica EGO, the user must call a set of functions designed for this functionality:
Function name | Output Data Type | Usage Example |
---|---|---|
outputDouble() | Real | outputDouble( “myDouble”, 3.14 ) |
outputNumberVector() | Tuple | outputNumberVector( “myTuple”, c(1:10) ) |
outputString() | String | outputString( “myString”, “This is a string” ) |
outputLookupTable() | LookupTable | outputLookupTable( “myLUT”, c(1:10), c(1:10) * 10 ) |
outputTable() | Table | outputTable( “myTable”, data.frame(State = c(“Massachusetts”, “Massachusetts”), City = c(“Boston”, “Chelsea”), Population = c(667137, 39398), stringsAsFactors = FALSE), 2 ) |
In the examples above, data was constructed inside the calling functions. You can also specify variables as parameters.
Keep in mind:
outputDouble
function.outputTable
function requires a table, that can be constructed with data.frame R function. There is another parameter that is optional with default value 1, and it means how many Key columns the table has, from the leftmost column.Tables or Lookup Tables can be transferred to R using the Number Table functor. When dealing with each of these types extra caution must be taken:
Lookup Tables are transferred to R as a List with two columns, “Key” and “Value”.
Each column can be individually accessed by using the $
operator, for example:
t1$Key[ 1 ]
will access the first key of the Lookup Table t1
. t2$Value[ 2 ]
will access the second value of the Lookup Table t2
Tables are transferred to R as a DataFrame type variable. Each column can be individually accessed by using the $
operator, just like Lookup Tables. Please check this page for more information on how to deal with Tables. The rules for them in Calculate R Expression are the same as sending and receiving through External Communication.
Expression:
for ( i in 1:length( t1$Value ) ) { tempValue <- t1$Value[ i ]; t1$Value[ i ] = tempValue * tempValue; } outputLookupTable( "poweredTable", t1$Key, t1$Value );
Expression:
tableMean <- mean( t1$Value ); print( paste( "Mean is", tableMean ) ); outputDouble( "mean", tableMean );
Noticed the print()
statement? Dinamica EGO's Message Log will show output messages from the R script (as Result's).
Expression:
outputFile <- s1; print( paste( "Plotting to", outputFile ) ); jpeg( outputFile, quality=100 ); plot( t1 ); dev.off();
CalculateRExpression