19 using namespace ROOT::R;
 
   20 ClassImp(TRInterface);
 
   22 static ROOT::R::TRInterface *gR = 
nullptr;
 
   23 static Bool_t statusEventLoop;
 
   25 TRInterface::TRInterface(
const Int_t argc, 
const Char_t *argv[], 
const Bool_t loadRcpp, 
const Bool_t verbose,
 
   26                          const Bool_t interactive)
 
   29    if (RInside::instancePtr()) 
throw std::runtime_error(
"Can only have one TRInterface instance");
 
   30    fR = 
new RInside(argc, argv, loadRcpp, verbose, interactive);
 
   34    rcompgen_rho = R_FindNamespace(Rf_mkString(
"utils"));
 
   35    RComp_assignBufferSym  = Rf_install(
".assignLinebuffer");
 
   36    RComp_assignStartSym   = Rf_install(
".assignStart");
 
   37    RComp_assignEndSym     = Rf_install(
".assignEnd");
 
   38    RComp_assignTokenSym   = Rf_install(
".assignToken");
 
   39    RComp_completeTokenSym = Rf_install(
".completeToken");
 
   40    RComp_getFileCompSym   = Rf_install(
".getFileComp");
 
   41    RComp_retrieveCompsSym = Rf_install(
".retrieveCompletions");
 
   42    rl_attempted_completion_function = R_custom_completion;
 
   43    statusEventLoop = kFALSE;
 
   44    std::string osname = Eval(
"Sys.info()['sysname']");
 
   46    if (osname == 
"Linux") {
 
   47       Execute(
"options(device='x11')");
 
   49       Execute(
"options(device='quartz')");
 
   54 TRInterface::~TRInterface()
 
   56    statusEventLoop = kFALSE;
 
   59    if (gR == 
this) gR = 
nullptr;
 
   63 Int_t  TRInterface::Eval(
const TString &code, TRObject  &ans)
 
   69       rc = fR->parseEval(code.Data(), fans);
 
   70    } 
catch (Rcpp::exception &__ex__) {
 
   71       Error(
"Eval", 
"%s", __ex__.what());
 
   72       forward_exception_to_r(__ex__) ;
 
   74       Error(
"Eval", 
"Can execute the requested code: %s", code.Data());
 
   77    ans.SetStatus((rc == 0) ? kTRUE : kFALSE);
 
   82 void TRInterface::Execute(
const TString &code)
 
   86       fR->parseEvalQ(code.Data());
 
   87    } 
catch (Rcpp::exception &__ex__) {
 
   88       Error(
"Execute", 
"%s", __ex__.what());
 
   89       forward_exception_to_r(__ex__) ;
 
   91       Error(
"Execute", 
"Can execute the requested code: %s", code.Data());
 
   96 TRObject TRInterface::Eval(
const TString &code)
 
  105       rc = fR->parseEval(code.Data(), ans);
 
  106    } 
catch (Rcpp::exception &__ex__) {
 
  107       Error(
"Eval", 
"%s", __ex__.what());
 
  108       forward_exception_to_r(__ex__) ;
 
  110       Error(
"Eval", 
"Can execute the requested code: %s", code.Data());
 
  113    return TRObject(ans, (rc == 0) ? kTRUE : kFALSE);
 
  117 void TRInterface::SetVerbose(Bool_t status)
 
  121    fR->setVerbose(status);
 
  125 TRInterface::Binding TRInterface::operator[](
const TString &name)
 
  127    return Binding(
this, name);
 
  131 void TRInterface::Assign(
const TRFunctionExport &obj, 
const TString &name)
 
  133    fR->assign(*obj.f, name.Data());
 
  137 void TRInterface::Assign(
const TRDataFrame &obj, 
const TString &name)
 
  140    fR->assign(obj.df, name.Data());
 
  144 void TRInterface::Interactive()
 
  147       Char_t *line = readline(
"[r]:");
 
  149       if (std::string(line) == 
".q") 
break;
 
  151       if (*line) add_history(line);
 
  158 TRInterface *TRInterface::InstancePtr()
 
  161       const Char_t *R_argv[] = {
"rootr",    
"--gui=none", 
"--no-save", 
"--no-readline",
 
  162                                 "--silent", 
"--vanilla",  
"--slave"};
 
  163       gR = 
new TRInterface(7, R_argv, 
true, 
false, 
false);
 
  165    gR->ProcessEventsLoop();
 
  170 TRInterface &TRInterface::Instance()
 
  172    return  *TRInterface::InstancePtr();
 
  176 Bool_t TRInterface::IsInstalled(TString pkg)
 
  178    TString cmd = 
"is.element('" + pkg + 
"', installed.packages()[,1])";
 
  179    return fR->parseEval(cmd.Data());
 
  183 Bool_t TRInterface::Require(TString pkg)
 
  185    TString cmd = 
"require('" + pkg + 
"',quiet=TRUE)";
 
  186    return fR->parseEval(cmd.Data());
 
  190 Bool_t TRInterface::Install(TString pkg, TString repos)
 
  192    TString cmd = 
"install.packages('" + pkg + 
"',repos='" + repos + 
"',dependencies=TRUE)";
 
  193    fR->parseEval(cmd.Data());
 
  194    return IsInstalled(pkg);
 
  198 #undef _POSIX_C_SOURCE 
  199 #include <R_ext/eventloop.h> 
  202 void TRInterface::ProcessEventsLoop()
 
  204    if (!statusEventLoop) {
 
  205       th = 
new TThread([](
void *) {
 
  206          while (statusEventLoop) {
 
  209             fd = R_checkActivity(usec, 0);
 
  210             R_runHandlers(R_InputHandlers, fd);
 
  211             if (gSystem) gSystem->Sleep(100);
 
  214       statusEventLoop = kTRUE;