Skip to content

Commit 2bfd0d1

Browse files
committed
Correct behaviour when pushing zero-length object to DataFrame.
This triggers the warning when adding a zero-length object to a DataFrame. It is not a valid DataFrame, so calls `warning()`, and doesn't call `as.data.frame()`.
1 parent d800e2a commit 2bfd0d1

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

inst/include/Rcpp/DataFrame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ namespace Rcpp{
142142
}
143143
}
144144
for (it = Parent::begin(); it != Parent::end(); ++it) {
145-
if (Rf_xlength(*it) > 1 && max_rows % Rf_xlength(*it) != 0) {
145+
if (Rf_xlength(*it) == 0 || ( Rf_xlength(*it) > 1 && max_rows % Rf_xlength(*it) != 0 )) {
146146
// We have a column that is not an integer fraction of the largest
147147
invalid_column_size = true;
148148
}

inst/tinytest/cpp/DataFrame.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,14 @@ DataFrame DataFrame_PushReplicateLength(){
181181
df1.push_back(x);
182182
return df1;
183183
}
184+
185+
// [[Rcpp::export]]
186+
DataFrame DataFrame_PushZeroLength(){
187+
NumericVector u(2);
188+
NumericVector v(0);
189+
190+
191+
DataFrame df1 = DataFrame::create(_["u"] = u);
192+
df1.push_back(v);
193+
return df1;
194+
}

inst/tinytest/test_dataframe.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@ expect_warning( DataFrame_PushWrongSize() )
110110
df <- data.frame( u = c(1, 0), v = c(0, 0, 0, 0), x = c(2) )
111111
expect_true( is.data.frame( DataFrame_PushReplicateLength() ) )
112112
expect_equal( DataFrame_PushReplicateLength(), df )
113+
114+
# test.DataFrame.PushZeroLength <- function(){
115+
expect_warning( DataFrame_PushZeroLength())

0 commit comments

Comments
 (0)