int main (int argc, char **argv) { structRstart rp; Rstart Rp = &rp; char Rversion[25], *RHome; sprintf(Rversion, "%s.%s", R_MAJOR, R_MINOR); if(strcmp(getDLLVersion(), Rversion) != 0) { fprintf(stderr, "Error: R.DLL version does not match\n"); exit(1); } R_setStartTime(); R_DefParams(Rp); if((RHome = get_R_HOME()) == NULL) { fprintf(stderr, "R_HOME must be set in the environment or Registry\n"); exit(1); } Rp->rhome = RHome; Rp->home = getRUser(); Rp->CharacterMode = LinkDLL; Rp->ReadConsole = myReadConsole; Rp->WriteConsole = myWriteConsole; Rp->CallBack = myCallBack; Rp->ShowMessage = askok; Rp->YesNoCancel = askyesnocancel; Rp->Busy = myBusy; Rp->R_Quiet = TRUE; Rp->R_Interactive = FALSE; Rp->RestoreAction = SA_RESTORE; Rp->SaveAction = SA_NOSAVE; R_SetParams(Rp); /* so R_ShowMessage is set */ R_SizeFromEnv(Rp); R_SetParams(Rp); R_set_command_line_arguments(argc, argv); FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)); signal(SIGBREAK, my_onintr); setup_term_ui(); /* initialize graphapp, eventloop, read Rconsole */ setup_Rmainloop(); #ifdef SIMPLE_CASE run_Rmainloop(); end_Rmainloop(); #else R_ReplDLLinit(); while(R_ReplDLLdo1() > 0) { /* add user actions here if desired */ } /* only get here on EOF (not q()) */ R_CleanUp(SA_DEFAULT, 0, 1); #endif end_Rmainloop(); return 0; }
REmbed::REmbed(const bool verbose, const bool interactive) : verbose(verbose), interactive(interactive) { loaded = false; // need to load the library RLibrary rlib; if (!rlib.load()) { // disable for future appsettings->setValue(GC_EMBED_R, false); rlib.errors.append("\nR has now been disabled in options."); QMessageBox msg(QMessageBox::Information, "Failed to load R library", rlib.errors.join("\n")); msg.exec(); return; } // we need to tell embedded R where to work QString envR_HOME(getenv("R_HOME")); QString configR_HOME = appsettings->value(NULL,GC_R_HOME,"").toString(); if (envR_HOME == "") { if (configR_HOME == "") { qDebug()<<"R HOME not set, R disabled"; return; } else { setenv("R_HOME", configR_HOME.toLatin1().constData(), true); } } // fire up R const char *R_argv[] = {name, "--gui=none", "--no-save", "--no-readline", "--silent", "--vanilla", "--slave"}; int R_argc = sizeof(R_argv) / sizeof(R_argv[0]); Rf_initEmbeddedR(R_argc, (char**)R_argv); R_ReplDLLinit(); // this is to populate the repl console buffers structRstart Rst; R_DefParams(&Rst); #ifdef WIN32 Rst.rhome = getenv("R_HOME"); Rst.home = getRUser(); Rst.CharacterMode = LinkDLL; Rst.ReadConsole = &RTool::R_ReadConsoleWin; Rst.WriteConsole = &RTool::R_WriteConsole; Rst.WriteConsoleEx = &RTool::R_WriteConsoleEx; Rst.CallBack = &RTool::R_ProcessEvents; Rst.ShowMessage = &RTool::R_ShowMessage; Rst.YesNoCancel = &RTool::R_YesNoCancel; Rst.Busy = &RTool::R_Busy; #endif Rst.R_Interactive = (Rboolean) interactive; // sets interactive() to eval to false R_SetParams(&Rst); loaded = true; }
int main (int argc, char **argv) { structRstart rp; Rstart Rp = &rp; char Rversion[25], *RHome; snprintf(Rversion, 25, "%s.%s", R_MAJOR, R_MINOR); if(strcmp(getDLLVersion(), Rversion) != 0) { fprintf(stderr, "Error: R.DLL version does not match\n"); exit(1); } R_setStartTime(); R_DefParams(Rp); if((RHome = get_R_HOME()) == NULL) { fprintf(stderr, "R_HOME must be set in the environment or Registry\n"); exit(1); } Rp->rhome = RHome; Rp->home = getRUser(); Rp->CharacterMode = LinkDLL; Rp->ReadConsole = myReadConsole; Rp->WriteConsole = NULL; /* for illustration purposes we use more flexible WriteConsoleEx */ Rp->WriteConsoleEx = myWriteConsoleEx; Rp->CallBack = myCallBack; Rp->ShowMessage = askok; Rp->YesNoCancel = askyesnocancel; Rp->Busy = myBusy; Rp->R_Quiet = TRUE; Rp->R_Interactive = FALSE; Rp->RestoreAction = SA_RESTORE; Rp->SaveAction = SA_NOSAVE; R_SetParams(Rp); R_set_command_line_arguments(argc, argv); FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)); signal(SIGBREAK, my_onintr); GA_initapp(0, 0); readconsolecfg(); setup_Rmainloop(); #ifdef SIMPLE_CASE run_Rmainloop(); #else R_ReplDLLinit(); while(R_ReplDLLdo1() > 0) { /* add user actions here if desired */ } /* only get here on EOF (not q()) */ #endif Rf_endEmbeddedR(0); return 0; }
int Rf_initialize_R(int argc, char **argv) { structRstart rp; Rstart Rp = &rp; char Rversion[25], *RHome; snprintf(Rversion, 25, "%s.%s", R_MAJOR, R_MINOR); if(strncmp(getDLLVersion(), Rversion, 25) != 0) { fprintf(stderr, "Error: R.DLL version does not match\n"); exit(1); } R_setStartTime(); R_DefParams(Rp); if((RHome = get_R_HOME()) == NULL) { fprintf(stderr, "R_HOME must be set in the environment or Registry\n"); exit(2); } Rp->rhome = RHome; Rp->home = getRUser(); Rp->CharacterMode = LinkDLL; Rp->ReadConsole = myReadConsole; Rp->WriteConsole = myWriteConsole; Rp->CallBack = myCallBack; Rp->ShowMessage = askok; Rp->YesNoCancel = askyesnocancel; Rp->Busy = myBusy; Rp->R_Quiet = TRUE; Rp->R_Interactive = TRUE; Rp->RestoreAction = SA_RESTORE; Rp->SaveAction = SA_NOSAVE; R_SetParams(Rp); R_set_command_line_arguments(argc, argv); FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)); signal(SIGBREAK, my_onintr); GA_initapp(0, 0); R_LoadRconsole = FALSE; readconsolecfg(); return 0; }
int rcmdfn (int cmdarg, int argc, char **argv) { /* tasks: find R_HOME, set as env variable (with / as separator) set R_ARCH set PATH to include R_HOME\bin set TMPDIR if unset set HOME if unset set R_CMD (depends on how this was launched), R_VERSION read R_HOME\etc\Rcmd_environ launch %R_HOME%\bin\$* */ int i, iused, status = 0; char *p, cmd[CMD_LEN]; char RCMD[] = "R CMD"; int len = strlen(argv[0]); char env_path[MAX_PATH]; int timing = 1; char *RHome = getRHOME(3); if(!strncmp(argv[0]+len-4, "Rcmd", 4) || !strncmp(argv[0]+len-4, "rcmd", 4) || !strncmp(argv[0]+len-8, "Rcmd.exe", 8) || !strncmp(argv[0]+len-8, "rcmd.exe", 8)) strcpy(RCMD, "Rcmd"); if (argc <= cmdarg) { fprintf(stderr, "%s%s%s", "Usage: ", RCMD, " command args\n\n"); rcmdusage(RCMD); return(0); } if (argc == cmdarg+1 && (!strcmp(argv[cmdarg], "--help") || !strcmp(argv[cmdarg], "-h")) ) { /* need to cover Rcmd --help, R CMD --help and R --help, as well as -h versions. */ if(cmdarg >= 2 || (cmdarg == 1 && !strcmp(RCMD, "Rcmd"))) { fprintf(stderr, "%s%s%s", "Usage: ", RCMD, " command args\n\n"); rcmdusage(RCMD); return(0); } /* R --help */ snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe --help", getRHOME(3), BINDIR); system(cmd); fprintf(stderr, "%s", "\n\nOr: R CMD command args\n\n"); rcmdusage(RCMD); return(0); } if (cmdarg == 0) { /* use of R.exe without CMD, -h, --help */ if (argc > 1 && !strcmp(argv[1], "RHOME")) { /* An historical special case */ fprintf(stdout, "%s", getRHOME(3)); return(0); } snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe", getRHOME(3), BINDIR); for (i = cmdarg + 1; i < argc; i++){ strcat(cmd, " "); if (strlen(cmd) + strlen(argv[i]) > 9900) { fprintf(stderr, "command line too long\n"); return(27); } if(strchr(argv[i], ' ') || !strlen(argv[i])) { strcat(cmd, "\""); strcat(cmd, argv[i]); strcat(cmd, "\""); } else strcat(cmd, argv[i]); } /* R.exe should ignore Ctrl-C, and let Rterm.exe handle it */ SetConsoleCtrlHandler(NULL, TRUE); return system(cmd); } /* From here on down, this was called as Rcmd or R CMD */ char RHOME[MAX_PATH]; strcpy(RHOME, "R_HOME="); strcat(RHOME, RHome); for (p = RHOME; *p; p++) if (*p == '\\') *p = '/'; putenv(RHOME); /* We follow Unix-alikes as from R 2.12.0 in setting environment variables in Rcmd BATCH. NB: Rcmd_environ uses R_HOME. */ strcpy(env_path, RHome); strcat(env_path, "/etc/Rcmd_environ"); process_Renviron(env_path); if (!strcmp(argv[cmdarg], "BATCH")) { /* ----- handle Rcmd BATCH ---- */ char infile[MAX_PATH], outfile[MAX_PATH], *p, cmd_extra[CMD_LEN]; DWORD ret; SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pi; STARTUPINFO si; HANDLE hOUT = INVALID_HANDLE_VALUE; /* process the command line */ cmd_extra[0] = '\0'; if((p = getenv("R_BATCH_OPTIONS")) && strlen(p)) { if(1+strlen(p) >= CMD_LEN) { fprintf(stderr, "command line too long\n"); return(27); } strcat(cmd_extra, " "); strcat(cmd_extra, p); } for(i = cmdarg + 1, iused = cmdarg; i < argc; i++) { if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", "Usage: ", RCMD, " BATCH [options] infile [outfile]\n\n", "Run R non-interactively with input from infile and place output (stdout\n", "and stderr) to another file. If not given, the name of the output file\n", "is the one of the input file, with a possible '.R' extension stripped,\n", "and '.Rout' appended.\n\n", "Options:\n" " -h, --help print short help message and exit\n", " -v, --version print version info and exit\n", " --no-timing do not report the timings\n", " -- end processing of options\n\n", "Further arguments starting with a '-' are considered as options as long\n", "as '--' was not encountered, and are passed on to the R process, which\n", "by default is started with '--restore --save'.\n\n", "Report bugs at bugs.r-project.org ."); return(0); } if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { fprintf(stderr, "BATCH %s\n%s%s%s\n", "1.2", "Copyright (C) 1997-2004 R Core Team.\n", "This is free software; see the GNU General Public Licence version 2\n", "or later for copying conditions. There is NO warranty."); return(0); } if (!strcmp(argv[i], "--no-timing")) { timing = 0; iused = i; continue; } if (!strcmp(argv[i], "--")) { iused = i; break; } if (argv[i][0] == '-') { if (strlen(cmd_extra) + strlen(argv[i]) > 9900) { fprintf(stderr, "command line too long\n"); return(27); } strcat(cmd_extra, " "); strcat(cmd_extra, argv[i]); iused = i; } else break; } if (iused+1 < argc) strcpy(infile, argv[iused+1]); else { fprintf(stderr, "no input file\n"); return(1); } if (iused+2 < argc) strcpy(outfile, argv[iused+2]); else { int len = strlen(infile); strcpy(outfile, infile); if (!strcmp(outfile+len-2, ".R")) strcat(outfile, "out"); else strcat(outfile, ".Rout"); } /* Unix has --restore --save --no-readline */ snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -f \"%s\" --restore --save", getRHOME(3), BINDIR, infile); if(strlen(cmd) + strlen(cmd_extra) >= CMD_LEN) { fprintf(stderr, "command line too long\n"); return(27); } strcat(cmd, cmd_extra); /* to get .Last.sys run: see profile/Common.R */ if(timing) putenv("R_BATCH=1234"); /* fprintf(stderr, "%s->%s\n", infile, outfile); fprintf(stderr, "%s\n", cmd); */ sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; hOUT = CreateFile(outfile, GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, 0, NULL); if (hOUT == INVALID_HANDLE_VALUE) { fprintf(stderr, "unable to open output file\n"); return(2); } SetStdHandle(STD_OUTPUT_HANDLE, hOUT); SetStdHandle(STD_ERROR_HANDLE, hOUT); si.cb = sizeof(si); si.lpReserved = NULL; si.lpReserved2 = NULL; si.cbReserved2 = 0; si.lpDesktop = NULL; si.lpTitle = NULL; si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_SHOWDEFAULT; ret = CreateProcess(0, cmd, &sa, &sa, TRUE, 0, NULL, NULL, &si, &pi); CloseHandle(hOUT); if (!ret) { fprintf(stderr, "unable to run Rterm.exe\n"); return(3); } CloseHandle(pi.hThread); return(pwait(pi.hProcess)); /* ------- end of BATCH -------- */ } /* Now Rcmd <cmd> or R CMD <cmd>: most commands are nowadays * handled internally on Windows */ /* Not sure that we still need these set -- they are Windows-only */ char Rversion[25]; snprintf(Rversion, 25, "R_VERSION=%s.%s", R_MAJOR, R_MINOR); putenv(Rversion); putenv("R_CMD=R CMD"); char *Path = malloc( strlen(getenv("PATH")) + MAX_PATH + 10 ); if (!Path) {fprintf(stderr, "PATH too long\n"); return(4);} strcpy(Path, "PATH="); strcat(Path, RHome); strcat(Path, "\\"); strcat(Path, BINDIR); strcat(Path, ";"); strcat(Path, getenv("PATH")); putenv(Path); free(Path); char Rarch[30]; strcpy(Rarch, "R_ARCH=/"); strcat(Rarch, R_ARCH); putenv(Rarch); char Bindir[30]; strcpy(Bindir, "BINDIR="); strcat(Bindir, BINDIR); putenv(Bindir); char Tmpdir[MAX_PATH+10]; if ( (p = getenv("TMPDIR")) && isDir(p)) { /* TMPDIR is already set */ } else { if ( (p = getenv("TEMP")) && isDir(p)) { strcpy(Tmpdir, "TMPDIR="); strcat(Tmpdir, p); putenv(Tmpdir); } else if ( (p = getenv("TMP")) && isDir(p)) { strcpy(Tmpdir, "TMPDIR="); strcat(Tmpdir, p); putenv(Tmpdir); } else { strcpy(Tmpdir, "TMPDIR="); strcat(Tmpdir, getRUser()); putenv(Tmpdir); } } char HOME[MAX_PATH+10]; if( !getenv("HOME") ) { strcpy(HOME, "HOME="); strcat(HOME, getRUser()); putenv(HOME); } if (!strcmp(argv[cmdarg], "INSTALL")) { /* Unix has --no-restore except for MM's undocumented --use-vanilla */ snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::.install_packages() R_DEFAULT_PACKAGES= LC_COLLATE=C --no-restore --slave --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "REMOVE")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -f \"%s/share/R/REMOVE.R\" R_DEFAULT_PACKAGES=NULL --no-restore --slave --args", getRHOME(3), BINDIR, getRHOME(3)); for (i = cmdarg + 1; i < argc; i++){ strcat(cmd, " "); if (strlen(cmd) + strlen(argv[i]) > 9900) { fprintf(stderr, "command line too long\n"); return(27); } /* Library names could contain spaces */ if(strchr(argv[i], ' ')) { strcat(cmd, "\""); strcat(cmd, argv[i]); strcat(cmd, "\""); } else strcat(cmd, argv[i]); } return(system(cmd)); } else if (!strcmp(argv[cmdarg], "build")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::.build_packages() R_DEFAULT_PACKAGES= LC_COLLATE=C --no-restore --slave --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "check")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::.check_packages() R_DEFAULT_PACKAGES= LC_COLLATE=C --no-restore --slave --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "Rprof")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::.Rprof() R_DEFAULT_PACKAGES=utils LC_COLLATE=C --vanilla --slave --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "texify")) { if (argc < cmdarg+2) { fprintf(stderr, "\nUsage: %s texify [options] filename\n", RCMD); return(1); } snprintf(cmd, CMD_LEN, "texify.exe -I %s/share/texmf/tex/latex -I %s/share/texmf/bibtex/bst", getRHOME(3), getRHOME(3)); PROCESS_CMD(" "); } else if (!strcmp(argv[cmdarg], "SHLIB")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::.SHLIB() R_DEFAULT_PACKAGES=NULL --no-restore --slave --no-site-file --no-init-file --args", getRHOME(3), BINDIR); PROCESS_CMD(" "); } else if (!strcmp(argv[cmdarg], "Rdiff")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::.Rdiff() R_DEFAULT_PACKAGES=NULL --vanilla --slave --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "Rdconv")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::.Rdconv() R_DEFAULT_PACKAGES= LC_COLLATE=C --vanilla --slave --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "Rd2txt")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::.Rdconv() R_DEFAULT_PACKAGES= LC_COLLATE=C --vanilla --slave --args nextArg-tnextArgtxt", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "Rd2pdf")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe -e tools:::..Rd2pdf() R_DEFAULT_PACKAGES= LC_ALL=C --vanilla --slave --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "Sweave")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe --no-restore --slave -e utils:::.Sweave() --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else if (!strcmp(argv[cmdarg], "Stangle")) { snprintf(cmd, CMD_LEN, "%s/%s/Rterm.exe --vanilla --slave -e utils:::.Stangle() --args ", getRHOME(3), BINDIR); PROCESS_CMD("nextArg"); } else { /* not one of those handled internally */ p = argv[cmdarg]; if (!strcmp(p, "config")) snprintf(cmd, CMD_LEN, "sh %s/bin/config.sh", RHome); else if (!strcmp(p, "open")) snprintf(cmd, CMD_LEN, "%s/%s/open.exe", RHome, BINDIR); else { /* RHOME/BINDIR is first in the path, so looks there first */ if (!strcmp(".sh", p + strlen(p) - 3)) strcpy(cmd, "sh "); else if (!strcmp(".pl", p + strlen(p) - 3)) strcpy(cmd, "perl "); else strcpy(cmd, ""); strcat(cmd, p); } for (i = cmdarg + 1; i < argc; i++){ strcat(cmd, " "); if (strlen(cmd) + strlen(argv[i]) > 9900) { fprintf(stderr, "command line too long\n"); return(27); } if(strchr(argv[i], ' ')) { strcat(cmd, "\""); strcat(cmd, argv[i]); strcat(cmd, "\""); } else strcat(cmd, argv[i]); } /* printf("cmd is %s\n", cmd); */ status = system(cmd); } return(status); }
// TODO: use a vector<string> would make all this a bit more readable void RInside::initialize(const int argc, const char* const argv[], const bool loadRcpp, const bool verbose, const bool interactive) { if (instance_m) { throw std::runtime_error( "can only have one RInside instance" ) ; } else { instance_m = this ; } verbose_m = verbose; // Default is false interactive_m = interactive; // generated from Makevars{.win} #include "RInsideEnvVars.h" #ifdef WIN32 // we need a special case for Windows where users may deploy an RInside binary from CRAN // which will have R_HOME set at compile time to CRAN's value -- so let's try to correct // this here: a) allow user's setting of R_HOME and b) use R's get_R_HOME() function if (getenv("R_HOME") == NULL) { // if on Windows and not set char *rhome = get_R_HOME(); // query it, including registry if (rhome != NULL) { // if something was found setenv("R_HOME", get_R_HOME(), 1); // store what we got as R_HOME } // this will now be used in next blocks } #endif for (int i = 0; R_VARS[i] != NULL; i+= 2) { if (getenv(R_VARS[i]) == NULL) { // if env variable is not yet set if (setenv(R_VARS[i],R_VARS[i+1],1) != 0){ throw std::runtime_error(std::string("Could not set R environment variable ") + std::string(R_VARS[i]) + std::string(" to ") + std::string(R_VARS[i+1])); } } } #ifndef WIN32 R_SignalHandlers = 0; // Don't let R set up its own signal handlers #endif init_tempdir(); const char *R_argv[] = {(char*)programName, "--gui=none", "--no-save", "--no-readline", "--silent", "--vanilla", "--slave"}; int R_argc = sizeof(R_argv) / sizeof(R_argv[0]); Rf_initEmbeddedR(R_argc, (char**)R_argv); #ifndef WIN32 R_CStackLimit = -1; // Don't do any stack checking, see R Exts, '8.1.5 Threading issues' #endif R_ReplDLLinit(); // this is to populate the repl console buffers structRstart Rst; R_DefParams(&Rst); Rst.R_Interactive = (Rboolean) interactive_m; // sets interactive() to eval to false #ifdef WIN32 Rst.rhome = getenv("R_HOME"); // which is set above as part of R_VARS Rst.home = getRUser(); Rst.CharacterMode = LinkDLL; Rst.ReadConsole = myReadConsole; Rst.WriteConsole = myWriteConsole; Rst.CallBack = myCallBack; Rst.ShowMessage = myAskOk; Rst.YesNoCancel = myAskYesNoCancel; Rst.Busy = myBusy; #endif R_SetParams(&Rst); if (true || loadRcpp) { // we always need Rcpp, so load it anyway // Rf_install is used best by first assigning like this so that symbols get into the symbol table // where they cannot be garbage collected; doing it on the fly does expose a minuscule risk of garbage // collection -- with thanks to Doug Bates for the explanation and Luke Tierney for the heads-up SEXP suppressMessagesSymbol = Rf_install("suppressMessages"); SEXP requireSymbol = Rf_install("require"); Rf_eval(Rf_lang2(suppressMessagesSymbol, Rf_lang2(requireSymbol, Rf_mkString("Rcpp"))), R_GlobalEnv); } global_env_m = new Rcpp::Environment(); // member variable for access to R's global environment autoloads(); // loads all default packages, using code autogenerate from Makevars{,.win} if ((argc - optind) > 1){ // for argv vector in Global Env */ Rcpp::CharacterVector s_argv( argv+(1+optind), argv+argc ); assign(s_argv, "argv"); } else { assign(R_NilValue, "argv") ; } init_rand(); // for tempfile() to work correctly */ }
// TODO: use a vector<string> would make all this a bit more readable void RInside::initialize(const int argc, const char* const argv[], const bool loadRcpp) { logTxt("RInside::ctor BEGIN", verbose); if (instance_) { throw std::runtime_error( "can only have one RInside instance" ) ; } else { instance_ = this ; } verbose_m = false; // Default is false // generated as littler.h via from svn/littler/littler.R #include <RInsideEnvVars.h> for (int i = 0; R_VARS[i] != NULL; i+= 2) { if (getenv(R_VARS[i]) == NULL) { // if env variable is not yet set if (setenv(R_VARS[i],R_VARS[i+1],1) != 0){ //perror("ERROR: couldn't set/replace an R environment variable"); //exit(1); throw std::runtime_error(std::string("Could not set R environment variable ") + std::string(R_VARS[i]) + std::string(" to ") + std::string(R_VARS[i+1])); } } } #ifndef WIN32 R_SignalHandlers = 0; // Don't let R set up its own signal handlers #endif #ifdef CSTACK_DEFNS R_CStackLimit = (uintptr_t)-1; // Don't do any stack checking, see R Exts, '8.1.5 Threading issues' #endif init_tempdir(); const char *R_argv[] = {(char*)programName, "--gui=none", "--no-save", "--no-readline", "--silent", "", ""}; const char *R_argv_opt[] = {"--vanilla", "--slave"}; int R_argc = (sizeof(R_argv) - sizeof(R_argv_opt) ) / sizeof(R_argv[0]); Rf_initEmbeddedR(R_argc, (char**)R_argv); R_ReplDLLinit(); // this is to populate the repl console buffers structRstart Rst; R_DefParams(&Rst); Rst.R_Interactive = (Rboolean) FALSE; // sets interactive() to eval to false #ifdef WIN32 Rst.rhome = getenv("R_HOME"); // which is set above as part of R_VARS Rst.home = getRUser(); Rst.CharacterMode = LinkDLL; Rst.ReadConsole = myReadConsole; Rst.WriteConsole = myWriteConsole; Rst.CallBack = myCallBack; Rst.ShowMessage = myAskOk; Rst.YesNoCancel = myAskYesNoCancel; Rst.Busy = myBusy; #endif R_SetParams(&Rst); global_env = R_GlobalEnv ; if (loadRcpp) { // if asked for, load Rcpp (before the autoloads) // Rf_install is used best by first assigning like this so that symbols get into the symbol table // where they cannot be garbage collected; doing it on the fly does expose a minuscule risk of garbage // collection -- with thanks to Doug Bates for the explanation and Luke Tierney for the heads-up SEXP suppressMessagesSymbol = Rf_install("suppressMessages"); SEXP requireSymbol = Rf_install("require"); Rf_eval(Rf_lang2(suppressMessagesSymbol, Rf_lang2(requireSymbol, Rf_mkString("Rcpp"))), R_GlobalEnv); } autoloads(); // loads all default packages if ((argc - optind) > 1){ // for argv vector in Global Env */ Rcpp::CharacterVector s_argv( argv+(1+optind), argv+argc ); assign(s_argv, "argv"); } else { assign(R_NilValue, "argv") ; } init_rand(); // for tempfile() to work correctly */ logTxt("RInside::ctor END", verbose); }