Logo ROOT   6.10/09
Reference Guide
DataFrame.C
Go to the documentation of this file.
1 // Author: Omar Zapata
2 #include<TRInterface.h>
3 #include<TRDataFrame.h>
4 #include<vector>
5 #include<array>
6 
7 using namespace ROOT::R;
8 
9 void DataFrame() {
10 //creating variables
11  TVectorD v(3);
12  std::vector<Double_t> sv(3);
13 //std::array<Int_t,3> a{ {1,2,3} };
14 
15 //assinging values
16  v[0]=1;
17  v[1]=2;
18  v[2]=3;
19 
20  sv[0]=0.101;
21  sv[1]=0.202;
22  sv[2]=0.303;
23 
24 
26 // r.SetVerbose(kTRUE);
27  std::list<std::string> names;
28  names.push_back("v1");
29  names.push_back("v2");
30  names.push_back("v3");
31 
32  TRDataFrame df(Label["var1"]=v,Label["var2"]=sv,Label["var3"]=sv,Label["strings"]=names);
33  TRDataFrame df2;
34 
35 
36  r["df"]<<df;
37 
38 //printting results
39  std::cout<<"-----------Printing Results---------\n";
40  r<<"print(df)";
41 
42  std::cout<<"------------------------------------\n";
43  df["var1"]>>sv;
44 
45  r["v"]<<sv;
46  r<<"print(v)";
47  df["var3"]<<sv;
48 
49  df["var4"]<<sv;
50  df["var5"]=names;
51  r["df"]<<df;
52  std::cout<<"------------------------------------\n";
53  r<<"print(df)";
54 
55  //tests of operators between dataframe and r interface object
56  std::cout<<"------------------------------------\n";
57  r<<"df2<-data.frame(v1=c(1,2,3),v2=c('a','b','c'),v3=c(3,2,1))";
58  r["df2"]>>df2;
59  r["v2"]<<df2["v2"];
60  r<<"print(v2)";
61  r["v3"]<<df2["v3"];
62  r<<"print(v3)";
63 
64  //tests between dataframes operator
65  std::cout<<"------------------------------------\n";
66  df2["v4"]<<df2["v3"];
67  df2["v2"]<<df2["v3"];
68  r["df2"]<<df2;
69  r<<"print(df2)";
70 
71  //the next line is not working
72  df2["v5"]=df2["v1"];
73  df2["v1"]=df2["v3"];
74  df2["v6"]<<df2["v5"];
75  r["df2"]<<df2;
76  r<<"print(df2)";
77 
78  // the next line donk work, the operator >> is not supported between Bindings(FIXED NOW))
79  df2["v6"]>>df2["v1"];
80 
81  //basic methods
82  std::cout<<"------------------------------------\n";
83  std::cout<<"nrows = "<<df2.GetNrows()<<std::endl;
84  std::cout<<"ncols = "<<df2.GetNcols()<<std::endl;
85 
86 
87  r["v5"]<<df2["v5"];
88  r<<"print(v5)";
89  df2["v5"]>>df["var1"];
90  r["v5"]<<df2["v5"];
91  r<<"print(v5)";
92 
93 
94  std::cout<<"-----Cast---"<<std::endl;
96  r["df=data.frame(v1=c(0.1,0.2,0.3),v2=c(0.4,0.5,0.6))"]>>dfm;
97  dfm.Print();
98 
99  dfm.Print("v1");
100 
101  TMatrixT<Float_t> m=dfm.AsMatrix<Float_t>();
102  m.Print();
103 
104  //Error Handling
105 /* std::cout<<"------------------------------------\n";
106  try{
107  r["qwe"]<<df2["qwe"];
108  }
109  catch(Rcpp::index_out_of_bounds& __ex__){
110  ::Error("operator=", "%s",__ex__.what());
111  forward_exception_to_r( __ex__ ) ;
112  }
113  catch(...){::Error("operator=", "Can not assign in v5");}
114 */
115 }
float Float_t
Definition: RtypesCore.h:53
void Print(const Char_t *label="")
Method to print the dataframe in stdout or a column given the label.
Definition: TRDataFrame.h:428
TMatrixT.
Definition: TMatrixDfwd.h:22
namespace associated R package for ROOT.
Definition: RExports.h:56
TRandom2 r(17)
SVector< double, 2 > v
Definition: Dict.h:5
TMarker * m
Definition: textangle.C:8
void DataFrame()
Definition: DataFrame.C:9
static TRInterface & Instance()
static method to get an TRInterface instance reference
void Print(Option_t *name="") const
Print the matrix as a table of elements.
Rcpp::internal::NamedPlaceHolder Label
Definition: RExports.cxx:14
This is a class to create DataFrames from ROOT to R
Definition: TRDataFrame.h:177