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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Mar 17 02:40:37 CET 2010


Author: edd
Date: 2010-03-17 02:40:37 +0100 (Wed, 17 Mar 2010)
New Revision: 118

Modified:
   pkg/DESCRIPTION
   pkg/inst/ChangeLog
   pkg/inst/examples/standard/rinside_sample2.cpp
   pkg/inst/examples/standard/rinside_sample5.cpp
   pkg/src/RInside.cpp
   pkg/src/RInside.h
Log:
RInside nwo throw exceptions when parseEval fails (unless the older int parseEval(str, SEXP)
  is called which remains non-throwing)
examples updated accordingly


Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2010-03-16 19:31:00 UTC (rev 117)
+++ pkg/DESCRIPTION	2010-03-17 01:40:37 UTC (rev 118)
@@ -1,6 +1,6 @@
 Package: RInside
 Title: C++ classes to embed R in C++ applications
-Version: 0.2.1.2
+Version: 0.2.2
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois
 Maintainer: Dirk Eddelbuettel <edd at debian.org>
@@ -15,7 +15,7 @@
  Several examples are provided in the examples/ directory of
  the installed package, and Doxygen-generated documentation of
  the C++ classes is included as well.
-Depends: R (>= 2.10.0), Rcpp (>= 0.7.7.5)
+Depends: R (>= 2.10.0), Rcpp (>= 0.7.10)
 SystemRequirements: None
 URL: http://dirk.eddelbuettel.com/code/rinside.html
 License: GPL-2

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-03-16 19:31:00 UTC (rev 117)
+++ pkg/inst/ChangeLog	2010-03-17 01:40:37 UTC (rev 118)
@@ -1,3 +1,14 @@
+2010-03-16  Dirk Eddelbuettel  <edd at dexter>
+
+	* src/RInside.{h,cpp}: parseEval* function changed slightly so that
+	  SEXP parseEval(string) is now preferred, and it as well as
+	  void parseEvalQ(string) now throw exceptions; they call the
+	  non-throwing older int parseEval(string, SEXP). We changed the
+	  parseEvalQ from int to void which is more in-line with it purpose.
+	* inst/examples/standard/rinside_sample{2,4,5}.cpp: Adapted
+	  accordingly, mostly by removing no-longer-needed error checking
+	  and exepction throw which has now move 'up' into RInside.cpp
+
 2010-02-25  Dirk Eddelbuettel  <edd at dexter>
 
 	* inst/examples/mpi/: Added two variants using the C++ API to MPI

Modified: pkg/inst/examples/standard/rinside_sample2.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_sample2.cpp	2010-03-16 19:31:00 UTC (rev 117)
+++ pkg/inst/examples/standard/rinside_sample2.cpp	2010-03-17 01:40:37 UTC (rev 118)
@@ -13,28 +13,26 @@
         SEXP ans;
 
         std::string txt = "suppressMessages(library(fPortfolio))";
-        if (R.parseEvalQ(txt))          // load library, no return value
-            throw std::runtime_error("R cannot evaluate '" + txt + "'");
+        R.parseEvalQ(txt);	        // load library, no return value
 
         txt = "M <- as.matrix(SWX.RET); print(head(M)); M";
-        if (R.parseEval(txt, ans))      // assign matrix M to SEXP variable ans
-            throw std::runtime_error("R cannot evaluate '" + txt + "'");
-        RcppMatrix<double> M(ans);      // convert SEXP variable to an RcppMatrix
+        ans = R.parseEval(txt);	      	// assign matrix M to SEXP variable ans
 
+	Rcpp::NumericMatrix M(ans);     // convert SEXP variable to a NumericMatrix
+
         std::cout << "M has " 
-                  << M.getDim1() << " rows and " 
-                  << M.getDim2() << " cols" << std::endl;
+                  << M.nrow() << " rows and " 
+                  << M.ncol() << " cols" << std::endl;
         
         txt = "colnames(M)";
-        if (R.parseEval(txt, ans))      // assign columns names of M to ans
-            throw std::runtime_error("R cannot evaluate '" + txt + "'");
-        RcppStringVector cnames(ans);   // and into string vector cnames
-        
+        ans = R.parseEval(txt); 	// assign columns names of M to ans
 
