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