[Phylobase-commits] r795 - pkg/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Apr 25 22:37:56 CEST 2010


Author: edd
Date: 2010-04-25 22:37:55 +0200 (Sun, 25 Apr 2010)
New Revision: 795

Modified:
   pkg/src/ReadWithNCL.cpp
Log:
add new version of ReadCharsWithNCL() using new Rcpp API
commented-out old version of ReadCharsWithNCL()
commented-out ReadWithNCL() which is not called by anyone
removed some old commentes and reindented to C++ style used by R


Modified: pkg/src/ReadWithNCL.cpp
===================================================================
--- pkg/src/ReadWithNCL.cpp	2010-04-24 15:10:40 UTC (rev 794)
+++ pkg/src/ReadWithNCL.cpp	2010-04-25 20:37:55 UTC (rev 795)
@@ -1,127 +1,95 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+
 //  Copyright (C) 2007-2008 Brian O'Meara & Derrick Zwickl
-//  A modification of the RcppExample.cp file of the R/C++ interface class
-//  library (see below) to use for loading trees and data from Nexus into R.
-//  Licensing as below.
 
-
-// RcppExample.cpp: Part of the R/C++ interface class library, Version 5.0
-//
-// Copyright (C) 2005-2006 Dominick Samperi
-//
-// This library is free software; you can redistribute it and/or modify it
-// under the terms of the GNU Lesser General Public License as published by
-// the Free Software Foundation; either version 2.1 of the License, or (at
-// your option) any later version.
-//
-// This library is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
-// License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with this library; if not, write to the Free Software Foundation,
-// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
-#include <fstream>
-
 #include <Rcpp.h>
 
-//TODO - figure out where/why length is getting defined as Rf_length so that
-//this isn't necessary to compile
-// DE: This comes from Rinternals.h via the R_NO_REMAP used in Rcpp.h -- I have
-//     found that some libraries fail to build when R defines length, error, ...
-//     so I prefer to use the safer and explicit Rf_ prefixes as eg Rf_error
-//     used here three times
-#define  Rf_length length
-
 #include <ncl/ncl.h>
 #include "NCLInterface.h"
 
 using namespace std;
 
