Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
calculaterexpression [2016/11/16 15:27] romulo |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ======= Calculate R Expression ======= | ||
| - | The functor 'Calculate R Expression' allows Dinamica EGO to call R externally and process the script outputs as if they were | ||
| - | part Dinamica itself. | ||
| - | ====== Setup ====== | ||
| - | |||
| - | There are two ways to setup the 'Calculate R Expression' functor:\\ | ||
| - | \\ | ||
| - | === R Plugin === | ||
| - | There is a plugin that contains everything needed to wrap R scripts inside Dinamica EGO, just extract [[http://csr.ufmg.br/~romulo/R.7z|this file]] | ||
| - | to your Dinamica EGO installation directory (usually c:\Program Files\Dinamica EGO) and you will be good to go.\\ | ||
| - | \\ | ||
| - | === Local R Installation === | ||
| - | Users can specify a local R installation to be used for execution, the pre requisites are:\\ | ||
| - | - R needs to be at least at the **3.3.1** version. | ||
| - | - **Rscript.exe** must be valid and located at the 'installation directory\\bin' subdirectory. | ||
| - | - [[externalcommunication|Dinamica package]] for R must be installed and at the latest version. | ||
| - | |||
| - | To specify your custom R installation, go to Tools -> Options -> External Comm tab and select 'Use alternative R installation for Calculate R Expression': | ||
| - | {{:dinamica_options_use_alternative_r_install.png?nolink|}} | ||
| - | \\ | ||
| - | \\ | ||
| - | ====== Usage ====== | ||
| - | The functor is available under the 'External Communication' tab at the library.\\ | ||
| - | \\ | ||
| - | \\ | ||
| - | === Inputs === | ||
| - | 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: | ||
| - | * Number Table (will be passed as a list containing two named members, 'Keys' and 'Values' - only **LookupTable**'s are supported at this time) | ||
| - | * Number Value | ||
| - | * Number String | ||
| - | |||
| - | 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:\\ | ||
| - | <code> | ||
| - | # Will store the 5th key from the passed LookupTable in the variable 'testing' | ||
| - | testing <- t1$Keys[ 5 ]; | ||
| - | |||
| - | # Store the second passed String. | ||
| - | secondString <- s2; | ||
| - | |||
| - | # Store the 10th passed Value. | ||
| - | tempNumber <- v10; | ||
| - | </code> | ||
| - | \\ | ||
| - | \\ | ||
| - | === Outputs === | ||
| - | 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 ) | | ||
| - | In the examples above, data was constructed inside the calling functions. You can also specify variables as parameters. | ||
| - | <note important>All output functions require a identifier as the first parameter, that's the name Dinamica uses for retrieving the correct data.</note> | ||
| - | \\ | ||
| - | Keep in mind:\\ | ||
| - | * You can pass any kind of numeric value on the outputDouble function. | ||
| - | * Collections of numbers are valid Number Vector's. | ||
| - | * A LookupTable requires **2** number vectors, one for the Keys and another for the Values. Both vectors must have the same number of elements. | ||
| - | \\ | ||
| - | \\ | ||
| - | ====== Example Scripts ====== | ||
| - | \\ | ||
| - | === Multiply each value of the passed LookupTable by itself. === | ||
| - | {{ ::ex1_calculate_r.png?nolink |}} | ||
| - | Expression: | ||
| - | <code> | ||
| - | for ( i in 1:length( t1$Values ) ) { | ||
| - | tempValue <- t1$Values[ i ]; | ||
| - | t1$Values[ i ] = tempValue * tempValue; | ||
| - | } | ||
| - | |||
| - | outputLookupTable( "poweredTable", t1$Keys, t1$Values ); | ||
| - | </code> | ||
| - | \\ | ||
| - | \\ | ||
| - | === Extract the mean of passed LookupTable values. === | ||
| - | {{ ::ex2_calculate_r.png?nolink |}} | ||
| - | Expression: | ||
| - | <code> | ||
| - | tableMean <- mean( t1$Values ); | ||
| - | print( paste( "Mean is", tableMean ) ); | ||
| - | outputDouble( "mean", tableMean ); | ||
| - | </code> | ||
| - | Noticed the **print()** statement? Dinamica EGO's Message Log will show output messages from the R script. | ||
| - | \\ | ||
| - | \\ | ||
| - | === Plot passed LookupTable to an image on the path specified by passed String === | ||
| - | {{ ::ex3_calculate_r.png?nolink |}} | ||
| - | Expression: | ||
| - | <code> | ||
| - | outputFile <- s1; | ||
| - | print( paste( "Plotting to", outputFile ) ); | ||
| - | jpeg( outputFile, quality=100 ); | ||
| - | plot( t1 ); | ||
| - | dev.off(); | ||
| - | </code> | ||