|
20 | 20 |
|
21 | 21 | namespace Rcpp{ |
22 | 22 |
|
| 23 | + namespace internal{ |
| 24 | + inline SEXP empty_data_frame(){ |
| 25 | + SEXP dataFrameSym = ::Rf_install( "data.frame"); |
| 26 | + return ::Rf_eval( ::Rf_lang1( dataFrameSym ), R_GlobalEnv ) ; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + template <template <class> class StoragePolicy> |
23 | 31 | template <typename T> |
24 | | - DataFrame::DataFrame( const T& obj) : DataFrame( wrap(obj) ) {} |
| 32 | + DataFrame_Impl<StoragePolicy>::DataFrame_Impl( const T& obj) : DataFrame_Impl( wrap(obj) ) {} |
| 33 | + |
| 34 | + template <template <class> class StoragePolicy> |
| 35 | + DataFrame_Impl<StoragePolicy>::DataFrame_Impl(): List( internal::empty_data_frame() ){} |
| 36 | + |
| 37 | + template <template <class> class StoragePolicy> |
| 38 | + DataFrame_Impl<StoragePolicy>::DataFrame_Impl(SEXP x) { |
| 39 | + set_sexp(x) ; |
| 40 | + } |
| 41 | + |
| 42 | + template <template <class> class StoragePolicy> |
| 43 | + DataFrame_Impl<StoragePolicy>::DataFrame_Impl( const DataFrame_Impl& other): List(other.get__()) {} |
| 44 | + |
| 45 | + template <template <class> class StoragePolicy> |
| 46 | + DataFrame_Impl<StoragePolicy>& DataFrame_Impl<StoragePolicy>::operator=( DataFrame_Impl& other) { |
| 47 | + Storage::set__( other.get__() ) ; |
| 48 | + return *this ; |
| 49 | + } |
| 50 | + |
| 51 | + template <template <class> class StoragePolicy> |
| 52 | + DataFrame_Impl<StoragePolicy>& DataFrame_Impl<StoragePolicy>::operator=( SEXP x) { |
| 53 | + Storage::set__(x) ; |
| 54 | + return *this ; |
| 55 | + } |
| 56 | + |
| 57 | + template <template <class> class StoragePolicy> |
| 58 | + DataFrame_Impl<StoragePolicy>::~DataFrame_Impl(){} |
| 59 | + |
| 60 | + template <template <class> class StoragePolicy> |
| 61 | + void DataFrame_Impl<StoragePolicy>::set_sexp(SEXP x) { |
| 62 | + if( ::Rf_inherits( x, "data.frame" )){ |
| 63 | + Storage::set__( x ) ; |
| 64 | + } else{ |
| 65 | + SEXP y = internal::convert_using_rfunction( x, "as.data.frame" ) ; |
| 66 | + Storage::set__( y ) ; |
| 67 | + x = y ; |
| 68 | + } |
| 69 | + } |
| 70 | + template <template <class> class StoragePolicy> |
| 71 | + int DataFrame_Impl<StoragePolicy>::nrows() const { |
| 72 | + return Rf_length( VECTOR_ELT(Storage::get__(), 0) ); |
| 73 | + } |
| 74 | + |
| 75 | + template <template <class> class StoragePolicy> |
| 76 | + DataFrame_Impl<StoragePolicy> DataFrame_Impl<StoragePolicy>::from_list( List obj ){ |
| 77 | + bool use_default_strings_as_factors = true ; |
| 78 | + bool strings_as_factors = true ; |
| 79 | + int strings_as_factors_index = -1 ; |
| 80 | + int n = obj.size() ; |
| 81 | + CharacterVector names = obj.attr( "names" ) ; |
| 82 | + if( !names.isNULL() ){ |
| 83 | + for( int i=0; i<n; i++){ |
| 84 | + if( names[i] == "stringsAsFactors" ){ |
| 85 | + strings_as_factors_index = i ; |
| 86 | + use_default_strings_as_factors = false ; |
| 87 | + if( !as<bool>(obj[i]) ) strings_as_factors = false ; |
| 88 | + break ; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if( use_default_strings_as_factors ) |
| 93 | + return DataFrame(obj) ; |
| 94 | + SEXP as_df_symb = Rf_install("as.data.frame"); |
| 95 | + SEXP strings_as_factors_symb = Rf_install("stringsAsFactors"); |
| 96 | + |
| 97 | + obj.erase(strings_as_factors_index) ; |
| 98 | + names.erase(strings_as_factors_index) ; |
| 99 | + obj.attr( "names") = names ; |
| 100 | + Scoped<SEXP> call = Rf_lang3(as_df_symb, obj, wrap( strings_as_factors ) ) ; |
| 101 | + SET_TAG( CDDR(call), strings_as_factors_symb ) ; |
| 102 | + Scoped<SEXP> res = Rcpp_eval( call ) ; |
| 103 | + DataFrame out( res ) ; |
| 104 | + return out ; |
| 105 | + } |
| 106 | + |
| 107 | + |
25 | 108 |
|
26 | 109 | } // namespace Rcpp |
27 | 110 |
|
|
0 commit comments