-//This function receives a list of parameters from
-//R, which can then be extracted below
-RcppExport SEXP ReadWithNCL(SEXP params) {
+// //This function receives a list of parameters from R, which can then be extracted below
+// RcppExport SEXP ReadWithNCL(SEXP params) {
 
-    SEXP  rl=R_NilValue; // Use this when there is nothing to be returned.
-    char* exceptionMesg=NULL;
-    try {
+//     SEXP  rl=R_NilValue; // Use this when there is nothing to be returned.
+//     char* exceptionMesg=NULL;
+//     try {
 
+// 		// Get parameters in params - only 1 is gotten now
+// 		RcppParams rparam(params);
+// #		if defined(FILENAME_AS_NEXUS)
+// 			string filename = "'";
+// 			filename+=rparam.getStringValue("filename");
+// 			filename+="'";
+// #		else
+// 			string filename = rparam.getStringValue("filename");
+// #		endif
 
-		// Get parameters in params - only 1 is gotten now
-		RcppParams rparam(params);
-#		if defined(FILENAME_AS_NEXUS)
-			string filename = "'";
-			filename+=rparam.getStringValue("filename");
-			filename+="'";
-#		else
-			string filename = rparam.getStringValue("filename");
-#		endif
+// 		BASICCMDLINE reader;
 
-		BASICCMDLINE reader;
+// 		//this is where the reader would be passed the filename to read
+// 		//reader.Run(filename.c_str()); //Will not compile
+// 		//reader.Run(NULL);
+// 		reader.Initialize(const_cast < char* > (filename.c_str()));
 
-		//this is where the reader would be passed the filename to read
-		//reader.Run(filename.c_str()); //Will not compile
-		//reader.Run(NULL);
-		reader.Initialize(const_cast < char* > (filename.c_str()));
+// 		string filenameString = "I was told to read a file named ";
+// 		filenameString += filename;
 
-		string filenameString = "I was told to read a file named ";
-		filenameString += filename;
+// 		//Various calls to the reader can be made here to fill the various strings
+// 		//string treeString = "This was passed in the tree string";
+// 		NxsString treeStringNxs;
+// 		reader.RReturnTrees(treeStringNxs);
+// 		string treeString=treeStringNxs.c_str();
 
-		//Various calls to the reader can be made here to fill the various strings
-		//string treeString = "This was passed in the tree string";
-		NxsString treeStringNxs;
-		reader.RReturnTrees(treeStringNxs);
-		string treeString=treeStringNxs.c_str();
+// 		string otherString = "";
 
-		string otherString = "";
+// 		//string discreteString = "This was passed in the discrete string";
 
-		//string discreteString = "This was passed in the discrete string";
+// 		NxsString characterStringNxs;
+// 		reader.RReturnCharacters(characterStringNxs,false, true, false, false);
+// 		//string characterString=characterStringNxs.c_str();
 
-		NxsString characterStringNxs;
-		reader.RReturnCharacters(characterStringNxs,false, true, false, false);
-		//string characterString=characterStringNxs.c_str();
+// 		string continuousString = "This was passed in the continuous string";
+// 		//reader.GetContinuousString();
 
-		string continuousString = "This was passed in the continuous string";
-		//reader.GetContinuousString();
+// 		//Just to test to see if reader is running
+// 		string testString = reader.TestRunning();
 
-		//Just to test to see if reader is running
-		string testString = reader.TestRunning();
+// 		// Build result set to be returned as a list to R.
+// 		RcppResultSet rs;
 
-		// Build result set to be returned as a list to R.
-		RcppResultSet rs;
+// 		//if the various strings are nonempty, add them to the ResultSet
+// 		if(filenameString.length() > 0)
+// 			rs.add("filenamestring", filenameString);
+// 		if(treeString.length() > 0)
+// 			rs.add("treestring", treeString);
+// 		if(otherString.length() > 0)
+// 			rs.add("otherstring", otherString);
+// 		if(characterStringNxs.length() > 0)
+// 			rs.add("characterstring", characterStringNxs.c_str());
+// 		if(continuousString.length() > 0)
+// 			rs.add("continuousstring", continuousString);
+// 		if(testString.length() > 0)
+// 			rs.add("teststring", testString);
 
-		//if the various strings are nonempty, add them to the ResultSet
-		if(filenameString.length() > 0)
-			rs.add("filenamestring", filenameString);
-		if(treeString.length() > 0)
-			rs.add("treestring", treeString);
-		if(otherString.length() > 0)
-			rs.add("otherstring", otherString);
-		if(characterStringNxs.length() > 0)
-			rs.add("characterstring", characterStringNxs.c_str());
-		if(continuousString.length() > 0)
-			rs.add("continuousstring", continuousString);
-		if(testString.length() > 0)
-			rs.add("teststring", testString);
 
+// 		// Get the list to be returned to R.
+// 		rl = rs.getReturnList();
 
-		// Get the list to be returned to R.
-		rl = rs.getReturnList();
+//     	} catch(std::exception& ex) {
+// 			exceptionMesg = copyMessageToR(ex.what());
+//     	} catch(...) {
+// 			exceptionMesg = copyMessageToR("unknown reason");
+//     	}
 
-    	} catch(std::exception& ex) {
-			exceptionMesg = copyMessageToR(ex.what());
-    	} catch(...) {
-			exceptionMesg = copyMessageToR("unknown reason");
-    	}
+//     if(exceptionMesg != NULL)
+// 	Rf_error(exceptionMesg);
 
-    if(exceptionMesg != NULL)
-	Rf_error(exceptionMesg);
+//     return rl;
 
-    return rl;
+// }
 
-}
-
 RcppExport SEXP ReadTreesWithNCL(SEXP params) {
 
     SEXP  rl=R_NilValue; // Use this when there is nothing to be returned.
@@ -174,62 +142,84 @@
 
 }
 
