forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRInterface.cxx
234 lines (203 loc) · 6.95 KB
/
TRInterface.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Author: Omar Zapata Omar.Zapata@cern.ch 2014
/*************************************************************************
* Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include<TRInterface.h>
#include"TRCompletion.h"
#include<vector>
extern "C"
{
#include <stdio.h>
#include <stdlib.h>
}
#include <TRint.h>
#include <TSystem.h>
#if defined(HAS_X11)
#include <X11/Xlib.h>
#include "TROOT.h"
#include "TEnv.h"
#endif
using namespace ROOT::R;
ClassImp(TRInterface);
static ROOT::R::TRInterface *gR = nullptr;
static Bool_t statusEventLoop;
TRInterface::TRInterface(const Int_t argc, const Char_t *argv[], const Bool_t loadRcpp, const Bool_t verbose,
const Bool_t interactive)
: TObject()
{
if (RInside::instancePtr()) throw std::runtime_error("Can only have one TRInterface instance");
fR = new RInside(argc, argv, loadRcpp, verbose, interactive);
//Installing the readline callbacks for completion in the
//method Interactive
rcompgen_rho = R_FindNamespace(Rf_mkString("utils"));
RComp_assignBufferSym = Rf_install(".assignLinebuffer");
RComp_assignStartSym = Rf_install(".assignStart");
RComp_assignEndSym = Rf_install(".assignEnd");
RComp_assignTokenSym = Rf_install(".assignToken");
RComp_completeTokenSym = Rf_install(".completeToken");
RComp_getFileCompSym = Rf_install(".getFileComp");
RComp_retrieveCompsSym = Rf_install(".retrieveCompletions");
rl_attempted_completion_function = R_custom_completion;
statusEventLoop = kFALSE;
std::string osname = Eval("Sys.info()['sysname']");
//only for linux/mac windows is not supported by ROOT yet.
#if defined(HAS_X11)
if (!gROOT->IsBatch()) {
if (gEnv->GetValue("X11.XInitThread", 1)) {
// Must be very first call before any X11 call !!
if (!XInitThreads())
Warning("OpenDisplay", "system has no X11 thread support");
}
}
#endif
if (osname == "Linux") {
Execute("options(device='x11')");
} else {
Execute("options(device='quartz')");
}
}
TRInterface::~TRInterface()
{
statusEventLoop = kFALSE;
if (th) delete th;
if (fR) delete fR;
if (gR == this) gR = nullptr;
}
//______________________________________________________________________________
Int_t TRInterface::Eval(const TString &code, TRObject &ans)
{
SEXP fans;
Int_t rc = kFALSE;
try {
rc = fR->parseEval(code.Data(), fans);
} catch (Rcpp::exception &__ex__) {
Error("Eval", "%s", __ex__.what());
forward_exception_to_r(__ex__) ;
} catch (...) {
Error("Eval", "Can execute the requested code: %s", code.Data());
}
ans = fans;
ans.SetStatus((rc == 0) ? kTRUE : kFALSE);
return rc;
}
//______________________________________________________________________________
void TRInterface::Execute(const TString &code)
{
try {
fR->parseEvalQ(code.Data());
} catch (Rcpp::exception &__ex__) {
Error("Execute", "%s", __ex__.what());
forward_exception_to_r(__ex__) ;
} catch (...) {
Error("Execute", "Can execute the requested code: %s", code.Data());
}
}
//______________________________________________________________________________
TRObject TRInterface::Eval(const TString &code)
{
// Execute R code.
//The RObject result of execution is returned in TRObject
SEXP ans;
int rc = kFALSE;
try {
rc = fR->parseEval(code.Data(), ans);
} catch (Rcpp::exception &__ex__) {
Error("Eval", "%s", __ex__.what());
forward_exception_to_r(__ex__) ;
} catch (...) {
Error("Eval", "Can execute the requested code: %s", code.Data());
}
return TRObject(ans, (rc == 0) ? kTRUE : kFALSE);
}
void TRInterface::SetVerbose(Bool_t status)
{
//verbose mode shows you all the procedures in stdout/stderr
//very important to debug and to see the results.
fR->setVerbose(status);
}
//______________________________________________________________________________
TRInterface::Binding TRInterface::operator[](const TString &name)
{
return Binding(this, name);
}
//______________________________________________________________________________
void TRInterface::Assign(const TRFunctionExport &obj, const TString &name)
{
fR->assign(*obj.f, name.Data());
}
//______________________________________________________________________________
void TRInterface::Assign(const TRDataFrame &obj, const TString &name)
{
//This method lets you pass c++ functions to R environment.
fR->assign(obj.df, name.Data());
}
//______________________________________________________________________________
void TRInterface::Interactive()
{
while (kTRUE) {
Char_t *line = readline("[r]:");
if (!line) continue;
if (std::string(line) == ".q") break;
Execute(line);
if (*line) add_history(line);
free(line);
}
}
//______________________________________________________________________________
TRInterface *TRInterface::InstancePtr()
{
if (!gR) {
const Char_t *R_argv[] = {"rootr", "--gui=none", "--no-save", "--no-readline",
"--silent", "--vanilla", "--slave"};
gR = new TRInterface(7, R_argv, true, false, false);
}
gR->ProcessEventsLoop();
return gR;
}
//______________________________________________________________________________
TRInterface &TRInterface::Instance()
{
return *TRInterface::InstancePtr();
}
//______________________________________________________________________________
Bool_t TRInterface::IsInstalled(TString pkg)
{
TString cmd = "is.element('" + pkg + "', installed.packages()[,1])";
return this->Eval(cmd).As<Bool_t>();
}
//______________________________________________________________________________
Bool_t TRInterface::Require(TString pkg)
{
TString cmd = "require('" + pkg + "',quiet=TRUE)";
return this->Eval(cmd).As<Bool_t>();
}
//______________________________________________________________________________
Bool_t TRInterface::Install(TString pkg, TString repos)
{
TString cmd = "install.packages('" + pkg + "',repos='" + repos + "',dependencies=TRUE)";
this->Eval(cmd);
return IsInstalled(pkg);
}
#undef _POSIX_C_SOURCE
#include <R_ext/eventloop.h>
//______________________________________________________________________________
void TRInterface::ProcessEventsLoop()
{
if (!statusEventLoop) {
th = new TThread([](void */*args */) {
while (statusEventLoop) {
fd_set *fd;
Int_t usec = 10000;
fd = R_checkActivity(usec, 0);
R_runHandlers(R_InputHandlers, fd);
if (gSystem) gSystem->Sleep(100);
}
});
statusEventLoop = kTRUE;
th->Run();
}
}