[Rinside-commits] r222 - in pkg: . inst inst/examples/standard src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 8 18:43:22 CET 2012


Author: edd
Date: 2012-01-08 18:43:21 +0100 (Sun, 08 Jan 2012)
New Revision: 222

Modified:
   pkg/ChangeLog
   pkg/inst/NEWS
   pkg/inst/examples/standard/Makefile.win
   pkg/src/RInside.cpp
Log:
correction to Windows init
update to internal RNG seed, copying R's new behaviour
small update to Windows makefile


Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2011-12-09 02:19:19 UTC (rev 221)
+++ pkg/ChangeLog	2012-01-08 17:43:21 UTC (rev 222)
@@ -1,3 +1,14 @@
+2012-01-08  Dirk Eddelbuettel  <edd at dexter>
+
+	* src/RInside.cpp: Correct console writer on Windows to not use
+	Rprintf (with thanks to both James Bates and John Brzustowski)
+
+	* src/RInside.cpp: Update RNG seeding to same scheme now used by
+	R which blends both (millisecond) time and process id
+
+	* inst/examples/standard/Makefile.win: Allow for an R_ARCH variable
+	to be set to enforce either i386 or x64 builds
+
 2011-12-06  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/RInside.cpp: Applied (a slightly simpler versions of) a patch
@@ -7,11 +18,11 @@
 	that we get some symbols from Rembedded.h
 
 	* inst/THANKS: Add James Bates for help with RInside on Windows
-	
+
 	* doxyfile: Add inst/examples/wt to example path
 
 	* cleanup: Some fixes ensuring qt and wt example can be cleaned
-	
+
 	* .Rbuildignore: Add some local files from qt and wt examples
 
 2011-12-04  Dirk Eddelbuettel  <edd at debian.org>

Modified: pkg/inst/NEWS
===================================================================
--- pkg/inst/NEWS	2011-12-09 02:19:19 UTC (rev 221)
+++ pkg/inst/NEWS	2012-01-08 17:43:21 UTC (rev 222)
@@ -1,5 +1,16 @@
-0.2.5   20xx-yy-zz
+0.2.6   2012-01-xx
 
+   o    Correct Windows initialization by not using Rprintf in internasl
+        console writer, with thanks to both James Bates and John Brzustowski
+
+   o    Update RNG seeding (used by tmpnam et al) to same scheme used by
+        R since 2.14.0: blending both millisecond time and process id
+
+   o    Small update to standard examples Makefile for Windows permitting 
+        to explicitly set i386 or x64 as a build architecture
+
+0.2.5   2011-12-06
+
    o    Applied (somewhat simplified) patch by James Bates which restores
         RInside to working on Windows -- with a big Thank You! to James for
         fixing a long-standing bug we inadvertendly introduced right after

Modified: pkg/inst/examples/standard/Makefile.win
===================================================================
--- pkg/inst/examples/standard/Makefile.win	2011-12-09 02:19:19 UTC (rev 221)
+++ pkg/inst/examples/standard/Makefile.win	2012-01-08 17:43:21 UTC (rev 222)
@@ -1,43 +1,43 @@
-## -*- mode: makefile; tab-width: 8; -*-
+n## -*- mode: makefile; tab-width: 8; -*-
 ##
-## Simple Makefile
-##
-## TODO:
-##  proper configure for non-Debian file locations,   [ Done ]
-##  allow RHOME to be set for non-default R etc
+## Simple Makefile for Windows
 
 ## This version is fairly directly derived from the Unix versions
 ## You may have to set R_HOME manually if this does not work
 ## It requires Rtools in the path -- as does all R package building
 R_HOME := 		$(shell R RHOME | sed -e "s|\\\\|\/|g")
 
+## You may have to set this to one of the two values to enforce a particular architecture
+#R_ARCH :=		--arch i386
+#R_ARCH :=		--arch x64
+
 sources := 		$(wildcard *.cpp)
 programs := 		$(sources:.cpp=)
 
-
 ## include headers and libraries for R
