Skip to content

Commit fab8b38

Browse files
waltersomeddelbuettel
authored andcommitted
Modify approach to copying attributes.
Make use of the fact set__() already does a conversion, and just use that. This has the benefit that it works with push_back(DataFrame), as well as push_back(Vector), and also handles adding vectors of different lengths properly.
1 parent d37dae3 commit fab8b38

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

inst/include/Rcpp/DataFrame.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,38 +85,26 @@ namespace Rcpp{
8585

8686
template <typename T>
8787
void push_back( const T& object){
88-
R_xlen_t NewSize = std::max(object.size(), static_cast<R_xlen_t>(nrow()));
8988
Parent::push_back(object);
90-
IntegerVector RowNames = Range(1, NewSize);
91-
Rf_setAttrib(Parent::get__(), R_RowNamesSymbol, RowNames);
92-
Rf_setAttrib(Parent::get__(), R_ClassSymbol, Rf_mkString("data.frame"));
89+
set__(Parent::get__());
9390
}
9491

9592
template <typename T>
9693
void push_back( const T& object, const std::string& name ){
97-
R_xlen_t NewSize = std::max(object.size(), static_cast<R_xlen_t>(nrow()));
9894
Parent::push_back(object, name);
99-
IntegerVector RowNames = Range(1, NewSize);
100-
Rf_setAttrib(Parent::get__(), R_RowNamesSymbol, RowNames);
101-
Rf_setAttrib(Parent::get__(), R_ClassSymbol, Rf_mkString("data.frame"));
95+
set__(Parent::get__());
10296
}
10397

10498
template <typename T>
10599
void push_front( const T& object){
106-
R_xlen_t NewSize = std::max(object.size(), static_cast<R_xlen_t>(nrow()));
107100
Parent::push_front(object);
108-
IntegerVector RowNames = Range(1, NewSize);
109-
Rf_setAttrib(Parent::get__(), R_RowNamesSymbol, RowNames);
110-
Rf_setAttrib(Parent::get__(), R_ClassSymbol, Rf_mkString("data.frame"));
101+
set__(Parent::get__());
111102
}
112103

113104
template <typename T>
114105
void push_front( const T& object, const std::string& name){
115-
R_xlen_t NewSize = std::max(object.size(), static_cast<R_xlen_t>(nrow()));
116106
Parent::push_front(object, name);
117-
IntegerVector RowNames = Range(1, NewSize);
118-
Rf_setAttrib(Parent::get__(), R_RowNamesSymbol, RowNames);
119-
Rf_setAttrib(Parent::get__(), R_ClassSymbol, Rf_mkString("data.frame"));
107+
set__(Parent::get__());
120108
}
121109

122110
// Offer multiple variants to accomodate both old interface here and signatures in other classes

inst/tinytest/cpp/DataFrame.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,30 @@ DataFrame DataFrame_PushFrontUnnamed(){
130130
df.push_front(v);
131131
return df;
132132
}
133+
134+
// [[Rcpp::export]]
135+
DataFrame DataFrame_PushFrontDataFrame(){
136+
NumericVector u(2);
137+
NumericVector v(2);
138+
NumericVector w(2);
139+
NumericVector x(2);
140+
141+
DataFrame df1 = DataFrame::create(_["u"] = u, _["v"] = v);
142+
DataFrame df2 = DataFrame::create(_["w"] = w, _["x"] = x);
143+
df1.push_front(df2);
144+
return df1;
145+
}
146+
147+
// [[Rcpp::export]]
148+
DataFrame DataFrame_PushBackDataFrame(){
149+
NumericVector u(2);
150+
NumericVector v(2);
151+
NumericVector w(2);
152+
NumericVector x(2);
153+
154+
DataFrame df1 = DataFrame::create(_["u"] = u, _["v"] = v);
155+
DataFrame df2 = DataFrame::create(_["w"] = w, _["x"] = x);
156+
df1.push_back(df2);
157+
return df1;
158+
}
159+

inst/tinytest/test_dataframe.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ expect_true( is.data.frame( DataFrame_PushBackNamed() ) )
7777
expect_equal( DataFrame_PushBackNamed(), df )
7878

7979
# test.DataFrame.PushBackUnamed <- function(){
80-
df <- data.frame( u = c(0, 0), c(0, 0), fix.empty.names = FALSE )
80+
df <- data.frame( u = c(0, 0), c(0, 0) )
8181
expect_true( is.data.frame( DataFrame_PushBackUnnamed() ) )
8282
expect_equal( DataFrame_PushBackUnnamed(), df )
8383

@@ -87,6 +87,17 @@ expect_true( is.data.frame( DataFrame_PushFrontNamed() ) )
8787
expect_equal( DataFrame_PushFrontNamed(), df )
8888

8989
# test.DataFrame.PushFrontUnnamed <- function(){
90-
df <- data.frame( c(0, 0), u = c(0, 0), fix.empty.names = FALSE )
90+
df <- data.frame( c(0, 0), u = c(0, 0) )
9191
expect_true( is.data.frame( DataFrame_PushFrontUnnamed() ) )
9292
expect_equal( DataFrame_PushFrontUnnamed(), df )
93+
94+
95+
# test.DataFrame.PushFrontDataFrame <- function(){
96+
df <- data.frame( w = c(0, 0), x = c(0, 0), u = c(0, 0), v = c(0, 0) )
97+
expect_true( is.data.frame( DataFrame_PushFrontDataFrame() ) )
98+
expect_equal( DataFrame_PushFrontDataFrame(), df )
99+
100+
# test.DataFrame.PushBackDataFrame <- function(){
101+
df <- data.frame( u = c(0, 0), v = c(0, 0), w = c(0, 0), x = c(0, 0) )
102+
expect_true( is.data.frame( DataFrame_PushBackDataFrame() ) )
103+
expect_equal( DataFrame_PushBackDataFrame(), df )

0 commit comments

Comments
 (0)