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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Oct 16 03:26:20 CEST 2012


Author: edd
Date: 2012-10-16 03:26:19 +0200 (Tue, 16 Oct 2012)
New Revision: 264

Added:
   pkg/inst/examples/standard/rinside_sample13.cpp
   pkg/inst/examples/standard/rinside_test2.cpp
Modified:
   pkg/ChangeLog
   pkg/DESCRIPTION
   pkg/inst/include/RInside.h
   pkg/src/RInside.cpp
Log:
two new booleans as constructor arguments to set 'verbose' and 'interactive'
new example


Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2012-10-12 00:48:11 UTC (rev 263)
+++ pkg/ChangeLog	2012-10-16 01:26:19 UTC (rev 264)
@@ -1,3 +1,13 @@
+2012-10-15  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/include/RInside.h: Added two new boolean arguments to
+	constructor to set verbose and/or interactive mode; verbose mode can
+	also be changed via a setter function.
+	* src/RInside.cpp: Corresponding implementation
+
+	* inst/examples/standard/rinside_sample13.cpp: New example
+	illustrating the new more fault-tolerant modes
+
 2012-10-11  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/include/RInside.h: Added non-throwing variants of parseEval()
@@ -14,6 +24,11 @@
 	* src/RInside.cpp: Applied (modified) patch by Theodore Lytras which
 	lets us recover from parsing errors on erroneous input lines
 
+2012-10-05  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/examples/standard/rinside_test2.cpp: Simple new test to check
+	the search path
+
 2012-09-15  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/RInside.cpp (initialize): On Windows, if R_HOME is not set as

Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2012-10-12 00:48:11 UTC (rev 263)
+++ pkg/DESCRIPTION	2012-10-16 01:26:19 UTC (rev 264)
@@ -1,6 +1,6 @@
 Package: RInside
 Title: C++ classes to embed R in C++ applications
-Version: 0.2.8.1
+Version: 0.2.8.2
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois
 Maintainer: Dirk Eddelbuettel <edd at debian.org>

Added: pkg/inst/examples/standard/rinside_sample13.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_sample13.cpp	                        (rev 0)
+++ pkg/inst/examples/standard/rinside_sample13.cpp	2012-10-16 01:26:19 UTC (rev 264)
@@ -0,0 +1,34 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// Triggering errors, and surviving to tell the tale
+//
+// Copyright (C) 2012  Dirk Eddelbuettel and GPL'ed 
+
+#include <RInside.h>                    // for the embedded R via RInside
+
+int main(int argc, char *argv[]) {
+
+    RInside R(argc, argv);              // create an embedded R instance 
+    
+    try {
+        std::string cmd = "cat(doesNotExist))"; // simple parse error due to double "))"
+        R.parseEvalQNT(cmd); 		        // eval quietly, does not throw on error
+        					// parseEvalQ would throw on the error
+
+        cmd = "try(cat(doesNotExist))"; 	// works also as the try() moderates
+        R.parseEvalQ(cmd); 		        // eval quietly, no error thrown
+        					// without try() we'd have an error and exit
+
+        cmd = "cat(\"End of main part\\n\")";
+        R.parseEval(cmd); 		        // eval the string, ignoring any returns
+
+    } catch( std::exception &ex ) {
+	std::cerr << "Exception caught: " << ex.what() << std::endl;
+    } catch(...) { 
+	std::cerr << "C++ exception (unknown reason)" << std::endl;
+    }
+    std::cout << "All done, past catch()\n";
+
+    exit(0);
+}
+

Added: pkg/inst/examples/standard/rinside_test2.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_test2.cpp	                        (rev 0)
+++ pkg/inst/examples/standard/rinside_test2.cpp	2012-10-16 01:26:19 UTC (rev 264)
@@ -0,0 +1,18 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// Show the search path to check if package methods is loaded
+//
+// Copyright (C) 2012  Dirk Eddelbuettel and GPL'ed 
+
+#include <RInside.h>                    // for the embedded R via RInside
+
+int main(int argc, char *argv[]) {
+
+    RInside R(argc, argv);              // create an embedded R instance 
+    
+    std::string cmd = "print(search())";
+    R.parseEval(cmd); 		        // eval the init string, ignoring any returns
+
+    exit(0);
+}
+

