Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TRInterface.h
Go to the documentation of this file.
1// @(#)root/r:$Id$
2// Author: Omar Zapata Omar.Zapata@cern.ch 29/05/2013
3
4/*************************************************************************
5 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_R_TRInterface
13#define ROOT_R_TRInterface
14
15#include <TRObject.h>
16
17#include <TRDataFrame.h>
18
19#include <TRFunctionExport.h>
20
21#include <TRFunctionImport.h>
22
23#include <TThread.h>
24
25/**
26 @namespace ROOT::R
27 namespace associated R package for ROOT.
28 */
29namespace ROOT {
30 namespace R {
31 /**
32 \class TRInterface
33 ROOT R was implemented using the
34 <A HREF="http://www.r-project.org/">R Project</A> library and the modules
35 <A HREF="http://cran.r-project.org/web/packages/Rcpp/index.html">Rcpp</A> and
36 <A HREF="http://cran.r-project.org/web/packages/RInside/index.html">RInside</A>
37 <h2>Users Guide </h2>
38 <a href="https://oproject.org/pages/ROOT%20R%20Users%20Guide"> https://oproject.org/pages/ROOT R Users Guide</a><br>
39
40 */
41
42
43 /**
44 <center><h2>TRInterface class</h2></center>
45
46 </p>
47 The TRInterface class lets you process R code from ROOT.<br>
48 You can call R libraries and their functions, plot results in R or ROOT,<br>
49 and use the power of ROOT and R at the same time.<br>
50 It also lets you pass scalars, vectors and matrices from ROOT to R<br>
51 and from R to ROOT using TRObject; but you can to use overloaded operators [],<< and >> <br>
52 to work with ROOTR like work with streams of data.<br>
53
54 TRInterface class can not be instantiated directly, but you can create objects using the static methods
55 TRInterface& Instance() and TRInterface* InstancePtr() to create your own objects.<br>
56 <br>
57 </p>
58 Show an example below:
59 Create an exponential fit, the idea is to create a set of numbers x,y with noise from ROOT,
60 pass them to R and fit the data to \f$ x^3 \f$, get the fitted coefficient(power) and plot the data,
61 the known function and the fitted function.
62 \code{.cpp}
63
64 TCanvas *c1 = new TCanvas("c1","Curve Fit",700,500);
65 c1->SetGrid();
66
67 // draw a frame for multiples graphs
68 TMultiGraph *mg = new TMultiGraph();
69
70 // create the first graph (points with gaussian noise)
71 const Int_t n = 24;
72 Double_t x[n] ;
73 Double_t y[n] ;
74 //Generate points along a X^3 with noise
75 TRandom rg;
76 rg.SetSeed(520);
77 for (Int_t i = 0; i < n; i++) {
78 x[i] = rg.Uniform(0, 1);
79 y[i] = TMath::Power(x[i], 3) + rg.Gaus() * 0.06;
80 }
81
82 TGraph *gr1 = new TGraph(n,x,y);
83 gr1->SetMarkerColor(kBlue);
84 gr1->SetMarkerStyle(8);
85 gr1->SetMarkerSize(1);
86 mg->Add(gr1);
87
88 // create second graph
89 TF1 *f_known=new TF1("f_known","pow(x,3)",0,1);
90 TGraph *gr2 = new TGraph(f_known);
91 gr2->SetMarkerColor(kRed);
92 gr2->SetMarkerStyle(8);
93 gr2->SetMarkerSize(1);
94 mg->Add(gr2);
95
96 //passing x and y values to R for fitting
97 ROOT::R::TRInterface &r=ROOT::R::TRInterface::Instance();
98 r["x"]<<TVectorD(n, x);
99 r["y"]<<TVectorD(n, y);
100 //creating a R data frame
101 r<<"ds<-data.frame(x=x,y=y)";
102 //fitting x and y to X^power using Nonlinear Least Squares
103 r<<"m <- nls(y ~ I(x^power),data = ds, start = list(power = 1),trace = T)";
104 //getting the fitted value (power)
105 Double_t power;
106 r["summary(m)$coefficients[1]"]>>power;
107
108 TF1 *f_fitted=new TF1("f_fitted","pow(x,[0])",0,1);
109 f_fitted->SetParameter(0,power);
110 //plotting the fitted function
111 TGraph *gr3 = new TGraph(f_fitted);
112 gr3->SetMarkerColor(kGreen);
113 gr3->SetMarkerStyle(8);
114 gr3->SetMarkerSize(1);
115
116 mg->Add(gr3);
117 mg->Draw("ap");
118
119 //displaying basic results
120 TPaveText *pt = new TPaveText(0.1,0.6,0.5,0.9,"brNDC");
121 pt->SetFillColor(18);
122 pt->SetTextAlign(12);
123 pt->AddText("Fitting x^power ");
124 pt->AddText(" \"Blue\" Points with gaussian noise to be fitted");
125 pt->AddText(" \"Red\" Known function x^3");
126 TString fmsg;
127 fmsg.Form(" \"Green\" Fitted function with power=%.4lf",power);
128 pt->AddText(fmsg);
129 pt->Draw();
130 c1->Update();
131 \endcode
132 */
133 class TRInterface: public TObject {
134 protected:
137 public:
138 //Proxy class to use operators for assignation Ex: r["name"]=object
139 class Binding {
140 public:
142 Binding(const Binding &obj) {
144 fName = obj.fName;
145 }
147 {
149 fName = obj.fName;
150 return *this;
151 }
152 template <class T> Binding &operator=(const T &data)
153 {
155 return *this;
156 }
158 {
159 //The method assign is not a template for a function
161 return *this;
162 }
163
165 {
166 //The method assign is not a template for a function
168 return *this;
169 }
170
172 {
173 fInterface->Assign(df, fName);
174 return *this;
175 }
176
178 {
179 fInterface->Assign(df, fName);
180 return *this;
181 }
182
183 template <class T> Binding &operator >>(T &var)
184 {
185 var = fInterface->Eval(fName).As<T>();
186 return *this;
187 }
188
189 template <class T> Binding &operator <<(T var)
190 {
191 fInterface->Assign<T>(var, fName);
192 return *this;
193 }
194#include<TRInterface_Binding.h>
195 template <class T> operator T()
196 {
197 return fInterface->Eval(fName);
198 }
199
200 private:
203 };
204 private:
205 /**
206 The command line arguments are by default argc=0 and argv=NULL,
207 The verbose mode is by default disabled but you can enable it to show procedures information in stdout/stderr \note some time can produce so much noise in the output
208 \param argc default 0
209 \param argv default null
210 \param loadRcpp default true
211 \param verbose default false
212 \param interactive default true
213 */
214 TRInterface(const Int_t argc = 0, const Char_t *argv[] = NULL, const Bool_t loadRcpp = true,
215 const Bool_t verbose = false, const Bool_t interactive = true);
216
217 public:
218 ~TRInterface();
219
220 /**
221 Method to set verbose mode, that produce extra output
222 \note some time can produce so much noise in the output
223 \param status boolean to enable of disable
224 */
225 void SetVerbose(Bool_t status);
226 /**
227 Method to eval R code and you get the result in a reference to TRObject
228 \param code R code
229 \param ans reference to TRObject
230 \return an true or false if the execution was successful or not.
231 */
232 Int_t Eval(const TString &code, TRObject &ans); // parse line, returns in ans; error code rc
233 /**
234 Method to eval R code
235 \param code R code
236 */
237 void Execute(const TString &code);
238
239 // "unhide" TObject::Execute methods.
240 using TObject::Execute;
241
242 /**
243 Method to eval R code and you get the result in a TRObject
244 \param code R code
245 \return a TRObject with result
246 */
247 TRObject Eval(const TString &code);
248
249
250 /**
251 Template method to assign C++ variables into R environment
252 \param var any R wrappable datatype
253 \param name name of the variable in R's environment
254 */
255 template<typename T >void Assign(const T &var, const TString &name)
256 {
257 // This method lets you pass variables from ROOT to R.
258 // The template T should be a supported ROOT datatype and
259 // the TString's name is the name of the variable in the R environment.
260 fR->assign<T>(var, name.Data());
261 }
262 /**
263 Method to assign TRFunctionExport in R's environment
264 \param fun TRFunctionExport
265 \param name name of the variable in R's environment
266 */
267 void Assign(const TRFunctionExport &fun, const TString &name);
268 /**
269 Method to assign TRDataFrame in R's environment
270 \param df TRDataFrame
271 \param name name of the variable in R's environment
272 */
273 void Assign(const TRDataFrame &df, const TString &name);
274
275 /**
276 Method to get a R prompt to work interactively with tab completion support
277 */
278 void Interactive();
279
280 /**
281 Init event loop in a thread to support actions in windows from R graphics system
282 */
283 void ProcessEventsLoop();
284
285 /**
286 Method to verify if a package is installed
287 \param pkg R's pkg name
288 \return true or false if the package is installed or not
289 */
291 /**
292 Method to load an R's package
293 \param pkg R's pkg name
294 \return true or false if the package was loaded or not
295 */
297 /**
298 Method to install an R's package
299 \param pkg R's pkg name
300 \param repos url for R's package repository
301 \return true or false if the package was installed or not
302 */
303 Bool_t Install(TString pkg, TString repos = "http://cran.r-project.org");
305
306 /**
307 static method to get an TRInterface instance reference
308 \return TRInterface instance reference
309 */
310 static TRInterface &Instance();
311 /**
312 static method to get an TRInterface instance pointer
313 \return TRInterface instance pointer
314 */
315 static TRInterface *InstancePtr();
316
318 };
319 }
320}
321
323{
324 r.Execute(code);
325 return r;
326}
327
328#endif
void Binding()
Definition Binding.C:21
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:51
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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
char name[80]
Definition TGX11.cxx:148
ROOT::R::TRInterface & operator<<(ROOT::R::TRInterface &r, TString code)
This is a class to create DataFrames from ROOT to R.
This is a class to pass functions from ROOT to R.
Binding & operator=(const TRFunctionExport &fun)
Binding(TRInterface *rnt, TString name)
Binding & operator<<(const TRDataFrame &df)
Binding(const Binding &obj)
Binding & operator>>(T &var)
Binding & operator<<(const TRFunctionExport &fun)
Binding & operator=(const TRDataFrame &df)
Binding & operator=(const T &data)
Binding & operator=(const Binding &obj)
ROOT R was implemented using the R Project library and the modules Rcpp and RInside
TRInterface(const Int_t argc=0, const Char_t *argv[]=NULL, const Bool_t loadRcpp=true, const Bool_t verbose=false, const Bool_t interactive=true)
The command line arguments are by default argc=0 and argv=NULL, The verbose mode is by default disabl...
void Execute(const TString &code)
Method to eval R code.
static TRInterface & Instance()
static method to get an TRInterface instance reference
void SetVerbose(Bool_t status)
Method to set verbose mode, that produce extra output.
Bool_t IsInstalled(TString pkg)
Method to verify if a package is installed.
Bool_t Require(TString pkg)
Method to load an R's package.
Int_t Eval(const TString &code, TRObject &ans)
Method to eval R code and you get the result in a reference to TRObject.
void Interactive()
Method to get a R prompt to work interactively with tab completion support.
void ProcessEventsLoop()
Init event loop in a thread to support actions in windows from R graphics system.
Binding operator[](const TString &name)
void Assign(const T &var, const TString &name)
Template method to assign C++ variables into R environment.
static TRInterface * InstancePtr()
static method to get an TRInterface instance pointer
Bool_t Install(TString pkg, TString repos="http://cran.r-project.org")
Method to install an R's package.
This is a class to get ROOT's objects from R's objects.
Definition TRObject.h:69
Mother of all ROOT objects.
Definition TObject.h:42
virtual void Execute(const char *method, const char *params, Int_t *error=nullptr)
Execute method on this object with the given parameter string, e.g.
Definition TObject.cxx:375
Basic string class.
Definition TString.h:138
<div class="legacybox"><h2>Legacy Code</h2> TThread is a legacy interface: there will be no bug fixes...
Definition TThread.h:40