[Rcpp-commits] r952 - in pkg/Rcpp: . inst inst/unitTests src src/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Apr 2 09:58:14 CEST 2010


Author: romain
Date: 2010-04-02 09:58:14 +0200 (Fri, 02 Apr 2010)
New Revision: 952

Added:
   pkg/Rcpp/inst/unitTests/runit.Formula.R
   pkg/Rcpp/src/Formula.cpp
   pkg/Rcpp/src/Rcpp/Formula.h
Modified:
   pkg/Rcpp/DESCRIPTION
   pkg/Rcpp/NEWS
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/src/Rcpp.h
Log:
new class Rcpp::Formula

Modified: pkg/Rcpp/DESCRIPTION
===================================================================
--- pkg/Rcpp/DESCRIPTION	2010-03-30 08:57:41 UTC (rev 951)
+++ pkg/Rcpp/DESCRIPTION	2010-04-02 07:58:14 UTC (rev 952)
@@ -1,6 +1,6 @@
 Package: Rcpp
 Title: Rcpp R/C++ interface package
-Version: 0.7.11
+Version: 0.7.11.1
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, with contributions 
  by Simon Urbanek, David Reiss and Douglas Bates; based on code written during 

Modified: pkg/Rcpp/NEWS
===================================================================
--- pkg/Rcpp/NEWS	2010-03-30 08:57:41 UTC (rev 951)
+++ pkg/Rcpp/NEWS	2010-04-02 07:58:14 UTC (rev 952)
@@ -1,3 +1,7 @@
+0.7.12
+
+	o	new class Rcpp::Formula to help building formulae in C++
+
 0.7.11	2010-03-26
 
     o	Vector<> gains a set of templated factory methods "create" which

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-03-30 08:57:41 UTC (rev 951)
+++ pkg/Rcpp/inst/ChangeLog	2010-04-02 07:58:14 UTC (rev 952)
@@ -1,3 +1,8 @@
+2010-04-2	Romain Francois <romain at r-enthusiasts.com>
+
+	* src/Rcpp/Formula.h: new Rcpp::Formula class
+	* inst/unitTests/runit.Formula.R: unit test for Rcpp::Formula
+
 2010-03-26  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.7.11
@@ -14,7 +19,7 @@
 
 2010-03-23  Dirk Eddelbuettel  <edd at debian.org>
 
-        * inst/unitTests/runit.CharacterVector.R: New unit test or bug found by Doug
+        * inst/unitTests/runit.CharacterVector.R: New unit test for bug found by Doug
 
 2010-03-23  Romain Francois <romain at r-enthusiasts.com>
 