Modified: pkg/inst/include/RInside.h
===================================================================
--- pkg/inst/include/RInside.h	2012-10-12 00:48:11 UTC (rev 263)
+++ pkg/inst/include/RInside.h	2012-10-16 01:26:19 UTC (rev 264)
@@ -31,31 +31,30 @@
     MemBuf mb_m;
     Rcpp::Environment global_env_m;
     
-    bool verbose_m;				// private switch
+    bool verbose_m;							// switch toggled by constructor, or setter
+	bool interactive_m;						// switch set by constructor only
 
     void init_tempdir(void);
     void init_rand(void);
     void autoloads(void);
     
-    void initialize(const int argc, const char* const argv[], const bool loadRcpp ) ;
+    void initialize(const int argc, const char* const argv[], 
+					const bool loadRcpp, const bool verbose, const bool interactive);
 
-    static RInside* instance_ ;
+    static RInside* instance_m ;
     
 #ifdef RINSIDE_CALLBACKS
     Callbacks* callbacks ;
-    friend void RInside_ShowMessage( const char* message) ;
-    friend void RInside_WriteConsoleEx( const char* message, int len, int oType ) ;
-    friend int RInside_ReadConsole(const char *prompt, unsigned char *buf, int len, int addtohistory) ;
-    friend void RInside_ResetConsole() ;
-    friend void RInside_FlushConsole() ;
-    friend void RInside_ClearerrConsole() ;
-    friend void RInside_Busy(int which) ;
+    friend void RInside_ShowMessage( const char* message);
+    friend void RInside_WriteConsoleEx( const char* message, int len, int oType );
+    friend int RInside_ReadConsole(const char *prompt, unsigned char *buf, int len, int addtohistory);
+    friend void RInside_ResetConsole();
+    friend void RInside_FlushConsole();
+    friend void RInside_ClearerrConsole();
+    friend void RInside_Busy(int which);
 #endif 
 
 public:
-    int  parseEval(const std::string & line, SEXP &ans); // parse line, return in ans; error code rc
-    void parseEvalQ(const std::string & line);		  	// parse line, no return (throws on error)
-    void parseEvalQNT(const std::string & line);		// parse line, no return (no throw)
 
     class Proxy {
 	public:
@@ -69,8 +68,11 @@
 	    Rcpp::RObject x;
 	};
 
-    Proxy parseEval(const std::string & line);		 	// parse line, return SEXP (throws on error)
-    Proxy parseEvalNT(const std::string & line);		// parse line, return SEXP (no throw)
+    int  parseEval(const std::string &line, SEXP &ans); // parse line, return in ans; error code rc
+    void parseEvalQ(const std::string &line);			// parse line, no return (throws on error)
+    void parseEvalQNT(const std::string &line);			// parse line, no return (no throw)
+    Proxy parseEval(const std::string &line);		 	// parse line, return SEXP (throws on error)
+    Proxy parseEvalNT(const std::string &line);			// parse line, return SEXP (no throw)
 
     template <typename T> 
     void assign(const T& object, const std::string& nam) {
@@ -78,8 +80,11 @@
     }
     
     RInside() ;
-    RInside(const int argc, const char* const argv[], const bool loadRcpp=false);
+    RInside(const int argc, const char* const argv[], 
+			const bool loadRcpp=false, const bool verbose=false, const bool interactive=false);
     ~RInside();
+
+	void setVerbose(const bool verbose) 	{ verbose_m = verbose; }
     
     Rcpp::Environment::Binding operator[]( const std::string& name ) ;
     

Modified: pkg/src/RInside.cpp
===================================================================
--- pkg/src/RInside.cpp	2012-10-12 00:48:11 UTC (rev 263)
+++ pkg/src/RInside.cpp	2012-10-16 01:26:19 UTC (rev 264)
@@ -23,15 +23,13 @@
 #include <RInside.h>
 #include <Callbacks.h>
 
-RInside* RInside::instance_ = 0 ;
+RInside* RInside::instance_m = 0 ;
 
 #include <sys/time.h>           // gettimeofday()
 #include <stdint.h>		// uint64_t
 #include <sys/types.h>		// pid_t
 #include <unistd.h>		// getpid()
 
