26 #define R_INTERFACE_PTRS 27 #include <Rinterface.h> 45 R_RunExitFinalizers();
57 #ifdef RINSIDE_CALLBACKS
65 static int myReadConsole(
const char *prompt,
char *buf,
int len,
int addtohistory) {
66 fputs(prompt, stdout);
68 if (fgets(buf, len, stdin))
74 static void myWriteConsole(
const char *buf,
int len) {
75 fwrite(buf,
sizeof(
char), len, stdout);
79 static void myCallBack() {
83 static void myBusy(
int which) {
87 void myAskOk(
const char *info) {
91 int myAskYesNoCancel(
const char *question) {
99 const bool verbose,
const bool interactive)
100 #ifdef RINSIDE_CALLBACKS 104 initialize(argc, argv, loadRcpp, verbose, interactive);
109 const bool verbose,
const bool interactive) {
112 throw std::runtime_error(
"can only have one RInside instance" ) ;
121 #include "RInsideEnvVars.h" 127 if (getenv(
"R_HOME") == NULL) {
128 char *rhome = get_R_HOME();
130 setenv(
"R_HOME", get_R_HOME(), 1);
135 for (
int i = 0; R_VARS[i] != NULL; i+= 2) {
136 if (getenv(R_VARS[i]) == NULL) {
137 if (
setenv(R_VARS[i],R_VARS[i+1],1) != 0){
138 throw std::runtime_error(std::string(
"Could not set R environment variable ") +
139 std::string(R_VARS[i]) + std::string(
" to ") +
140 std::string(R_VARS[i+1]));
146 R_SignalHandlers = 0;
151 const char *R_argv[] = {(
char*)
programName,
"--gui=none",
"--no-save",
152 "--silent",
"--vanilla",
"--slave",
"--no-readline"};
153 int R_argc =
sizeof(R_argv) /
sizeof(R_argv[0]);
155 Rf_initEmbeddedR(R_argc, (
char**)R_argv);
168 char *temp = getenv(
"R_HOME");
169 strncpy(rHome, temp, MAX_PATH);
172 Rst.home = getRUser();
173 Rst.CharacterMode = LinkDLL;
174 Rst.ReadConsole = myReadConsole;
175 Rst.WriteConsole = myWriteConsole;
176 Rst.CallBack = myCallBack;
177 Rst.ShowMessage = myAskOk;
178 Rst.YesNoCancel = myAskYesNoCancel;
183 if (
true || loadRcpp) {
188 SEXP suppressMessagesSymbol = Rf_install(
"suppressMessages");
189 SEXP requireSymbol = Rf_install(
"require");
190 SEXP reqsymlang, langobj;
192 PROTECT(reqsymlang = Rf_lang2(requireSymbol, Rf_mkString(
"Rcpp")));
193 PROTECT(langobj = Rf_lang2(suppressMessagesSymbol, reqsymlang));
194 Rf_eval(langobj, R_GlobalEnv);
202 if ((argc - optind) > 1){
203 Rcpp::CharacterVector s_argv( argv+(1+optind), argv+argc );
206 assign(R_NilValue,
"argv") ;
215 tmp = getenv(
"TMPDIR");
219 tmp = getenv(
"TEMP");
224 R_TempDir = (
char*) tmp;
225 if (
setenv(
"R_SESSION_TMPDIR",tmp,1) != 0){
226 throw std::runtime_error(std::string(
"Could not set / replace R_SESSION_TMPDIR to ") + std::string(tmp));
231 unsigned int pid = getpid();
233 gettimeofday (&tv, NULL);
234 unsigned int seed = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
241 #include "RInsideAutoloads.h" 282 int i,j, idx=0, nobj ;
283 Rcpp::Language delayed_assign_call(Rcpp::Function(
"delayedAssign"),
289 Rcpp::Language::Proxy delayed_assign_name = delayed_assign_call[1];
291 Rcpp::Language autoloader_call(Rcpp::Function(
"autoloader"),
292 Rcpp::Named(
"name", R_NilValue) ,
293 Rcpp::Named(
"package", R_NilValue)
295 Rcpp::Language::Proxy autoloader_name = autoloader_call[1];
296 Rcpp::Language::Proxy autoloader_pack = autoloader_call[2];
297 delayed_assign_call[2] = autoloader_call;
300 for( i=0; i<packc; i++){
303 autoloader_pack = pack[i] ;
306 for (j = 0; j < nobj ; j++){
309 autoloader_name = packobj[idx+j] ;
312 delayed_assign_name = packobj[idx+j] ;
315 delayed_assign_call.eval() ;
320 }
catch( std::exception& ex){
321 throw std::runtime_error(std::string(
"Error calling delayedAssign: ") + std::string(ex.what()));
328 SEXP cmdSexp, cmdexpr = R_NilValue;
329 int i, errorOccurred;
333 PROTECT(cmdSexp = Rf_allocVector(STRSXP, 1));
336 cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &status, R_NilValue));
341 for(i = 0; i < Rf_length(cmdexpr); i++){
342 ans = R_tryEval(VECTOR_ELT(cmdexpr, i), *
global_env_m, &errorOccurred);
355 case PARSE_INCOMPLETE:
365 if (
verbose_m) Rf_warning(
"Parse Error: \"%s\"\n", line.c_str());
388 throw std::runtime_error(std::string(
"Error evaluating: ") + line);
401 throw std::runtime_error(std::string(
"Error evaluating: ") + line);
426 while (R_ReplDLLdo1() > 0) {}
431 #ifdef RINSIDE_CALLBACKS 433 void Callbacks::Busy_(
int which ){
434 R_is_busy =
static_cast<bool>( which ) ;
438 int Callbacks::ReadConsole_(
const char* prompt,
unsigned char* buf,
int len,
int addtohistory ){
440 std::string res( ReadConsole( prompt, static_cast<bool>(addtohistory) ) ) ;
446 int last = (l>len-1)?len-1:l ;
447 strncpy( (
char*)buf, res.c_str(), last ) ;
450 }
catch(
const std::exception& ex){
456 void Callbacks::WriteConsole_(
const char* buf,
int len,
int oType ){
458 buffer.assign( buf, len ) ;
459 WriteConsole( buffer, oType) ;
463 void RInside_ShowMessage(
const char* message ){
467 void RInside_WriteConsoleEx(
const char* message,
int len,
int oType ){
471 int RInside_ReadConsole(
const char *prompt,
unsigned char *buf,
int len,
int addtohistory){
472 return RInside::instance().callbacks->ReadConsole_( prompt, buf, len, addtohistory ) ;
475 void RInside_ResetConsole(){
479 void RInside_FlushConsole(){
483 void RInside_ClearerrConsole(){
487 void RInside_Busy(
int which ){
491 void RInside::set_callbacks(Callbacks* callbacks_){
492 callbacks = callbacks_ ;
499 if( callbacks->has_ShowMessage() ){
500 ptr_R_ShowMessage = RInside_ShowMessage ;
502 if( callbacks->has_ReadConsole() ){
503 ptr_R_ReadConsole = RInside_ReadConsole;
505 if( callbacks->has_WriteConsole() ){
506 ptr_R_WriteConsoleEx = RInside_WriteConsoleEx ;
507 ptr_R_WriteConsole = NULL;
509 if( callbacks->has_ResetConsole() ){
510 ptr_R_ResetConsole = RInside_ResetConsole;
512 if( callbacks->has_FlushConsole() ){
513 ptr_R_FlushConsole = RInside_FlushConsole;
515 if( callbacks->has_CleanerrConsole() ){
516 ptr_R_ClearerrConsole = RInside_ClearerrConsole;
518 if( callbacks->has_Busy() ){
519 ptr_R_Busy = RInside_Busy;
523 R_Consolefile = NULL;
Rcpp::Environment::Binding operator[](const std::string &name)
void parseEvalQ(const std::string &line)
static RInside * instancePtr()
void add(const std::string &)
static RInside * instance_m
int setenv(const char *env_var, const char *env_val, int dummy)
int parseEval(const std::string &line, SEXP &ans)
Proxy parseEvalNT(const std::string &line)
void parseEvalQNT(const std::string &line)
void initialize(const int argc, const char *const argv[], const bool loadRcpp, const bool verbose, const bool interactive)
Rcpp::Environment * global_env_m
void assign(const T &object, const std::string &nam)
static RInside & instance()