diff --git a/NEWS.md b/NEWS.md index b59d54e58c..58544d9c33 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/inst/include/tools/FilteredListOf.h b/inst/include/tools/FilteredListOf.h new file mode 100644 index 0000000000..96fa9169e2 --- /dev/null +++ b/inst/include/tools/FilteredListOf.h @@ -0,0 +1,31 @@ +#ifndef dplyr_tools_FilteredListOf_H +#define dplyr_tools_FilteredListOf_H + +namespace Rcpp { + + template + class FilteredListOf { + public: + + FilteredListOf(SEXP data_) : data(data_){ + int n = data.size() ; + for( int i=0; i( data[indices[i]]) ; + } + + int size() const { + return indices.size() ; + } + + private: + List data ; + std::vector indices ; + } ; +} + +#endif diff --git a/inst/include/tools/StrictListOf.h b/inst/include/tools/StrictListOf.h new file mode 100644 index 0000000000..ec777c51bc --- /dev/null +++ b/inst/include/tools/StrictListOf.h @@ -0,0 +1,37 @@ +#ifndef dplyr_tools_FilteredListOf_H +#define dplyr_tools_FilteredListOf_H + +namespace Rcpp { + + template + class StrictListOf { + public: + + StrictListOf(SEXP data_) : data(data_){ + int n = data.size() ; + for( int i=0; i(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( data[i] ) ; + } + + int size() const { + return data.size() ; + } + + private: + List data ; + } ; +} + +#endif diff --git a/inst/include/tools/tools.h b/inst/include/tools/tools.h index f5c4163919..72aa699ee6 100644 --- a/inst/include/tools/tools.h +++ b/inst/include/tools/tools.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/inst/tests/test-rbind.r b/inst/tests/test-rbind.r index 4396a7e693..9b4cd6db72 100644 --- a/inst/tests/test-rbind.r +++ b/inst/tests/test-rbind.r @@ -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)) +}) + diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 98334adbcc..55df22bdf3 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -448,13 +448,13 @@ BEGIN_RCPP END_RCPP } // rbind_all -List rbind_all(ListOf dots); +List rbind_all(StrictListOf dots); RcppExport SEXP dplyr_rbind_all(SEXP dotsSEXP) { BEGIN_RCPP SEXP __sexp_result; { Rcpp::RNGScope __rngScope; - Rcpp::traits::input_parameter< ListOf >::type dots(dotsSEXP ); + Rcpp::traits::input_parameter< StrictListOf >::type dots(dotsSEXP ); List __result = rbind_all(dots); PROTECT(__sexp_result = Rcpp::wrap(__result)); } diff --git a/src/dplyr.cpp b/src/dplyr.cpp index 58f0807916..e49bb55ddb 100644 --- a/src/dplyr.cpp +++ b/src/dplyr.cpp @@ -1549,7 +1549,7 @@ List rbind__impl( Dots dots ){ //' @export //' @rdname rbind // [[Rcpp::export]] -List rbind_all( ListOf dots ){ +List rbind_all( StrictListOf dots ){ return rbind__impl(dots) ; }