-RCPPFLAGS := 		$(shell $(R_HOME)/bin/R CMD config --cppflags)
-RLDFLAGS := 		$(shell $(R_HOME)/bin/R CMD config --ldflags)
-RBLAS := 		$(shell $(R_HOME)/bin/R CMD config BLAS_LIBS)
-RLAPACK := 		$(shell $(R_HOME)/bin/R CMD config LAPACK_LIBS)
+RCPPFLAGS := 		$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config --cppflags)
+RLDFLAGS := 		$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config --ldflags)
+RBLAS := 		$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config BLAS_LIBS)
+RLAPACK := 		$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config LAPACK_LIBS)
 
 
 ## include headers and libraries for Rcpp interface classes
-RCPPINCL := 		$(shell echo 'Rcpp:::CxxFlags()' | $(R_HOME)/bin/R --vanilla --slave)
-RCPPLIBS := 		$(shell echo 'Rcpp:::LdFlags()'  | $(R_HOME)/bin/R --vanilla --slave)
+RCPPINCL := 		$(shell echo 'Rcpp:::CxxFlags()' | $(R_HOME)/bin/R $(R_ARCH) --vanilla --slave)
+RCPPLIBS := 		$(shell echo 'Rcpp:::LdFlags()'  | $(R_HOME)/bin/R $(R_ARCH) --vanilla --slave)
 
 
 ## include headers and libraries for RInside embedding classes
-RINSIDEINCL := 		$(shell echo 'RInside:::CxxFlags()' | $(R_HOME)/bin/R --vanilla --slave)
-RINSIDELIBS := 		$(shell echo 'RInside:::LdFlags()'  | $(R_HOME)/bin/R --vanilla --slave)
+RINSIDEINCL := 		$(shell echo 'RInside:::CxxFlags()' | $(R_HOME)/bin/R $(R_ARCH) --vanilla --slave)
+RINSIDELIBS := 		$(shell echo 'RInside:::LdFlags()'  | $(R_HOME)/bin/R $(R_ARCH) --vanilla --slave)
 
+
 ## compiler etc settings used in default make rules
-CXX := 			$(shell $(R_HOME)/bin/R CMD config CXX)
-CPPFLAGS := 		-Wall $(shell $(R_HOME)/bin/R CMD config CPPFLAGS)
-CXXFLAGS := 		$(RCPPFLAGS) $(RCPPINCL) $(RINSIDEINCL) $(shell $(R_HOME)/bin/R CMD config CXXFLAGS)
+CXX := 			$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config CXX)
+CPPFLAGS := 		-Wall $(shell $(R_HOME)/bin/R $(R_ARCH) CMD config CPPFLAGS)
+CXXFLAGS := 		$(RCPPFLAGS) $(RCPPINCL) $(RINSIDEINCL) $(shell $(R_HOME)/bin/R $(R_ARCH) CMD config CXXFLAGS)
 LDFLAGS	= 		-s
-LDLIBS := 		$(RLDFLAGS) $(RBLAS) $(RLAPACK) $(RINSIDELIBS) $(RCPPLIBS) 
-CC := 			$(shell $(R_HOME)/bin/R CMD config CXX)
+LDLIBS := 		$(RLDFLAGS) $(RBLAS) $(RLAPACK) $(RINSIDELIBS) $(RCPPLIBS)
+CC := 			$(shell $(R_HOME)/bin/R $(R_ARCH) CMD config CXX)
 
 
 all : 			$(programs)
@@ -47,4 +47,4 @@
 
 
 checkR:
-			echo "R is at $(R_HOME)"
\ No newline at end of file
+			echo "R is at $(R_HOME)"

Modified: pkg/src/RInside.cpp
===================================================================
--- pkg/src/RInside.cpp	2011-12-09 02:19:19 UTC (rev 221)
+++ pkg/src/RInside.cpp	2012-01-08 17:43:21 UTC (rev 222)
@@ -2,8 +2,8 @@
 //
 // RInside.cpp: R/C++ interface class library -- Easier R embedding into C++
 //
