Rcpp Version 0.9.10
Formula.cpp
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 //
00003 // Formula.cpp: Rcpp R/C++ interface class library -- Formulae
00004 //
00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
00006 //
00007 // This file is part of Rcpp.
00008 //
00009 // Rcpp is free software: you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // Rcpp is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #include <Rcpp/Formula.h>
00023 
00024 namespace Rcpp{
00025         
00026     Formula::Formula() : Language(){}
00027         
00028     Formula::Formula(SEXP x) : Language(){
00029         switch( TYPEOF( x ) ){
00030         case LANGSXP:
00031             if( ::Rf_inherits( x, "formula") ){
00032                 setSEXP( x );
00033             } else{
00034                 SEXP y = internal::convert_using_rfunction( x, "as.formula") ;
00035                 setSEXP( y ) ;
00036             }
00037             break;
00038         case EXPRSXP:
00039         case VECSXP:
00040             /* lists or expression, try the first one */
00041             if( ::Rf_length(x) > 0 ){
00042                 SEXP y = VECTOR_ELT( x, 0 ) ;
00043                 if( ::Rf_inherits( y, "formula" ) ){
00044                     setSEXP( y ) ;  
00045                 } else{
00046                     SEXP z = internal::convert_using_rfunction( y, "as.formula") ;
00047                     setSEXP( z ) ;
00048                 }
00049             } else{
00050                 throw not_compatible( "cannot create formula from empty list or expression" ) ; 
00051             }
00052             break;
00053         default:
00054             SEXP y = internal::convert_using_rfunction( x, "as.formula") ;
00055             setSEXP( y ) ;
00056         }
00057     }
00058         
00059     Formula::Formula( const std::string& code) : Language() {
00060         setSEXP( internal::convert_using_rfunction( ::Rf_mkString(code.c_str()), "as.formula") );       
00061     }
00062         
00063     Formula::Formula( const Formula& other ) : Language( other.asSexp() ){}
00064         
00065     Formula& Formula::operator=( const Formula& other ){
00066         setSEXP( other.asSexp() );
00067         return *this ;
00068     }
00069         
00070 } // namespace Rcpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines