Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement push_back and push_front for DataFrame. #1099

Merged
merged 7 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
* inst/NEWS.Rd: Idem
* vignettes/rmd/Rcpp.bib: Idem
* inst/bib/Rcpp.bib: Idem
2020-07-01 Walter Somerville <[email protected]>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

  • in chronological order
  • with proper empty lines

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just sent a trivial commit back to you for that so make sure you pull to your box.


* inst/include/Rcpp/DataFrame.h: Implement explict push_back and
push_front for DataFrame, which takes care to set the class and
row.names attributes.
* inst/tinyTest/test_dataframe.R: Add in tests for push_back/push_front
* inst/tinyTest/cpp/DataFrame.cpp: Idem

2020-06-27 Dirk Eddelbuettel <[email protected]>

Expand Down
48 changes: 48 additions & 0 deletions inst/include/Rcpp/DataFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ namespace Rcpp{
return LENGTH(rn);
}

template <typename T>
void push_back( const T& object){
Parent::push_back(object);
set_type_after_push();
}

template <typename T>
void push_back( const T& object, const std::string& name ){
Parent::push_back(object, name);
set_type_after_push();
}

template <typename T>
void push_front( const T& object){
Parent::push_front(object);
set_type_after_push();
}

template <typename T>
void push_front( const T& object, const std::string& name){
Parent::push_front(object, name);
set_type_after_push();
}

// Offer multiple variants to accomodate both old interface here and signatures in other classes
inline int nrows() const { return DataFrame_Impl::nrow(); }
inline int rows() const { return DataFrame_Impl::nrow(); }
Expand All @@ -106,6 +130,30 @@ namespace Rcpp{
}
}

void set_type_after_push(){
int max_rows = 0;
bool invalid_column_size = false;
SEXP data = Parent::get__();
List::iterator it;
// Get the maximum number of rows
for (it = Parent::begin(); it != Parent::end(); ++it) {
if (Rf_xlength(*it) > max_rows) {
max_rows = Rf_xlength(*it);
}
}
for (it = Parent::begin(); it != Parent::end(); ++it) {
if (Rf_xlength(*it) > 1 && max_rows % Rf_xlength(*it) != 0) {
// We have a column that is not an integer fraction of the largest
invalid_column_size = true;
}
}
if (invalid_column_size) {
warning("Column sizes are not equal in DataFrame::push_back, object degrading to List\n");
} else {
set__(Parent::get__());
}
}