-// Copyright (C) 2009        Dirk Eddelbuettel
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2009         Dirk Eddelbuettel
+// Copyright (C) 2010 - 2012  Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of RInside.
 //
@@ -26,6 +26,7 @@
 RInside* RInside::instance_ = 0 ;
 
 #include <sys/time.h>           // gettimeofday
+#include <stdint.h>		// uint64_t
 
 bool verbose = false;
 const char *programName = "RInside";
@@ -50,8 +51,8 @@
     instance_ = 0 ;
 }
 
-RInside::RInside() 
-#ifdef RINSIDE_CALLBACKS 
+RInside::RInside()
+#ifdef RINSIDE_CALLBACKS
     : callbacks(0)
 #endif
 {
@@ -62,53 +63,54 @@
 static int myReadConsole(const char *prompt, char *buf, int len, int addtohistory) {
     fputs(prompt, stdout);
     fflush(stdout);
-    if (fgets(buf, len, stdin)) 
+    if (fgets(buf, len, stdin))
         return 1;
-    else 
+    else
         return 0;
 }
- 
+
 static void myWriteConsole(const char *buf, int len) {
-     Rprintf("%s", buf);
+    fwrite(buf, sizeof(char), len, stdout);
+    fflush(stdout);
 }
- 
+
 static void myCallBack() {
     /* called during i/o, eval, graphics in ProcessEvents */
 }
- 
+
 static void myBusy(int which) {
     /* set a busy cursor ... if which = 1, unset if which = 0 */
 }
- 
+
 void myAskOk(const char *info) {
- 
+
 }
 
 int myAskYesNoCancel(const char *question) {
     const int yes = 1;
     return yes;
 }
- 
+
 #endif
 
 RInside::RInside(const int argc, const char* const argv[], const bool loadRcpp)
-#ifdef RINSIDE_CALLBACKS 
+#ifdef RINSIDE_CALLBACKS
 : callbacks(0)
-#endif 
+#endif
 {
-    initialize( argc, argv, loadRcpp ); 
+    initialize( argc, argv, loadRcpp );
 }
 
-// TODO: use a vector<string> would make all this a bit more readable 
+// 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 ;      
+        instance_ = this ;
     }
-    
+
     verbose_m = false;          // Default is false
 
     // generated as littler.h via from svn/littler/littler.R
@@ -120,7 +122,7 @@
                 //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]) + std::string(" to ") +
                                          std::string(R_VARS[i+1]));
             }
         }
@@ -131,7 +133,7 @@
     #endif
 
     #ifdef CSTACK_DEFNS
-    R_CStackLimit = (uintptr_t)-1;      // Don't do any stack checking, see R Exts, '8.1.5 Threading issues' 
+    R_CStackLimit = (uintptr_t)-1;      // Don't do any stack checking, see R Exts, '8.1.5 Threading issues'
     #endif
 
     init_tempdir();
@@ -141,13 +143,13 @@
     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 
+    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 
+    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.rhome = getenv("R_HOME");       // which is set above as part of R_VARS
     Rst.home = getRUser();
     Rst.CharacterMode = LinkDLL;
     Rst.ReadConsole = myReadConsole;
@@ -160,10 +162,10 @@
     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  
+        // 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");
@@ -178,7 +180,7 @@
     } else {
         assign(R_NilValue, "argv") ;
     }
-  
+
     init_rand();                        // for tempfile() to work correctly */
     logTxt("RInside::ctor END", verbose);
 }
@@ -189,9 +191,9 @@
     tmp = getenv("TMPDIR");
     if (tmp == NULL) {
         tmp = getenv("TMP");
-        if (tmp == NULL) { 
+        if (tmp == NULL) {
             tmp = getenv("TEMP");
-            if (tmp == NULL) 
+            if (tmp == NULL)
                 tmp = "/tmp";
             }
     }
@@ -203,19 +205,19 @@
     }
 }
 
