Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
calculate_r_expression [2017/07/25 22:58]
admin [Description]
calculate_r_expression [2018/09/18 14:14] (current)
francisco [R Script Outputs]
Line 85: Line 85:
 | outputString() ​ | String ​ | outputString( "​myString",​ "This is a string"​ )  | | outputString() ​ | String ​ | outputString( "​myString",​ "This is a string"​ )  |
 | outputLookupTable() ​ | LookupTable ​ | outputLookupTable( "​myLUT",​ c(1:10), c(1:10) * 10 )  | | outputLookupTable() ​ | LookupTable ​ | outputLookupTable( "​myLUT",​ c(1:10), c(1:10) * 10 )  |
-| outputTable() ​ | Table  | outputTable( "​myTable", ​anyTable)  |+| 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. In the examples above, data was constructed inside the calling functions. You can also specify variables as parameters.
  
-<note important>​All output functions require an identifier as the first parameter, that's the name Dinamica uses for retrieving the correct data.</​note>​+<note important>​All output functions require an identifier as the first parameter, that's the name Dinamica uses for retrieving the correct data (to put into the struct output).</​note>​
  
 Keep in mind: Keep in mind:
Line 97: Line 97:
   * Collections of numbers are valid number vector'​s.   * Collections of numbers are valid number vector'​s.
  
-  * A [[Lookup Table Type|Lookup Table]] requires //2// number vectors, one for the "Keys" and another for the "Values". Both vectors must have the same number of elements.+  * A [[Lookup Table Type|Lookup Table]] requires //2// number vectors, one for the "Key" and another for the "Value". Both vectors must have the same number of elements. 
 + 
 +  * The ''​outputTable''​ function requires a table, that can be constructed with [[http://​www.r-tutor.com/​r-introduction/​data-frame | 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. 
 +<note important>​Notice the use of the stringAsFactors = FALSE flag. This flag prevents R from converting the passed strings to Factors(numbers).</​note>​
  
 ==== Tables / Lookup Tables ==== ==== Tables / Lookup Tables ====
Line 105: Line 108:
 === Lookup Tables === === Lookup Tables ===
  
-Lookup Tables are transferred to R as a List with two columns, "Keys" and "Values".+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: Each column can be individually accessed by using the ''​$''​ operator, for example:
  
-  * ''​t1$Keys[ 1 ]''​ will access the first key of the Lookup Table ''​t1''​. ​+  * ''​t1$Key[ 1 ]''​ will access the first key of the Lookup Table ''​t1''​. ​
    
-  * ''​t2$Values[ 2 ]''​ will access the second value of the Lookup Table ''​t2''​+  * ''​t2$Value[ 2 ]''​ will access the second value of the Lookup Table ''​t2''​
  
 === Tables === === Tables ===
Line 125: Line 128:
 Expression: Expression:
 <code rsplus> <code rsplus>
-for ( i in 1:length( t1$Values ​) ) { +for ( i in 1:length( t1$Value ) ) { 
-  tempValue <- t1$Values[ i ]; +  tempValue <- t1$Value[ i ]; 
-  t1$Values[ i ] = tempValue * tempValue;+  t1$Value[ i ] = tempValue * tempValue;
 } }
  
-outputLookupTable( "​poweredTable",​ t1$Keys, t1$Values ​);+outputLookupTable( "​poweredTable",​ t1$Key, t1$Value );
 </​code>​ </​code>​
  
Line 139: Line 142:
 Expression: Expression:
 <code rsplus> <code rsplus>
-tableMean <- mean( t1$Values ​);+tableMean <- mean( t1$Value );
 print( paste( "Mean is", tableMean ) ); print( paste( "Mean is", tableMean ) );
 outputDouble( "​mean",​ tableMean ); outputDouble( "​mean",​ tableMean );
 </​code>​ </​code>​
  
-Noticed the ''​print()''​ statement? Dinamica EGO's Message Log will show output messages from the R script (as Result'​s).+Noticed the ''​print()''​ statement? Dinamica EGO's Message Log will show output messages from the R script (as Result'​s).<note warning>​If the Message Log level is Unconditional,​ the messages will not be printed.</​note>​
  
 ==== Plot passed Lookup Table to an image on the path specified by passed String ==== ==== Plot passed Lookup Table to an image on the path specified by passed String ====