static DataFrame_Impl from_list( Parent obj ){
bool use_default_strings_as_factors = true ;
bool strings_as_factors = true ;
Expand Down
87 changes: 87 additions & 0 deletions inst/tinytest/cpp/DataFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,90 @@ IntegerVector DataFrame_nrow( DataFrame df){
IntegerVector DataFrame_ncol( DataFrame df){
return IntegerVector::create(df.ncol(), df.cols());
}

// [[Rcpp::export]]
DataFrame DataFrame_PushBackNamed(){
NumericVector u(2);
NumericVector v(2);
DataFrame df = DataFrame::create(_["u"] = u);
df.push_back(v, "v");
return df;
}

// [[Rcpp::export]]
DataFrame DataFrame_PushBackUnnamed(){
NumericVector u(2);
NumericVector v(2);
DataFrame df = DataFrame::create(_["u"] = u);
df.push_back(v);
return df;
}

// [[Rcpp::export]]
DataFrame DataFrame_PushFrontNamed(){
NumericVector u(2);
NumericVector v(2);
DataFrame df = DataFrame::create(_["u"] = u);
df.push_front(v, "v");
return df;
}

// [[Rcpp::export]]
DataFrame DataFrame_PushFrontUnnamed(){
NumericVector u(2);
NumericVector v(2);
DataFrame df = DataFrame::create(_["u"] = u);
df.push_front(v);
return df;
}

// [[Rcpp::export]]
DataFrame DataFrame_PushFrontDataFrame(){
NumericVector u(2);
NumericVector v(2);
NumericVector w(2);
NumericVector x(2);

DataFrame df1 = DataFrame::create(_["u"] = u, _["v"] = v);
DataFrame df2 = DataFrame::create(_["w"] = w, _["x"] = x);
df1.push_front(df2);
return df1;
}

// [[Rcpp::export]]
DataFrame DataFrame_PushBackDataFrame(){
NumericVector u(2);
NumericVector v(2);
NumericVector w(2);
NumericVector x(2);

DataFrame df1 = DataFrame::create(_["u"] = u, _["v"] = v);
DataFrame df2 = DataFrame::create(_["w"] = w, _["x"] = x);
df1.push_back(df2);
return df1;
}

// [[Rcpp::export]]
DataFrame DataFrame_PushWrongSize(){
NumericVector u(2);
NumericVector v(3);

DataFrame df1 = DataFrame::create(_["u"] = u);
df1.push_back(v);
return df1;
}

// [[Rcpp::export]]
DataFrame DataFrame_PushReplicateLength(){
NumericVector u(2);
NumericVector v(4);
NumericVector x(1);

u[0] = 1;
x[0] = 2;

DataFrame df1 = DataFrame::create(_["u"] = u);
df1.push_back(v);
df1.push_back(x);
return df1;
}
40 changes: 40 additions & 0 deletions inst/tinytest/test_dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,43 @@ expect_equal( DataFrame_nrow( df ), rep(nrow(df), 2) )
# test.DataFrame.ncol <- function(){
df <- data.frame( x = 1:10, y = 1:10 )
expect_equal( DataFrame_ncol( df ), rep(ncol(df), 2) )

# test.DataFrame.PushBackNamed <- function(){
df <- data.frame( u = c(0, 0), v = c(0, 0) )
expect_true( is.data.frame( DataFrame_PushBackNamed() ) )
expect_equal( DataFrame_PushBackNamed(), df )

# test.DataFrame.PushBackUnamed <- function(){
df <- data.frame( u = c(0, 0), c(0, 0) )
expect_true( is.data.frame( DataFrame_PushBackUnnamed() ) )
expect_equal( DataFrame_PushBackUnnamed(), df )

# test.DataFrame.PushFrontNamed <- function(){
df <- data.frame( v = c(0, 0), u = c(0, 0) )
expect_true( is.data.frame( DataFrame_PushFrontNamed() ) )
expect_equal( DataFrame_PushFrontNamed(), df )

# test.DataFrame.PushFrontUnnamed <- function(){
df <- data.frame( c(0, 0), u = c(0, 0) )
expect_true( is.data.frame( DataFrame_PushFrontUnnamed() ) )
expect_equal( DataFrame_PushFrontUnnamed(), df )


# test.DataFrame.PushFrontDataFrame <- function(){
df <- data.frame( w = c(0, 0), x = c(0, 0), u = c(0, 0), v = c(0, 0) )
expect_true( is.data.frame( DataFrame_PushFrontDataFrame() ) )
expect_equal( DataFrame_PushFrontDataFrame(), df )

# test.DataFrame.PushBackDataFrame <- function(){
df <- data.frame( u = c(0, 0), v = c(0, 0), w = c(0, 0), x = c(0, 0) )
expect_true( is.data.frame( DataFrame_PushBackDataFrame() ) )
expect_equal( DataFrame_PushBackDataFrame(), df )

# test.DataFrame.PushWrongSize <- function(){
df <- data.frame( u = c(0, 0), v = c(0, 0), w = c(0, 0), x = c(0, 0) )
expect_warning( DataFrame_PushWrongSize() )

# test.DataFrame.PushReplicateLength <- function(){
df <- data.frame( u = c(1, 0), v = c(0, 0, 0, 0), x = c(2) )
expect_true( is.data.frame( DataFrame_PushReplicateLength() ) )
expect_equal( DataFrame_PushReplicateLength(), df )