ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
example.C
Go to the documentation of this file.
1 #include "TRInterface.h"
2 
3 // simple example on how to use ROOT-R interface
4 
5 void example() {
6 
8  // print R version
9  r.Execute("print(version$version.string)");
10 
11  // compute standard deviation of 1000 vector normal numbers
12 
13  double std_dev_r = r.Eval("sd(rnorm(1000))");
14  std::vector<double> v = r.Eval("rnorm(1000)");
15  double std_dev_root = TMath::StdDev(v.begin(),v.end());
16  std::cout << "standard deviation from R = " << std_dev_r << std::endl;
17  std::cout << "standard deviation from ROOT = " << std_dev_root << std::endl;
18  if (!TMath::AreEqualAbs(std_dev_r,std_dev_root,0.1))
19  Error("ROOT-R-Example","Different std-dev found");
20 
21  // use << to execute the R command instead of Execute
22  r << "mat<-matrix(c(1,2,3,4,5,6),2,3,byrow=TRUE)";
23  TMatrixD m = r["mat"];
24  std::array<double,6> a = r.Eval("seq(1:6)");
25  TMatrixD m2(2,3,a.data());
26 
27  if (!(m==m2)) {
28  Error("ROOT-R-Example","Different matrix found");
29  m.Print();
30  m2.Print();
31  }
32 
33  // example on how to pass ROOT objects to R
34  std::vector<double> v_root{1,2,3,4,5,6,7,8};
35  r["v"] = v_root;
36  r << "v2<-seq(1:8)";
37  bool isEqual = r.Eval("prod(v==v2)");
38  if (!isEqual) {
39  Error("ROOT-R-Example","Different vector created");
40  r << "print(v)";
41  r << "print(v2)";
42  }
43 
44  // example on how to pass functions to R
45 
46  r["gaus_pdf"] = ROOT::Math::normal_pdf;
47 
48  r << "y<-gaus_pdf(0,1,1)";
49  double value_r = r["y"];
50  double value_root = ROOT::Math::normal_pdf(0,1,1);
51  std::cout << "Function gaussian(0,1,1) evaluated in R = " << value_r << std::endl;
52  std::cout << "Function gaussian(0,1,1) evaluated in ROOT = " << value_root << std::endl;
53  if (value_r != value_root)
54  Error("ROOT-R-Example","Different function value forund in r = %f and ROOT = %f", value_r, value_root);
55 
56 
57 }
void Execute(const TString &code)
Method to eval R code.
Definition: TRInterface.cxx:77
TArc * a
Definition: textangle.C:12
double normal_pdf(double x, double sigma=1, double x0=0)
Probability density function of the normal (Gaussian) distribution.
void Error(const char *location, const char *msgfmt,...)
Bool_t AreEqualAbs(Double_t af, Double_t bf, Double_t epsilon)
Definition: TMath.h:192
void Print(Option_t *name="") const
Print the matrix as a table of elements.
void example()
Definition: example.C:26
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
TMarker * m
Definition: textangle.C:8
Double_t StdDev(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:311
static TRInterface & Instance()
static method to get an TRInterface instance reference
Int_t Eval(const TString &code, TRObject &ans)
Method to eval R code and you get the result in a reference to TRObject.
Definition: TRInterface.cxx:58