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