Logo ROOT  
Reference Guide
example.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_r
3/// \notebook -nodraw
4/// Simple example on how to use ROOT-R interface.
5///
6/// \macro_code
7///
8/// \author
9
10#include "TMath.h"
11#include "Math/PdfFunc.h"
12#include "TMatrixD.h"
13#include "TError.h"
14#include <array>
15#include <vector>
16#include "TRInterface.h"
17
18void example() {
20 // print R version
21 r.Execute("print(version$version.string)");
22
23 // compute standard deviation of 1000 vector normal numbers
24
25 double std_dev_r = r.Eval("sd(rnorm(10000))");
26 std::vector<double> v = r.Eval("rnorm(10000)");
27 double std_dev_root = TMath::StdDev(v.begin(),v.end());
28 std::cout << "standard deviation from R = " << std_dev_r << std::endl;
29 std::cout << "standard deviation from ROOT = " << std_dev_root << std::endl;
30 if (!TMath::AreEqualAbs(std_dev_r,std_dev_root,0.1))
31 Error("ROOT-R-Example","Different std-dev found");
32
33 // use << to execute the R command instead of Execute
34 r << "mat<-matrix(c(1,2,3,4,5,6),2,3,byrow=TRUE)";
35 TMatrixD m = r["mat"];
36 std::array<double,6> a = r.Eval("seq(1:6)");
37 TMatrixD m2(2,3,a.data());
38
39 if (!(m==m2)) {
40 Error("ROOT-R-Example","Different matrix found");
41 m.Print();
42 m2.Print();
43 }
44
45 // example on how to pass ROOT objects to R
46 std::vector<double> v_root{1,2,3,4,5,6,7,8};
47 r["v"] = v_root;
48 r << "v2<-seq(1:8)";
49 bool isEqual = r.Eval("prod(v==v2)");
50 if (!isEqual) {
51 Error("ROOT-R-Example","Different vector created");
52 r << "print(v)";
53 r << "print(v2)";
54 }
55
56 // example on how to pass functions to R
57
58 r["gaus_pdf"] = ROOT::Math::normal_pdf;
59
60 r << "y<-gaus_pdf(0,1,1)";
61 double value_r = r["y"];
62 double value_root = ROOT::Math::normal_pdf(0,1,1);
63 std::cout << "Function gaussian(0,1,1) evaluated in R = " << value_r << std::endl;
64 std::cout << "Function gaussian(0,1,1) evaluated in ROOT = " << value_root << std::endl;
65 if (value_r != value_root)
66 Error("ROOT-R-Example","Different function value found in r = %f and ROOT = %f", value_r, value_root);
67}
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
ROOT R was implemented using the R Project library and the modules Rcpp and RInside
Definition: TRInterface.h:136
static TRInterface & Instance()
static method to get an TRInterface instance reference
void Print(Option_t *option="") const override
Dump this marker with its attributes.
Definition: TMarker.cxx:334
double normal_pdf(double x, double sigma=1, double x0=0)
Probability density function of the normal (Gaussian) distribution.
static constexpr double m2
Bool_t AreEqualAbs(Double_t af, Double_t bf, Double_t epsilon)
Comparing floating points.
Definition: TMath.h:418
Double_t StdDev(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:525
TMarker m
Definition: textangle.C:8
TArc a
Definition: textangle.C:12