Logo ROOT  
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
25namespace 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="https://oproject.org/pages/ROOT%20R%20Users%20Guide"> https://oproject.org/pages/ROOT R Users Guide</a><br>
114
115 @ingroup R
116 */
117
118 class TRInterface;
119 class TRFunctionImport: public TObject {
120 friend class TRInterface;
121 friend SEXP Rcpp::wrap<TRFunctionImport>(const TRFunctionImport &f);
122 friend TRFunctionImport Rcpp::as<>(SEXP);
123
124 protected:
125 Rcpp::Function *f;//Internal Rcpp function to import
126 /**
127 TRFunctionImport constructor for Rcpp::DataFrame
128 \param fun raw function object from Rcpp
129 */
130
132 {
133 *f = fun;
134 }
135
136 public:
137 /**
138 TRFunctionImport constructor
139 \param name name of function from R
140 */
142 /**
143 TRFunctionImport constructor
144 \param name name of function from R
145 \param ns namespace of function from R
146 */
147 TRFunctionImport(const TString &name, const TString &ns);
148 /**
149 TRFunctionImport copy constructor
150 \param fun other TRFunctionImport
151 */
153 /**
154 TRFunctionImport constructor
155 \param obj raw R object
156 */
157 TRFunctionImport(SEXP obj);
158 /**
159 TRFunctionImport constructor
160 \param obj TRObject object
161 */
163
165 {
166 if (f) delete f;
167 }
169 {
170 return (*f)();
171 }
174 };
175
176 template<> inline TRObject::operator TRFunctionImport()
177 {
178 return (SEXP)fObj;
179 }
180 }
181}
182
183
184
185#endif
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
#define ClassDef(name, id)
Definition: Rtypes.h:322
char name[80]
Definition: TGX11.cxx:109
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.
ROOT R was implemented using the R Project library and the modules Rcpp and RInside
Definition: TRInterface.h:136
This is a class to get ROOT's objects from R's objects.
Definition: TRObject.h:70
Mother of all ROOT objects.
Definition: TObject.h:37
Basic string class.
Definition: TString.h:131
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: RNumpyDS.hxx:30
static constexpr double ns