00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // RcppFrame.h: Rcpp R/C++ interface class library -- data.framee support 00004 // 00005 // Copyright (C) 2005 - 2006 Dominick Samperi 00006 // Copyright (C) 2008 - 2009 Dirk Eddelbuettel 00007 // 00008 // This file is part of Rcpp. 00009 // 00010 // Rcpp is free software: you can redistribute it and/or modify it 00011 // under the terms of the GNU General Public License as published by 00012 // the Free Software Foundation, either version 2 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // Rcpp is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU General Public License 00021 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00022 00023 #ifndef RcppFrame_h 00024 #define RcppFrame_h 00025 00026 #include <RcppCommon.h> 00027 #include <RcppDate.h> 00028 #include <RcppDatetime.h> 00029 00030 enum ColType { // Supported data frame column types. 00031 COLTYPE_DOUBLE, COLTYPE_INT, COLTYPE_STRING, 00032 COLTYPE_FACTOR, COLTYPE_LOGICAL, 00033 COLTYPE_DATE, COLTYPE_DATETIME 00034 }; 00035 class ColDatum; // forward declaration, see below 00036 00037 class RcppFrame { 00038 std::vector<std::string> colNames; 00039 std::vector<std::vector<ColDatum> > table; // table[row][col] 00040 00041 public: 00042 RcppFrame(SEXP df); // Construct from R data frame. 00043 RcppFrame(std::vector<std::string> colNames); 00044 std::vector<std::string>& getColNames(); 00045 std::vector<std::vector<ColDatum> >& getTableData(); 00046 void addRow(std::vector<ColDatum> rowData); 00047 int rows(); 00048 int cols(); 00049 }; 00050 00051 00052 class ColDatum { 00053 public: 00054 ColDatum(); 00055 ColDatum(const ColDatum& datum); 00056 ~ColDatum(); 00057 00058 ColType getType() const { return type; } 00059 void setDoubleValue(double val); 00060 void setIntValue(int val); 00061 void setLogicalValue(int val); 00062 void setStringValue(std::string val); 00063 void setDateValue(RcppDate date); 00064 void setDatetimeValue(RcppDatetime datetime); 00065 void setFactorValue(std::string *names, int numNames, int factorLevel); 00066 00067 double getDoubleValue(); 00068 int getIntValue(); 00069 int getLogicalValue(); 00070 std::string getStringValue(); 00071 RcppDate getDateValue(); 00072 double getDateRCode(); 00073 RcppDatetime getDatetimeValue(); 00074 void checkFactorType(); 00075 int getFactorNumLevels(); 00076 int getFactorLevel(); 00077 std::string *getFactorLevelNames(); 00078 std::string getFactorLevelName(); 00079 00080 private: 00081 ColType type; 00082 std::string s; 00083 double x; // used for double and datetime 00084 int i; // used for int and logical 00085 int level; // factor level 00086 int numLevels; // number of levels for this factor 00087 std::string *levelNames; // level name = levelNames[level-1] 00088 RcppDate d; 00089 }; 00090 00091 00092 #endif