-void RInside::init_rand(void) { /* set seed for tempfile()  */
-    unsigned int seed;
-    struct timeval tv;
+void RInside::init_rand(void) { 		// code borrows from R's TimeToSeed() in datetime.c
+    unsigned int pid = getpid();
+    struct timeval tv;          		// this is ifdef'ed by R, we just assume we have it 
     gettimeofday (&tv, NULL);
-    // changed uint64_t to unsigned int. Need to figure out why BDR used that instead. 
-    seed = ((unsigned int) tv.tv_usec << 16) ^ tv.tv_sec;
+    unsigned int seed = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
+    seed ^= (pid << 16);         		// R 2.14.0 started to also use pid to support parallel 
     srand(seed);
 }
 
 void RInside::autoloads() {
 
-    #include <RInsideAutoloads.h>  
- 
+    #include <RInsideAutoloads.h>
+
     // Autoload default packages and names from autoloads.h
     //
     // This function behaves in almost every way like
@@ -236,66 +238,66 @@
     //     invisible()
     // }
     //
-    // What's missing is the updating of the string vector .Autoloaded with 
-    // the list of packages, which by my code analysis is useless and only 
+    // What's missing is the updating of the string vector .Autoloaded with
+    // the list of packages, which by my code analysis is useless and only
     // for informational purposes.
     //
     //
-    
-    // we build the call : 
+
+    // we build the call :
     //
-    //  delayedAssign( NAME, 
-    //          autoloader( name = NAME, package = PACKAGE), 
-    //          .GlobalEnv, 
+    //  delayedAssign( NAME,
+    //          autoloader( name = NAME, package = PACKAGE),
+    //          .GlobalEnv,
     //          .AutoloadEnv )
-    //          
-    //  where : 
+    //
+    //  where :
     //  - PACKAGE is updated in a loop
     //  - NAME is updated in a loop
-    //  
     //
-     
+    //
+
     int i,j, idx=0, nobj ;
-    Rcpp::Language delayed_assign_call(Rcpp::Function("delayedAssign"), 
-                                       R_NilValue,     // arg1: assigned in loop 
-                                       R_NilValue,     // arg2: assigned in loop 
+    Rcpp::Language delayed_assign_call(Rcpp::Function("delayedAssign"),
+                                       R_NilValue,     // arg1: assigned in loop
+                                       R_NilValue,     // arg2: assigned in loop
                                        global_env,
                                        global_env.find(".AutoloadEnv")
                                        );
     Rcpp::Language::Proxy delayed_assign_name  = delayed_assign_call[1];
 
     Rcpp::Language autoloader_call(Rcpp::Function("autoloader"),
-                                   Rcpp::Named( "name", R_NilValue) ,  // arg1 : assigned in loop 
-                                   Rcpp::Named( "package", R_NilValue) // arg2 : assigned in loop 
+                                   Rcpp::Named( "name", R_NilValue) ,  // arg1 : assigned in loop
+                                   Rcpp::Named( "package", R_NilValue) // arg2 : assigned in loop
                                    );
     Rcpp::Language::Proxy autoloader_name = autoloader_call[1];
     Rcpp::Language::Proxy autoloader_pack = autoloader_call[2];
     delayed_assign_call[2] = autoloader_call;
-    
-    try { 
+
+    try {
         for( i=0; i<packc; i++){
-                
+
             // set the 'package' argument of the autoloader call */
             autoloader_pack = pack[i] ;
-                
-            nobj = packobjc[i] ; 
+
+            nobj = packobjc[i] ;
             for (j = 0; j < nobj ; j++){
-                
-                // set the 'name' argument of the autoloader call */ 
+
+                // set the 'name' argument of the autoloader call */
                 autoloader_name = packobj[idx+j] ;
-                   
+
                 // Set the 'name' argument of the delayedAssign call */
                 delayed_assign_name = packobj[idx+j] ;
-                    
+
                 // evaluate the call */
                 delayed_assign_call.eval() ;
-                    
+
             }
             idx += packobjc[i] ;
         }
     } catch( std::exception& ex){
         // fprintf(stderr,"%s: Error calling delayedAssign:\n %s", programName, ex.what() );
-        // exit(1);         
+        // exit(1);
         throw std::runtime_error(std::string("Error calling delayedAssign: ") + std::string(ex.what()));
     }
 }
@@ -307,7 +309,7 @@
     int i, errorOccurred;
 
     mb_m.add((char*)line.c_str());
-    
+
     PROTECT(cmdSexp = Rf_allocVector(STRSXP, 1));
     SET_STRING_ELT(cmdSexp, 0, Rf_mkChar(mb_m.getBufPtr()));
 
@@ -315,7 +317,7 @@
 
     switch (status){
     case PARSE_OK:
-        // Loop is needed here as EXPSEXP might be of length > 1 
+        // Loop is needed here as EXPSEXP might be of length > 1
         for(i = 0; i < Rf_length(cmdexpr); i++){
             ans = R_tryEval(VECTOR_ELT(cmdexpr, i),NULL,&errorOccurred);
             if (errorOccurred) {
@@ -330,7 +332,7 @@
         mb_m.rewind();
         break;
     case PARSE_INCOMPLETE:
-        // need to read another line 
+        // need to read another line
         break;
     case PARSE_NULL:
         fprintf(stderr, "%s: ParseStatus is null (%d)\n", programName, status);
@@ -373,7 +375,7 @@
 }
 
 Rcpp::Environment::Binding RInside::operator[]( const std::string& name ){
-    return global_env[name]; 
+    return global_env[name];
 }
 
 RInside& RInside::instance(){
@@ -386,23 +388,23 @@
 
 void Callbacks::Busy_( int which ){
     R_is_busy = static_cast<bool>( which ) ;
-    Busy( R_is_busy ) ; 
+    Busy( R_is_busy ) ;
 }
 
 int Callbacks::ReadConsole_( const char* prompt, unsigned char* buf, int len, int addtohistory ){
     try {
         std::string res( ReadConsole( prompt, static_cast<bool>(addtohistory) ) ) ;
-                
+
         /* At some point we need to figure out what to do if the result is
          * longer than "len"... For now, just truncate. */
-                 
+
         int l = res.size() ;
         int last = (l>len-1)?len-1:l ;
         strncpy( (char*)buf, res.c_str(), last ) ;
         buf[last] = 0 ;
         return 1 ;
     } catch( const std::exception& ex){
-        return -1 ;     
+        return -1 ;
     }
 }
 
@@ -415,11 +417,11 @@
 }
 
 void RInside_ShowMessage( const char* message ){
-    RInside::instance().callbacks->ShowMessage( message ) ;     
+    RInside::instance().callbacks->ShowMessage( message ) ;
 }
 
 void RInside_WriteConsoleEx( const char* message, int len, int oType ){
-    RInside::instance().callbacks->WriteConsole_( message, len, oType ) ;               
+    RInside::instance().callbacks->WriteConsole_( message, len, oType ) ;
 }
 
 int RInside_ReadConsole(const char *prompt, unsigned char *buf, int len, int addtohistory){
@@ -430,7 +432,7 @@
     RInside::instance().callbacks->ResetConsole() ;
 }
 
-void RInside_FlushConsole(){                                       
+void RInside_FlushConsole(){
     RInside::instance().callbacks->FlushConsole() ;
 }
 
@@ -444,7 +446,7 @@
 
 void RInside::set_callbacks(Callbacks* callbacks_){
     callbacks = callbacks_ ;
-        
+
 #ifdef WIN32
     // do something to tell user that he doesn't get this
 #else
@@ -472,10 +474,10 @@
     if( callbacks->has_Busy() ){
         ptr_R_Busy = RInside_Busy;
     }
-    
+
     R_Outputfile = NULL;
-    R_Consolefile = NULL;    
-#endif    
+    R_Consolefile = NULL;
+#endif
 }
 
 void RInside::repl(){



More information about the Rinside-commits mailing list