-
-bool verbose = false;
 const char *programName = "RInside";
 
 #ifdef WIN32
@@ -41,7 +39,6 @@
 #endif
 
 RInside::~RInside() {           // now empty as MemBuf is internal
-    logTxt("RInside::dtor BEGIN", verbose);
     R_dot_Last();
     R_RunExitFinalizers();
     R_CleanTempDir();
@@ -50,8 +47,7 @@
     //fpu_setup(FALSE);
     //#endif
     Rf_endEmbeddedR(0);
-    logTxt("RInside::dtor END", verbose);
-    instance_ = 0 ;
+    instance_m = 0 ;
 }
 
 RInside::RInside()
@@ -59,7 +55,7 @@
     : callbacks(0)
 #endif
 {
-    initialize( 0, 0, false );
+    initialize(0, 0, false, false, false);
 }
 
 #ifdef WIN32
@@ -96,25 +92,27 @@
 
 #endif
 
-RInside::RInside(const int argc, const char* const argv[], const bool loadRcpp)
+RInside::RInside(const int argc, const char* const argv[], const bool loadRcpp,
+                 const bool verbose, const bool interactive)
 #ifdef RINSIDE_CALLBACKS
 : callbacks(0)
 #endif
 {
-    initialize( argc, argv, loadRcpp );
+    initialize(argc, argv, loadRcpp, verbose, interactive);
 }
 
 // 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);
+void RInside::initialize(const int argc, const char* const argv[], const bool loadRcpp, 
+                         const bool verbose, const bool interactive) {
 
-    if (instance_) {
+    if (instance_m) {
         throw std::runtime_error( "can only have one RInside instance" ) ;
     } else {
-        instance_ = this ;
+        instance_m = this ;
     }
 
-    verbose_m = false;          // Default is false
+    verbose_m = verbose;          	// Default is false
+    interactive_m = interactive;
 
     // generated from Makevars{.win}
     #include "RInsideEnvVars.h"
@@ -160,7 +158,7 @@
 
     structRstart Rst;
     R_DefParams(&Rst);
-    Rst.R_Interactive = (Rboolean) FALSE;       // sets interactive() to eval to false
+    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();
@@ -195,7 +193,6 @@
     }
 
     init_rand();                        // for tempfile() to work correctly */
-    logTxt("RInside::ctor END", verbose);
 }
 
 void RInside::init_tempdir(void) {
@@ -330,7 +327,7 @@
         for(i = 0; i < Rf_length(cmdexpr); i++){
             ans = R_tryEval(VECTOR_ELT(cmdexpr, i), global_env_m, &errorOccurred);
             if (errorOccurred) {
-                Rf_warning("%s: Error in evaluating R code (%d)\n", programName, status);
+                if (verbose_m) Rf_warning("%s: Error in evaluating R code (%d)\n", programName, status);
                 UNPROTECT(2);
                 mb_m.rewind();
                 return 1;
@@ -345,22 +342,22 @@
         // need to read another line
         break;
     case PARSE_NULL:
-        Rf_warning("%s: ParseStatus is null (%d)\n", programName, status);
+        if (verbose_m) Rf_warning("%s: ParseStatus is null (%d)\n", programName, status);
         UNPROTECT(2);
         mb_m.rewind();
         return 1;
         break;
     case PARSE_ERROR:
-        Rf_warning("Parse Error: \"%s\"\n", line.c_str());
+        if (verbose_m) Rf_warning("Parse Error: \"%s\"\n", line.c_str());
         UNPROTECT(2);
         mb_m.rewind();
         return 1;
         break;
     case PARSE_EOF:
-        Rf_warning("%s: ParseStatus is eof (%d)\n", programName, status);
+        if (verbose_m) Rf_warning("%s: ParseStatus is eof (%d)\n", programName, status);
         break;
     default:
-        Rf_warning("%s: ParseStatus is not documented %d\n", programName, status);
+        if (verbose_m) Rf_warning("%s: ParseStatus is not documented %d\n", programName, status);
         UNPROTECT(2);
         mb_m.rewind();
         return 1;
@@ -403,7 +400,7 @@
 }
 
 RInside& RInside::instance(){
-    return *instance_ ;
+    return *instance_m;
 }
 
 /* callbacks */



More information about the Rinside-commits mailing list