Skip to content

Commit 8b993cf

Browse files
author
Florian Plaza Oñate
committed
Allow to assign empty vector or NULL to dimnames
1 parent 3b54aa1 commit 8b993cf

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

inst/include/Rcpp/vector/DimNameProxy.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,25 @@ namespace internal{
3434
data_(other.data_), dim_(other.dim_) {}
3535

3636
inline DimNameProxy& assign(SEXP other) {
37-
SEXP dims = Rf_getAttrib(data_, R_DimSymbol);
38-
if (INTEGER(dims)[dim_] != Rf_length(other)) {
39-
stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other));
40-
}
41-
SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol);
42-
if (Rf_isNull(dimnames)) {
43-
Shield<SEXP> new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims)));
44-
SET_VECTOR_ELT(new_dimnames, dim_, other);
45-
Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames);
46-
} else {
47-
SET_VECTOR_ELT(dimnames, dim_, other);
48-
}
37+
if (Rf_length(other) == 0)
38+
{
39+
Rf_setAttrib(data_, R_DimNamesSymbol, R_NilValue);
40+
} else {
41+
SEXP dims = Rf_getAttrib(data_, R_DimSymbol);
42+
43+
if (INTEGER(dims)[dim_] != Rf_length(other)) {
44+
stop("dimension extent is '%d' while length of names is '%d'", INTEGER(dims)[dim_], Rf_length(other));
45+
}
46+
47+
SEXP dimnames = Rf_getAttrib(data_, R_DimNamesSymbol);
48+
if (Rf_isNull(dimnames)) {
49+
Shield<SEXP> new_dimnames(Rf_allocVector(VECSXP, Rf_length(dims)));
50+
SET_VECTOR_ELT(new_dimnames, dim_, other);
51+
Rf_setAttrib(data_, R_DimNamesSymbol, new_dimnames);
52+
} else {
53+
SET_VECTOR_ELT(dimnames, dim_, other);
54+
}
55+
}
4956
return *this;
5057
}
5158

0 commit comments

Comments
 (0)