Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TRInterface.cxx
Go to the documentation of this file.
1// Author: Omar Zapata Omar.Zapata@cern.ch 2014
2
3/*************************************************************************
4 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10#include<TRInterface.h>
11#include"TRCompletion.h"
12#include<vector>
13
14extern "C"
15{
16#include <stdio.h>
17#include <stdlib.h>
18}
19#include <TRint.h>
20#include <TSystem.h>
21
22#if defined(HAS_X11)
23#include <X11/Xlib.h>
24#include "TROOT.h"
25#include "TEnv.h"
26#endif
27using namespace ROOT::R;
29
30static ROOT::R::TRInterface *gR = nullptr;
32
33TRInterface::TRInterface(const Int_t argc, const Char_t *argv[], const Bool_t loadRcpp, const Bool_t verbose,
34 const Bool_t interactive)
35 : TObject()
36{
37 if (RInside::instancePtr()) throw std::runtime_error("Can only have one TRInterface instance");
38 fR = new RInside(argc, argv, loadRcpp, verbose, interactive);
39
40 //Installing the readline callbacks for completion in the
41 //method Interactive
42 rcompgen_rho = R_FindNamespace(Rf_mkString("utils"));
43 RComp_assignBufferSym = Rf_install(".assignLinebuffer");
44 RComp_assignStartSym = Rf_install(".assignStart");
45 RComp_assignEndSym = Rf_install(".assignEnd");
46 RComp_assignTokenSym = Rf_install(".assignToken");
47 RComp_completeTokenSym = Rf_install(".completeToken");
48 RComp_getFileCompSym = Rf_install(".getFileComp");
49 RComp_retrieveCompsSym = Rf_install(".retrieveCompletions");
52 std::string osname = Eval("Sys.info()['sysname']");
53 //only for linux/mac windows is not supported by ROOT yet.
54#if defined(HAS_X11)
55 if (!gROOT->IsBatch()) {
56 if (gEnv->GetValue("X11.XInitThread", 1)) {
57 // Must be very first call before any X11 call !!
58 if (!XInitThreads())
59 Warning("OpenDisplay", "system has no X11 thread support");
60 }
61 }
62#endif
63 if (osname == "Linux") {
64 Execute("options(device='x11')");
65 } else {
66 Execute("options(device='quartz')");
67 }
68
69}
70
72{
74 if (th) delete th;
75 if (fR) delete fR;
76 if (gR == this) gR = nullptr;
77}
78
79//______________________________________________________________________________
81{
82 SEXP fans;
83
84 Int_t rc = kFALSE;
85 try {
86 rc = fR->parseEval(code.Data(), fans);
87 } catch (Rcpp::exception &__ex__) {
88 Error("Eval", "%s", __ex__.what());
89 forward_exception_to_r(__ex__) ;
90 } catch (...) {
91 Error("Eval", "Can execute the requested code: %s", code.Data());
92 }
93 ans = fans;
94 ans.SetStatus((rc == 0) ? kTRUE : kFALSE);
95 return rc;
96}
97
98//______________________________________________________________________________
100{
101 try {
102
103 fR->parseEvalQ(code.Data());
104 } catch (Rcpp::exception &__ex__) {
105 Error("Execute", "%s", __ex__.what());
106 forward_exception_to_r(__ex__) ;
107 } catch (...) {
108 Error("Execute", "Can execute the requested code: %s", code.Data());
109 }
110}
111
112//______________________________________________________________________________
114{
115// Execute R code.
116//The RObject result of execution is returned in TRObject
117
118 SEXP ans;
119
120 int rc = kFALSE;
121 try {
122 rc = fR->parseEval(code.Data(), ans);
123 } catch (Rcpp::exception &__ex__) {
124 Error("Eval", "%s", __ex__.what());
125 forward_exception_to_r(__ex__) ;
126 } catch (...) {
127 Error("Eval", "Can execute the requested code: %s", code.Data());
128 }
129
130 return TRObject(ans, (rc == 0) ? kTRUE : kFALSE);
131}
132
133
135{
136 //verbose mode shows you all the procedures in stdout/stderr
137 //very important to debug and to see the results.
138 fR->setVerbose(status);
139}
140
141//______________________________________________________________________________
143{
144 return Binding(this, name);
145}
146
147//______________________________________________________________________________
149{
150 fR->assign(*obj.f, name.Data());
151}
152
153//______________________________________________________________________________
155{
156 //This method lets you pass c++ functions to R environment.
157 fR->assign(obj.df, name.Data());
158}
159
160//______________________________________________________________________________
162{
163 while (kTRUE) {
164 Char_t *line = readline("[r]:");
165 if (!line) continue;
166 if (std::string(line) == ".q") break;
167 Execute(line);
168 if (*line) add_history(line);
169 free(line);
170 }
171}
172
173
174//______________________________________________________________________________
176{
177 if (!gR) {
178 const Char_t *R_argv[] = {"rootr", "--gui=none", "--no-save", "--no-readline",
179 "--silent", "--vanilla", "--slave"};
180 gR = new TRInterface(7, R_argv, true, false, false);
181 }
183 return gR;
184}
185
186//______________________________________________________________________________
188{
189 return *TRInterface::InstancePtr();
190}
191
192//______________________________________________________________________________
194{
195 TString cmd = "is.element('" + pkg + "', installed.packages()[,1])";
196 return this->Eval(cmd).As<Bool_t>();
197}
198
199//______________________________________________________________________________
201{
202 TString cmd = "require('" + pkg + "',quiet=TRUE)";
203 return this->Eval(cmd).As<Bool_t>();
204}
205
206//______________________________________________________________________________
208{
209 TString cmd = "install.packages('" + pkg + "',repos='" + repos + "',dependencies=TRUE)";
210 this->Eval(cmd);
211 return IsInstalled(pkg);
212}
213
214
215#undef _POSIX_C_SOURCE
216#include <R_ext/eventloop.h>
217
218//______________________________________________________________________________
220{
221 if (!statusEventLoop) {
222 th = new TThread([](void */*args */) {
223 while (statusEventLoop) {
224 fd_set *fd;
225 Int_t usec = 10000;
226 fd = R_checkActivity(usec, 0);
227 R_runHandlers(R_InputHandlers, fd);
228 if (gSystem) gSystem->Sleep(100);
229 }
230 });
232 th->Run();
233 }
234}
char Char_t
Definition RtypesCore.h:37
const Bool_t kFALSE
Definition RtypesCore.h:92
bool Bool_t
Definition RtypesCore.h:63
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
R__EXTERN TEnv * gEnv
Definition TEnv.h:171
char name[80]
Definition TGX11.cxx:110
rl_completion_func_t * rl_attempted_completion_function
static Bool_t statusEventLoop
static ROOT::R::TRInterface * gR
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
#define free
Definition civetweb.c:1539
This is a class to create DataFrames from ROOT to R.
Rcpp::DataFrame df
This is a class to pass functions from ROOT to R.
ROOT R was implemented using the R Project library and the modules Rcpp and RInside
TRInterface(const Int_t argc=0, const Char_t *argv[]=NULL, const Bool_t loadRcpp=true, const Bool_t verbose=false, const Bool_t interactive=true)
The command line arguments are by deafult argc=0 and argv=NULL, The verbose mode is by default disabl...
void Execute(const TString &code)
Method to eval R code.
static TRInterface & Instance()
static method to get an TRInterface instance reference
void SetVerbose(Bool_t status)
Method to set verbose mode, that produce extra output.
Bool_t IsInstalled(TString pkg)
Method to verify if a package is installed.
Bool_t Require(TString pkg)
Method to load an R's package.
Int_t Eval(const TString &code, TRObject &ans)
Method to eval R code and you get the result in a reference to TRObject.
void Interactive()
Method to get a R prompt to work interactively with tab completation support.
void ProcessEventsLoop()
Init event loop in a thread to support actions in windows from R graphics system.
Binding operator[](const TString &name)
void Assign(const T &var, const TString &name)
Template method to assign C++ variables into R enviroment.
static TRInterface * InstancePtr()
static method to get an TRInterface instance pointer
Bool_t Install(TString pkg, TString repos="http://cran.r-project.org")
Method to install an R's package.
This is a class to get ROOT's objects from R's objects.
Definition TRObject.h:70
void SetStatus(Bool_t status)
TRObject is a current valid object?
Definition TRObject.h:96
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:491
Mother of all ROOT objects.
Definition TObject.h:37
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:879
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:893
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:438
Int_t Run(void *arg=nullptr)
Start the thread.
Definition TThread.cxx:568
TLine * line
namespace associated R package for ROOT.
Definition RExports.h:56
SEXP RComp_assignBufferSym
SEXP RComp_completeTokenSym
SEXP RComp_retrieveCompsSym
SEXP rcompgen_rho
SEXP RComp_assignStartSym
SEXP RComp_assignTokenSym
SEXP RComp_getFileCompSym
char ** R_custom_completion(const char *text, int start, int end)
SEXP RComp_assignEndSym