Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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
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 */
112 class TRInterface;
113 class TRFunctionImport: public TObject {
114 friend class TRInterface;
118 protected:
119 Rcpp::Function *f;//Internal Rcpp function to import
120 /**
121 TRFunctionImport constructor for Rcpp::DataFrame
122 \param fun raw function object from Rcpp
123 */
124
125 TRFunctionImport(const Rcpp::Function &fun)
126 {
127 *f = fun;
128 }
130 public:
131 /**
132 TRFunctionImport constructor
133 \param name name of function from R
134 */
136 /**
137 TRFunctionImport constructor
138 \param name name of function from R
139 \param ns namespace of function from R
140 */
141 TRFunctionImport(const TString &name, const TString &ns);
142 /**
143 TRFunctionImport copy constructor
144 \param fun other TRFunctionImport
145 */
147 /**
148 TRFunctionImport constructor
149 \param obj raw R object
150 */
151 TRFunctionImport(SEXP obj);
152 /**
153 TRFunctionImport constructor
154 \param obj TRObject object
155 */
157
159 {
160 if (f) delete f;
161 }
163 {
164 return (*f)();
165 }
168 };
169
170 template<> inline TRObject::operator TRFunctionImport()
171 {
172 return (SEXP)fObj;
173 }
174 }
175}
176
177
178
179#endif
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
char name[80]
Definition TGX11.cxx:148
This is a class to pass functions from ROOT to R.
TRFunctionImport(const Rcpp::Function &fun)
TRFunctionImport constructor for Rcpp::DataFrame.
This is a class to get ROOT's objects from R's objects.
Definition TRObject.h:69
TObject()
TObject constructor.
Definition TObject.h:259
Basic string class.
Definition TString.h:138
namespace associated R package for ROOT.
Definition RExports.h:72
SEXP wrap(const TString &s)
Definition RExports.h:83
TString as(SEXP s)
Definition RExports.h:87