Skip to content

Commit

Permalink
make rbind_all stricter (only accepting lists of data frames). closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Mar 17, 2014
1 parent 79c4d3d commit d9c2d8e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -7,6 +7,8 @@
* `glimpse()` makes it possible to see all the columns in a tbl,
displaying as much data for each variable as can be fit on a single line.

* `rbind_all()` is stricter and only accepts list of data frames (#288)

## Bug fixes

* Code adapted to Rcpp > 0.11.1
Expand Down
31 changes: 31 additions & 0 deletions inst/include/tools/FilteredListOf.h
@@ -0,0 +1,31 @@
#ifndef dplyr_tools_FilteredListOf_H
#define dplyr_tools_FilteredListOf_H

namespace Rcpp {

template <typename T>
class FilteredListOf {
public:

FilteredListOf(SEXP data_) : data(data_){
int n = data.size() ;
for( int i=0; i<n; i++){
indices.push_back(i) ;
}
}

T operator[](int i) const {
return as<T>( data[indices[i]]) ;
}

int size() const {
return indices.size() ;
}

private:
List data ;
std::vector<int> indices ;
} ;
}

#endif
37 changes: 37 additions & 0 deletions inst/include/tools/StrictListOf.h
@@ -0,0 +1,37 @@
#ifndef dplyr_tools_FilteredListOf_H
#define dplyr_tools_FilteredListOf_H

namespace Rcpp {

template <typename T>
class StrictListOf {
public:

StrictListOf(SEXP data_) : data(data_){
int n = data.size() ;
for( int i=0; i<n; i++){
if( !is<T>(data[i]) ){
std::stringstream s ;
s << "object at index "
<< i
<< " not compatible with class "
<< DEMANGLE(T) ;
stop( s.str() ) ;
}
}
}

T operator[](int i) const {
return as<T>( data[i] ) ;
}

int size() const {
return data.size() ;
}

private:
List data ;
} ;
}

#endif
2 changes: 2 additions & 0 deletions inst/include/tools/tools.h
Expand Up @@ -6,6 +6,8 @@
#include <tools/hash.h>
#include <tools/delete_all.h>
#include <tools/ListOf.h>
#include <tools/StrictListOf.h>
#include <tools/FilteredListOf.h>
#include <tools/collapse.h>
#include <tools/get_single_class.h>
#include <tools/SlicingIndex.h>
Expand Down
6 changes: 6 additions & 0 deletions inst/tests/test-rbind.r
Expand Up @@ -90,3 +90,9 @@ test_that( "rbind handles NA in factors #279", {
expect_equal(res$c, c("d","b"))

})

test_that( "rbind_all only accepts data frames #288",{
ll <- list(c(1,2,3,4, 5), c(6, 7, 8, 9, 10))
expect_error(rbind_all(ll))
})

4 changes: 2 additions & 2 deletions src/RcppExports.cpp
Expand Up @@ -448,13 +448,13 @@ BEGIN_RCPP
END_RCPP
}
// rbind_all
List rbind_all(ListOf<DataFrame> dots);
List rbind_all(StrictListOf<DataFrame> dots);
RcppExport SEXP dplyr_rbind_all(SEXP dotsSEXP) {
BEGIN_RCPP
SEXP __sexp_result;
{
Rcpp::RNGScope __rngScope;
Rcpp::traits::input_parameter< ListOf<DataFrame> >::type dots(dotsSEXP );
Rcpp::traits::input_parameter< StrictListOf<DataFrame> >::type dots(dotsSEXP );
List __result = rbind_all(dots);
PROTECT(__sexp_result = Rcpp::wrap(__result));
}
Expand Down
2 changes: 1 addition & 1 deletion src/dplyr.cpp
Expand Up @@ -1549,7 +1549,7 @@ List rbind__impl( Dots dots ){
//' @export
//' @rdname rbind
// [[Rcpp::export]]
List rbind_all( ListOf<DataFrame> dots ){
List rbind_all( StrictListOf<DataFrame> dots ){
return rbind__impl(dots) ;
}

Expand Down

0 comments on commit d9c2d8e

Please sign in to comment.