This project is read-only.

R.net reading tree

Mar 3, 2014 at 6:52 AM
Edited Mar 3, 2014 at 6:53 AM
how could I read tree values from R into .net? I tried this approach but it returns me null
engine.Evaluate("data=read.spss(file='C:/dev/Anomaly/Vorselaar2011D.sav',to.data.frame=TRUE));
engine.Evaluate("data$timestamp<-as.Date(as.POSIXct(data$timestamp, origin = \"1582-10-14\",tz = \"GMT\"))");
var data =engine.Evaluate("treeElect<tree(Electricity_kWh~Water_lh+Day_of_Week+TempOut_mean+GasM3h, data=data)");
var dataset = engine.Evaluate("treeElect").AsDataFrame();
when I do it in R console I can see this output
> treeElect
node), split, n, deviance, yval
      * denotes terminal node


 1) root 365 1006000 146.40  
   2) Water_lh < 214 123   84580  85.46  
     4) GasM3h < 64 94   10540  77.95 *
     5) GasM3h > 64 29   51550 109.80  
      10) Day_of_Week < 5 5    9028 186.30 *
      11) Day_of_Week > 5 24    7148  93.86 *
   3) Water_lh > 214 242  232100 177.40  
     6) Water_lh < 582.25 90   44470 154.70  
      12) GasM3h < 87.75 75   20880 149.00 *
      13) GasM3h > 87.75 15    8857 183.30 *
     7) Water_lh > 582.25 152  113800 190.90  
      14) GasM3h < 54.4 72   29300 175.30 *
      15) GasM3h > 54.4 80   51360 204.90  
        30) Day_of_Week < 1.5 22    5340 231.50 *
        31) Day_of_Week > 1.5 58   24540 194.80 *
how can I get this in .net ?
Mar 3, 2014 at 3:31 PM
That looks like you are printing to the standard out, so you will have to setup a callback for where you want that information displayed.
Mar 3, 2014 at 4:47 PM
ok , and can you tell me how can I do it ? or put here link to it ?
Mar 3, 2014 at 10:21 PM
Not quite sure what you are after exactly, so there are a couple of options covered in the following sample code. Note that treeElect in your code is expected to be a list, not a data frame.
      private static void ReproTreeEnquiry(REngine e)
      {
         e.Evaluate("library(tree)");
         var irtr = e.Evaluate("ir.tr <- tree(Species ~., iris)");
         // the following will print a human readable tree to the console output
         e.Evaluate("print(ir.tr)");
         var aList = irtr.AsList(); // May work only with the latest dev code
         // for R.NET 1.5.5 you may need to do instead:
         aList = e.Evaluate("as.list(tree(Species ~., iris))").AsList();
         var theDataFrame = aList[0].AsDataFrame();
         // Further processing of theDataFrame, etc.
      }
Mar 4, 2014 at 7:26 AM
I mean how can I get in c# this text which appears in console which is taken from R console.