-        for (int i=0; i<M.getDim2(); i++) {
-            std::cout << "Column " << cnames(i) << " in row 42 has " << M(42,i) << std::endl;
+	Rcpp::CharacterVector cnames(ans);   // and into string vector cnames
+
+        for (int i=0; i<M.ncol(); i++) {
+            std::cout << "Column " << cnames[i] << " in row 42 has " << M(42,i) << std::endl;
         }
-        
+    
     } catch(std::exception& ex) {
         std::cerr << "Exception caught: " << ex.what() << std::endl;
     } catch(...) {

Modified: pkg/inst/examples/standard/rinside_sample5.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_sample5.cpp	2010-03-16 19:31:00 UTC (rev 117)
+++ pkg/inst/examples/standard/rinside_sample5.cpp	2010-03-17 01:40:37 UTC (rev 118)
@@ -2,7 +2,7 @@
 //
 // Another simple example inspired by an r-devel mail by Martin Becker
 //
-// Copyright (C) 2009 Dirk Eddelbuettel and GPL'ed 
+// Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Romain Francois 
 
 #include "RInside.h"                    // for the embedded R via RInside
 
@@ -13,15 +13,12 @@
         SEXP ans;
 
         std::string txt = "myenv <- new.env(hash=TRUE, size=NA)";
-        if (R.parseEvalQ(txt))          // eval string quietly, no result
-            throw std::runtime_error("R cannot evaluate '" + txt + "'");
+        R.parseEvalQ(txt);          	// eval string quietly, no result
 
         txt = "as.integer(is.environment(myenv))";
-        if (R.parseEval(txt, ans))      // eval string, result in ans
-            throw std::runtime_error("R cannot evaluate '" + txt + "'");
-	
-	RcppVector<int> V(ans);   	// convert SEXP variable to an RcppVector
-  
+        ans = R.parseEval(txt);      	// eval string, result in ans
+	Rcpp::LogicalVector V(ans);   	// convert SEXP variable to vector
+
 	std::cout << "We "
 		  << (V(0) ? "do" : "do not")
 		  << " have an environment." << std::endl;

Modified: pkg/src/RInside.cpp
===================================================================
--- pkg/src/RInside.cpp	2010-03-16 19:31:00 UTC (rev 117)
+++ pkg/src/RInside.cpp	2010-03-17 01:40:37 UTC (rev 118)
@@ -65,10 +65,10 @@
     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");
+		//perror("ERROR: couldn't set/replace an R environment variable");
 		//exit(1);
-		throw runtime_error("Could not set R environment variable " + 
-				    std::string(R_VARS[i]) + " to " std::string(R_VARS[i+1]));
+		throw std::runtime_error("Could not set R environment variable " + 
+					 std::string(R_VARS[i]) + " to " +  std::string(R_VARS[i+1]));
 	    }
 	}
     }
@@ -125,9 +125,9 @@
     }
     R_TempDir = (char*) tmp;
     if (setenv("R_SESSION_TMPDIR",tmp,1) != 0){
-	perror("Fatal Error: couldn't set/replace R_SESSION_TMPDIR!");
+	//perror("Fatal Error: couldn't set/replace R_SESSION_TMPDIR!");
 	//exit(1);
-	throw runtime_error("Could not set / replace R_SESSION_TMPDIR to " + std::string(tmp));
+	throw std::runtime_error("Could not set / replace R_SESSION_TMPDIR to " + std::string(tmp));
     }
 }
 
@@ -222,8 +222,10 @@
 	    idx += packobjc[i] ;
     	}
     } catch( std::exception& ex){
-	fprintf(stderr,"%s: Error calling delayedAssign:\n %s", programName, ex.what() );
-	exit(1);	    
+	//fprintf(stderr,"%s: Error calling delayedAssign:\n %s", programName, ex.what() );
+	//exit(1);	    
+	// is it wrong to throw in a catch() block?
+	throw std::runtime_error("Error calling delayedAssign: " + std::string(ex.what()));
     }
 }
 
@@ -281,16 +283,20 @@
     return 0;
 }
 
-int RInside::parseEvalQ(const std::string & line) {
+void RInside::parseEvalQ(const std::string & line) {
     SEXP ans;
     int rc = parseEval(line, ans);
-    return rc;
+    if (rc != 0) {
+	throw std::runtime_error("Error evaluating: " + line);
+    }
 }
 
 SEXP RInside::parseEval(const std::string & line) {
     SEXP ans;
-    /*nt rc = */ parseEval(line, ans);
-    // TODO: throw on error
+    int rc = parseEval(line, ans);
+    if (rc != 0) {
+	throw std::runtime_error("Error evaluating: " + line);
+    }
     return ans;
 }
 

Modified: pkg/src/RInside.h
===================================================================
--- pkg/src/RInside.h	2010-03-16 19:31:00 UTC (rev 117)
+++ pkg/src/RInside.h	2010-03-17 01:40:37 UTC (rev 118)
@@ -55,9 +55,9 @@
     void initialize(const int argc, const char* const argv[] ) ;
 
 public:
-    int parseEval(const std::string & line, SEXP &ans);
-    int parseEvalQ(const std::string & line);
-    SEXP parseEval(const std::string & line);
+    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)
+    SEXP parseEval(const std::string & line);		 // parse line, return SEXP (throws on error)
 
     template <typename T>
     void assign(const T& object, const std::string& nam){



More information about the Rinside-commits mailing list