ROOT  6.06/09
Reference Guide
TRFunctionImport.h
Go to the documentation of this file.
1 // @(#)root/r:$Id$
2 // Author: Omar Zapata 28/06/2015
3 
4 
5 /*************************************************************************
6  * Copyright (C) 2015, Omar Andres Zapata Mesa *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 #ifndef ROOT_R_TRFunctionImport
13 #define ROOT_R_TRFunctionImport
14 
15 
16 #ifndef ROOT_R_RExports
17 #include<RExports.h>
18 #endif
19 
20 #ifndef ROOT_R_TRObject
21 #include<TRObject.h>
22 #endif
23 
24 #ifndef Rcpp_hpp
25 #include <Rcpp.h>
26 #endif
27 
28 
29 namespace ROOT {
30  namespace R {
31 
32  /**
33  \class TRFunctionImport
34  This is a class to pass functions from ROOT to R
35 
36  <center><h2>TRFunctionImport class</h2></center>
37  <p>
38  The TRFunctionImport class lets you call R's functions to ROOT's environment<br>
39  The object associated to this class have a set of overloaded operators to use the object like function<br>
40  </p>
41  \code{.cpp}
42  #include<TRInterface.h>
43 
44  using namespace ROOT::R;
45  void Function()
46  {
47  TRInterface &r = TRInterface::Instance();
48  r.SetVerbose(1);
49  ////////////////////////////////////////
50  //defining functions to be used from R//
51  ////////////////////////////////////////
52  TRFunctionImport c("c");
53  TRFunctionImport list("list");
54  TRFunctionImport asformula("as.formula");
55  TRFunctionImport nls("nls");
56  TRFunctionImport confint("confint");
57  TRFunctionImport summary("summary");
58  TRFunctionImport print("print");
59  TRFunctionImport plot("plot");
60  TRFunctionImport lines("lines");
61  TRFunctionImport devnew("dev.new");
62  TRFunctionImport devoff("dev.off");
63  TRFunctionImport min("min");
64  TRFunctionImport max("max");
65  TRFunctionImport seq("seq");
66  TRFunctionImport predict("predict");
67 
68  r<<"options(device='png')";//enable plot in png file
69 
70  ////////////////////////
71  //doing the procedure //
72  ////////////////////////
73  TRObject xdata = c(-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9);
74  TRObject ydata = c(0.699369,0.700462,0.695354,1.03905,1.97389,2.41143,1.91091,0.919576,-0.730975,-1.42001);
75 
76  TRDataFrame data;
77  data["xdata"]=xdata;
78  data["ydata"]=ydata;
79 
80  //fit = nls(ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata), start=list(p1=1,p2=0.2)) <- R code
81  TRObject fit = nls(asformula("ydata ~ p1*cos(p2*xdata) + p2*sin(p1*xdata)"),Label["data"]=data, Label["start"]=list(Label["p1"]=1,Label["p2"]=0.2));
82  print(summary(fit));
83 
84  print(confint(fit));
85 
86  devnew("Fitting Regression");
87  plot(xdata,ydata);
88 
89  TRObject xgrid=seq(min(xdata),max(xdata),Label["len"]=10);
90  lines(xgrid,predict(fit,xgrid),Label["col"] = "green");
91  devoff();
92  }
93  \endcode
94 
95  Output
96  \code
97  Formula: ydata ~ p1 * cos(p2 * xdata) + p2 * sin(p1 * xdata)
98 
99  Parameters:
100  Estimate Std. Error t value Pr(>|t|)
101  p1 1.881851 0.027430 68.61 2.27e-12 ***
102  p2 0.700230 0.009153 76.51 9.50e-13 ***
103  ---
104  Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
105 
106  Residual standard error: 0.08202 on 8 degrees of freedom
107 
108  Number of iterations to convergence: 7
109  Achieved convergence tolerance: 2.189e-06
110 
111  Waiting for profiling to be done...
112  2.5% 97.5%
113  p1 1.8206081 1.9442365
114  p2 0.6794193 0.7209843
115  \endcode
116  <h2>Users Guide </h2>
117  <a href="http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide"> http://oproject.org/tiki-index.php?page=ROOT+R+Users+Guide</a><br>
118  <a href="https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface"> https://root.cern.ch/drupal/content/how-use-r-root-root-r-interface</a>
119 
120  @ingroup R
121  */
122 
123  class TRInterface;
124  class TRFunctionImport: public TObject {
125  friend class TRInterface;
126  friend SEXP Rcpp::wrap<TRFunctionImport>(const TRFunctionImport &f);
127  friend TRFunctionImport Rcpp::as<>(SEXP);
128 
129  protected:
130  Rcpp::Function *f;//Internal Rcpp function to import
131  /**
132  TRFunctionImport constructor for Rcpp::DataFrame
133  \param fun raw function object from Rcpp
134  */
135 
137  {
138  *f = fun;
139  }
140 
141  public:
142  /**
143  TRFunctionImport constructor
144  \param name name of function from R
145  */
146  TRFunctionImport(const TString &name);
147  /**
148  TRFunctionImport constructor
149  \param name name of function from R
150  \param ns namespace of function from R
151  */
152  TRFunctionImport(const TString &name, const TString &ns);
153  /**
154  TRFunctionImport copy constructor
155  \param fun other TRFunctionImport
156  */
158  /**
159  TRFunctionImport constructor
160  \param obj raw R object
161  */
162  TRFunctionImport(SEXP obj);
163  /**
164  TRFunctionImport constructor
165  \param obj TRObject object
166  */
168 
170  {
171  if (f) delete f;
172  }
173  SEXP operator()()
174  {
175  return (*f)();
176  }
179  };
180 
181  template<> inline TRObject::operator TRFunctionImport()
182  {
183  return (SEXP)fObj;
184  }
185  }
186 }
187 
188 
189 
190 #endif
void * fObj
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
Basic string class.
Definition: TString.h:137
#define ClassDef(name, id)
Definition: Rtypes.h:254
This is a class to get ROOT's objects from R's objects
Definition: TRObject.h:73
This is a class to pass functions from ROOT to R.
TRFunctionImport(const Rcpp::Function &fun)
TRFunctionImport constructor for Rcpp::DataFrame.
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
TObject * obj
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28