Added: pkg/Rcpp/inst/unitTests/runit.Formula.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Formula.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.Formula.R	2010-04-02 07:58:14 UTC (rev 952)
@@ -0,0 +1,45 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+#
+# This file is part of Rcpp.
+#
+# Rcpp is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# Rcpp 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+.setUp <- function(){
+	suppressMessages( require( inline ) )
+}
+
+test.Formula <- function(){
+	funx <- cfunction(signature(), '
+	Formula f( "x ~ y + z" ) ;
+	return f;
+	',
+	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;")
+	checkEquals( funx(), x ~ y + z, msg = "Formula( string )" )
+}
+
+test.Formula.SEXP <- function(){
+	funx <- cfunction(signature( form = "ANY" ), '
+	Formula f(form) ;
+	return f;
+	',
+	Rcpp=TRUE, verbose=FALSE, includes = "using namespace Rcpp;")
+	checkEquals( funx( x ~ y + z), x ~ y + z, msg = "Formula( SEXP = formula )" )
+	checkEquals( funx( "x ~ y + z" ), x ~ y + z, msg = "Formula( SEXP = STRSXP )" )
+	checkEquals( funx( parse( text = "x ~ y + z") ), x ~ y + z, msg = "Formula( SEXP = EXPRSXP )" )
+	checkEquals( funx( list( "x ~ y + z") ), x ~ y + z, msg = "Formula( SEXP = VECSXP(1 = STRSXP) )" )
+	checkEquals( funx( list( x ~ y + z) ), x ~ y + z, msg = "Formula( SEXP = VECSXP(1 = formula) )" )
+	
+}

Added: pkg/Rcpp/src/Formula.cpp
===================================================================
--- pkg/Rcpp/src/Formula.cpp	                        (rev 0)
+++ pkg/Rcpp/src/Formula.cpp	2010-04-02 07:58:14 UTC (rev 952)
@@ -0,0 +1,70 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Formula.cpp: Rcpp R/C++ interface class library -- Formulae
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <Rcpp/Formula.h>
+
+namespace Rcpp{
+	
+	Formula::Formula() : Language(){}
+	
+	Formula::Formula(SEXP x) : Language(){
+		switch( TYPEOF( x ) ){
+		case LANGSXP:
+			if( ::Rf_inherits( x, "formula") ){
+				setSEXP( x );
+			} else{
+				SEXP y = internal::convert_using_rfunction( x, "as.formula") ;
+				setSEXP( y ) ;
+			}
+			break;
+		case EXPRSXP:
+		case VECSXP:
+			/* lists or expression, try the first one */
+			if( ::Rf_length(x) > 0 ){
+				SEXP y = VECTOR_ELT( x, 0 ) ;
+				if( ::Rf_inherits( y, "formula" ) ){
+					setSEXP( y ) ;	
+				} else{
+					SEXP z = internal::convert_using_rfunction( y, "as.formula") ;
+					setSEXP( z ) ;
+				}
+			} else{
+				Rcpp_error( "cannot create formula from empty list or expression" ) ;	
+			}
+			break;
+		default:
+			SEXP y = internal::convert_using_rfunction( x, "as.formula") ;
+			setSEXP( y ) ;
+		}
+	}
+	
+	Formula::Formula( const std::string& code) : Language() {
+		setSEXP( internal::convert_using_rfunction( ::Rf_mkString(code.c_str()), "as.formula") );	
+	}
+	
+	Formula::Formula( const Formula& other ) : Language( other.asSexp() ){}
+	
+	Formula& Formula::operator=( const Formula& other ){
+		setSEXP( other.asSexp() );
+		return *this ;
+	}
+	
+} // namespace Rcpp

Added: pkg/Rcpp/src/Rcpp/Formula.h
===================================================================
--- pkg/Rcpp/src/Rcpp/Formula.h	                        (rev 0)
+++ pkg/Rcpp/src/Rcpp/Formula.h	2010-04-02 07:58:14 UTC (rev 952)
@@ -0,0 +1,46 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Formula.h: Rcpp R/C++ interface class library -- formula
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__Formula_h
+#define Rcpp__Formula_h
+
+#include <RcppCommon.h>
+#include <Rcpp/Language.h>
+
+namespace Rcpp{ 
+
+class Formula : public Language {
+public:
+
+	Formula() ;
+	
+	Formula(SEXP lang) ;
+
+	Formula(const Formula& other) ;
+	explicit Formula( const std::string& code ); 
+
+	Formula& operator=(const Formula& other) ;
+	
+} ; 
+
+} // namespace Rcpp
+
+#endif

Modified: pkg/Rcpp/src/Rcpp.h
===================================================================
--- pkg/Rcpp/src/Rcpp.h	2010-03-30 08:57:41 UTC (rev 951)
+++ pkg/Rcpp/src/Rcpp.h	2010-04-02 07:58:14 UTC (rev 952)
@@ -66,5 +66,6 @@
 #include <Rcpp/Function.h>
 #include <Rcpp/WeakReference.h>
 #include <Rcpp/StringTransformer.h>
+#include <Rcpp/Formula.h>
 
 #endif



More information about the Rcpp-commits mailing list