Skip to content

Commit

Permalink
early factor conversion for #59
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Nov 11, 2019
1 parent 7dff7ef commit 7609aa0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
23 changes: 21 additions & 2 deletions inst/include/jsonify/to_json/writers/complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace complex {
bool factors_as_string
) {

//Rcpp::Rcout << "is_factor: 1 " << Rf_isFactor( this_vec ) << std::endl;

switch( TYPEOF( this_vec ) ) {
case REALSXP: {
if( Rf_isMatrix( this_vec ) ) {
Expand Down Expand Up @@ -79,6 +81,8 @@ namespace complex {
int row
) {

//Rcpp::Rcout << "is_factor: 2 " << Rf_isFactor( this_vec ) << std::endl;

switch( TYPEOF( this_vec ) ) {
case REALSXP: {

Expand Down Expand Up @@ -180,6 +184,19 @@ namespace complex {
int n_rows = df.nrows();
Rcpp::StringVector column_names = df.names();

if( factors_as_string ) {
//Rcpp::Rcout << "factors_as_string " << std::endl;
// convert here, cos speed?
for( df_col = 0; df_col < n_cols; ++df_col ) {
const char *h = column_names[ df_col ];
if( Rf_isFactor( df[ h ] ) ) {
//Rcpp::Rcout << "converting factor" << std::endl;
df[ h ] = Rcpp::as< Rcpp::StringVector >( df[ h ] );
}
}
}


if ( by == "column") {
writer.StartObject();

Expand All @@ -206,7 +223,9 @@ namespace complex {
SEXP this_vec = df[ h ];

switch( TYPEOF( this_vec ) ) {


//Rcpp::Rcout << "is_factor: " << Rf_isFactor( this_vec ) << std::endl;

case VECSXP: {
Rcpp::List lst = Rcpp::as< Rcpp::List >( this_vec );
write_value( writer, lst, unbox, digits, numeric_dates, factors_as_string, by, row, in_data_frame );
Expand All @@ -232,7 +251,7 @@ namespace complex {
const char *h = column_names[ df_col ];
writer.String( h );
SEXP this_vec = df[ h ];

switch( TYPEOF( this_vec ) ) {
case VECSXP: {
Rcpp::List lst = Rcpp::as< Rcpp::List >( this_vec );
Expand Down
4 changes: 3 additions & 1 deletion inst/include/jsonify/to_json/writers/simple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ namespace simple {

} else if ( factors_as_string && Rf_isFactor( iv ) ) {

//Rcpp::Rcout << "factors 1" << std::endl;

Rcpp::CharacterVector lvls = iv.attr( "levels" );
if (lvls.length() == 0 && iv.length() == 0 ) {
writer.StartArray();
Expand Down Expand Up @@ -209,7 +211,7 @@ namespace simple {
write_value( writer, sv, row );

} else if ( factors_as_string && Rf_isFactor( iv ) ) {

//Rcpp::Rcout << "factors 2" << std::endl;
Rcpp::StringVector lvls = iv.attr( "levels" );

if (lvls.length() == 0 && iv.length() == 0 ) {
Expand Down

0 comments on commit 7609aa0

Please sign in to comment.