ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TRInterface.cxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2013-2014, Omar Andres Zapata Mesa *
3  * All rights reserved. *
4  * *
5  * For the licensing terms see $ROOTSYS/LICENSE. *
6  * For the list of contributors see $ROOTSYS/README/CREDITS. *
7  *************************************************************************/
8 #include<TRInterface.h>
9 #include"TRCompletion.h"
10 #include<vector>
11 
12 extern "C"
13 {
14 #include <stdio.h>
15 #include <stdlib.h>
16 }
17 #include<TRint.h>
18 
19 using namespace ROOT::R;
21 
22 static ROOT::R::TRInterface *gR = NULL;
23 static Bool_t statusEventLoop;
24 
25 TRInterface::TRInterface(const int argc, const char *argv[], const bool loadRcpp, const bool verbose, const bool interactive): TObject()
26 {
27  if (RInside::instancePtr()) throw std::runtime_error("Can only have one TRInterface instance");
28  fR = new RInside(argc, argv, loadRcpp, verbose, interactive);
29 
30  //Installing the readline callbacks for completion in the
31  //method Interactive
32  rcompgen_rho = R_FindNamespace(Rf_mkString("utils"));
33  RComp_assignBufferSym = Rf_install(".assignLinebuffer");
34  RComp_assignStartSym = Rf_install(".assignStart");
35  RComp_assignEndSym = Rf_install(".assignEnd");
36  RComp_assignTokenSym = Rf_install(".assignToken");
37  RComp_completeTokenSym = Rf_install(".completeToken");
38  RComp_getFileCompSym = Rf_install(".getFileComp");
39  RComp_retrieveCompsSym = Rf_install(".retrieveCompletions");
41  statusEventLoop = kFALSE;
42  std::string osname = Eval("Sys.info()['sysname']");
43  //only for linux/mac windows is not supported by ROOT yet.
44  if (osname == "Linux") {
45  Execute("options(device='x11')");
46  } else {
47  Execute("options(device='quartz')");
48  }
49 
50 }
51 
53 {
54  if (th) delete th;
55 }
56 
57 //______________________________________________________________________________
59 {
60  SEXP fans;
61 
62  Int_t rc = kFALSE;
63  try {
64  rc = fR->parseEval(code.Data(), fans);
65  } catch (Rcpp::exception &__ex__) {
66  Error("Eval", "%s", __ex__.what());
67  forward_exception_to_r(__ex__) ;
68  } catch (...) {
69  Error("Eval", "Can execute the requested code: %s", code.Data());
70  }
71  ans = fans;
72  ans.SetStatus((rc == 0) ? kTRUE : kFALSE);
73  return rc;
74 }
75 
76 //______________________________________________________________________________
77 void TRInterface::Execute(const TString &code)
78 {
79  try {
80 
81  fR->parseEvalQ(code.Data());
82  } catch (Rcpp::exception &__ex__) {
83  Error("Execute", "%s", __ex__.what());
84  forward_exception_to_r(__ex__) ;
85  } catch (...) {
86  Error("Execute", "Can execute the requested code: %s", code.Data());
87  }
88 }
89 
90 //______________________________________________________________________________
92 {
93 // Execute R code.
94 //The RObject result of execution is returned in TRObject
95 
96  SEXP ans;
97 
98  int rc = kFALSE;
99  try {
100  rc = fR->parseEval(code.Data(), ans);
101  } catch (Rcpp::exception &__ex__) {
102  Error("Eval", "%s", __ex__.what());
103  forward_exception_to_r(__ex__) ;
104  } catch (...) {
105  Error("Eval", "Can execute the requested code: %s", code.Data());
106  }
107 
108  return TRObject(ans , (rc == 0) ? kTRUE : kFALSE);
109 }
110 
111 
113 {
114  //verbose mode shows you all the procedures in stdout/stderr
115  //very important to debug and to see the results.
116  fR->setVerbose(status);
117 }
118 
119 //______________________________________________________________________________
121 {
122  return Binding(this, name);
123 }
124 
125 //______________________________________________________________________________
127 {
128  fR->assign(*obj.f, name.Data());
129 }
130 
131 //______________________________________________________________________________
133 {
134  //This method lets you pass c++ functions to R environment.
135  fR->assign(obj.df, name.Data());
136 }
137 
138 //______________________________________________________________________________
140 {
141  while (kTRUE) {
142  char *line = readline("[r]:");
143  if (!line) continue;
144  if (std::string(line) == ".q") break;
145  Execute(line);
146  if (*line) add_history(line);
147  free(line);
148  }
149 }
150 
151 
152 //______________________________________________________________________________
154 {
155  if (!gR) {
156  const char *R_argv[] = {"rootr", "--gui=none", "--no-save", "--no-readline", "--silent", "--vanilla", "--slave"};
157  gR = new TRInterface(7, R_argv, true, false, false);
158  }
159  gR->ProcessEventsLoop();
160  return gR;
161 }
162 
163 //______________________________________________________________________________
165 {
166  return *TRInterface::InstancePtr();
167 }
168 
169 //______________________________________________________________________________
171 {
172  TString cmd = "is.element('" + pkg + "', installed.packages()[,1])";
173  return fR->parseEval(cmd.Data());
174 }
175 
176 //______________________________________________________________________________
178 {
179  TString cmd = "require('" + pkg + "',quiet=TRUE)";
180  return fR->parseEval(cmd.Data());
181 }
182 
183 //______________________________________________________________________________
185 {
186  TString cmd = "install.packages('" + pkg + "',repos='" + repos + "',dependencies=TRUE)";
187  fR->parseEval(cmd.Data());
188  return IsInstalled(pkg);
189 }
190 
191 
192 #undef _POSIX_C_SOURCE
193 #include <R_ext/eventloop.h>
194 
195 //______________________________________________________________________________
197 {
198  if (!statusEventLoop) {
199  th = new TThread([](void *args) {
200  while (kTRUE) {
201  fd_set *fd;
202  int usec = 10000;
203  fd = R_checkActivity(usec, 0);
204  R_runHandlers(R_InputHandlers, fd);
205  gSystem->Sleep(100);
206  }
207  });
208  th->Run();
210  }
211 }
SEXP RComp_assignTokenSym
TRInternalFunction * f
SEXP RComp_getFileCompSym
XYZVector ans(TestRotation const &t, XYZVector const &v_in)
SEXP RComp_assignBufferSym
void Execute(const TString &code)
Method to eval R code.
Definition: TRInterface.cxx:77
Bool_t IsInstalled(TString pkg)
Method to verify if a package is installed.
TLine * line
static TRInterface * InstancePtr()
static method to get an TRInterface instance pointer
static ClassImp(TRInterface) static ROOT Bool_t statusEventLoop
Definition: TRInterface.cxx:20
Basic string class.
Definition: TString.h:137
SEXP RComp_completeTokenSym
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TRInterface(const int argc=0, const char *argv[]=NULL, const bool loadRcpp=true, const bool verbose=false, const bool interactive=true)
The command line arguments are by deafult argc=0 and argv=NULL, The verbose mode is by default disabl...
Definition: TRInterface.cxx:25
SEXP RComp_assignStartSym
void Binding()
Definition: Binding.C:21
const char * Data() const
Definition: TString.h:349
Rcpp::DataFrame df
Definition: TRDataFrame.h:187
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:441
void Assign(const T &var, const TString &name)
Template method to assign C++ variables into R enviroment.
Definition: TRInterface.h:260
Bool_t Install(TString pkg, TString repos="http://cran.r-project.org")
Method to install an R's package.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:552
Bool_t Require(TString pkg)
Method to load an R's package.
This is a class to get ROOT's objects from R's objects
Definition: TRObject.h:73
rl_completion_func_t * rl_attempted_completion_function
SEXP RComp_retrieveCompsSym
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
char ** R_custom_completion(const char *text, int start, int end)
bool verbose
void SetStatus(Bool_t status)
TRObject is a current valid object?
Definition: TRObject.h:99
tuple free
Definition: fildir.py:30
void ProcessEventsLoop()
Init event loop in a thread to support actions in windows from R graphics system. ...
void Interactive()
Method to get a R prompt to work interactively with tab completation support.
SEXP rcompgen_rho
#define ClassImp(name)
Definition: Rtypes.h:279
void SetVerbose(Bool_t status)
Method to set verbose mode, that produce extra output.
static TRInterface & Instance()
static method to get an TRInterface instance reference
Binding operator[](const TString &name)
#define name(a, b)
Definition: linkTestLib0.cpp:5
SEXP RComp_assignEndSym
Mother of all ROOT objects.
Definition: TObject.h:58
#define NULL
Definition: Rtypes.h:82
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
Int_t Eval(const TString &code, TRObject &ans)
Method to eval R code and you get the result in a reference to TRObject.
Definition: TRInterface.cxx:58
This is a class to pass functions from ROOT to R
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28
This is a class to create DataFrames from ROOT to R
Definition: TRDataFrame.h:183