-RcppExport SEXP ReadCharsWithNCL(SEXP params) {
+// old API version -- version for the new API below
+// RcppExport SEXP ReadCharsWithNCL(SEXP params) {
 
-    SEXP  rl=R_NilValue; // Use this when there is nothing to be returned.
-    char* exceptionMesg=NULL;
-    try {
+//     SEXP  rl = R_NilValue;
+//     char* exceptionMesg = NULL;
+//     try {
 
+// 	// Get parameters in params - only 1 is gotten now
+// 	RcppParams rparam(params);
+// #	if defined(FILENAME_AS_NEXUS)
+// 	    string filename = "'";
+// 	    filename+=rparam.getStringValue("filename");
+// 	    filename+="'";
+// #	else
+// 	    string filename = rparam.getStringValue("filename");
+// #	endif
 
-		// Get parameters in params - only 1 is gotten now
-		RcppParams rparam(params);
-#		if defined(FILENAME_AS_NEXUS)
-			string filename = "'";
-			filename+=rparam.getStringValue("filename");
-			filename+="'";
-#		else
-			string filename = rparam.getStringValue("filename");
-#		endif
+// 	bool allchar = rparam.getBoolValue("allchar");
+// 	bool levelsall=rparam.getBoolValue("levelsall");
+// 	bool polymorphictomissing=rparam.getBoolValue("polymorphictomissing");
+// 	bool returnlabels=rparam.getBoolValue("returnlabels");
 
-		bool allchar = rparam.getBoolValue("allchar");
-		bool levelsall=rparam.getBoolValue("levelsall");
-		bool polymorphictomissing=rparam.getBoolValue("polymorphictomissing");
-		bool returnlabels=rparam.getBoolValue("returnlabels");
+// 	BASICCMDLINE reader;
 
-		BASICCMDLINE reader;
+// 	//this is where the reader would be passed the filename to read
+// 	//reader.Run(filename.c_str()); //Will not compile
+// 	//reader.Run(NULL);
+// 	reader.Initialize(const_cast < char* > (filename.c_str()));
 
-		//this is where the reader would be passed the filename to read
-		//reader.Run(filename.c_str()); //Will not compile
-		//reader.Run(NULL);
-		reader.Initialize(const_cast < char* > (filename.c_str()));
+// 	NxsString charStringNxs;
+// 	reader.RReturnCharacters(charStringNxs,allchar, polymorphictomissing, levelsall, returnlabels);
+// 	string charString = charStringNxs.c_str();
+// 	std::cout << "charString is: " << charString << endl;
 
-		NxsString charStringNxs;
-		reader.RReturnCharacters(charStringNxs,allchar, polymorphictomissing, levelsall, returnlabels);
-		string charString=charStringNxs.c_str();
+// 	// Build result set to be returned as a list to R.
+// 	RcppResultSet rs;
 
-		// Build result set to be returned as a list to R.
-		RcppResultSet rs;
+// 	//if the various strings are nonempty, add them to the ResultSet
+// 	if(charString.length() > 0)
+// 	    rs.add("charstring", charString);
 
-		//if the various strings are nonempty, add them to the ResultSet
-		if(charString.length() > 0)
-			rs.add("charstring", charString);
+// 	// Get the list to be returned to R.
+// 	rl = rs.getReturnList();
 
+//     } catch(std::exception& ex) {
+// 	exceptionMesg = copyMessageToR(ex.what());
+//     } catch(...) {
+// 	exceptionMesg = copyMessageToR("unknown reason");
+//     }
 
-		// Get the list to be returned to R.
-		rl = rs.getReturnList();
+//     if(exceptionMesg != NULL)
+// 	Rf_error(exceptionMesg);
 
-	} catch(std::exception& ex) {
-		exceptionMesg = copyMessageToR(ex.what());
-	} catch(...) {
-		exceptionMesg = copyMessageToR("unknown reason");
-	}
+//     return rl;
 
-    if(exceptionMesg != NULL)
-		Rf_error(exceptionMesg);
+// }
 
-    return rl;
+RcppExport SEXP ReadCharsWithNCL(SEXP params) {
 
+    Rcpp::List list(params);
+    #if defined(FILENAME_AS_NEXUS)
+        string filename = "'" + list["filename"] + "'";
+    #else
+	string filename = list["filename"];
+    #endif
+    
+    BASICCMDLINE reader;
+    reader.Initialize(const_cast < char* > (filename.c_str()));
+    
+    NxsString charStringNxs;
+    reader.RReturnCharacters(charStringNxs,
+			     list["allchar"], 			// boolean flags 
+			     list["polymorphictomissing"],
+			     list["levelsall"], 
+			     list["returnlabels"]);
+    string charString = charStringNxs.c_str();
+
+    return Rcpp::List::create(Rcpp::Named("charstring") = charString);
 }
 
 
-



More information about the Phylobase-commits mailing list