Skip to content

Commit

Permalink
Handle xts objects without dim attribute in rbind()
Browse files Browse the repository at this point in the history
rbind() would throw an obscure error if one of the xts objects does
not have a dim attribute. We can handle this case even though all xts
objects should always have a dim attribute.

Fixes #361.
  • Loading branch information
joshuaulrich committed Jan 16, 2022
1 parent ecd9cd7 commit 6a605cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions inst/unitTests/runit.xts.methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ test.rbind_zero_length_non_zero_length_Date_errors <- function() {
xpe <- tryCatch(rbind(xpz, xp1), error = identity)
checkIdentical(zpe$message, xpe$message)
}
test.rbind_no_dim_does_not_error <- function() {
d <- rep(0.1, 2)
i <- rep(581910048, 2)

xts_no_dim <-
structure(d[1], class = c("xts", "zoo"),
index = structure(i[1], tzone = "UTC", tclass = "Date"))
xts_out <-
structure(d, class = c("xts", "zoo"), .Dim = 2:1,
index = structure(i, tzone = "UTC", tclass = "Date"))

xts_rbind <- rbind(xts_no_dim, xts_no_dim)
checkIdentical(xts_out, xts_rbind)
}

# Test that as.Date.numeric() works at the top level (via zoo::as.Date()),
# and for functions defined in the xts namespace even if xts::as.Date.numeric()
Expand Down
2 changes: 1 addition & 1 deletion src/rbind.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ return(result);
SEXP dim;
PROTECT(dim = allocVector(INTSXP, 2));
INTEGER(dim)[0] = truelen;
INTEGER(dim)[1] = INTEGER(getAttrib(x, R_DimSymbol))[1];
INTEGER(dim)[1] = ncx;
UNPROTECT(1);
setAttrib(result, R_DimSymbol, dim);
setAttrib(result, R_DimNamesSymbol, getAttrib(x, R_DimNamesSymbol));
Expand Down

0 comments on commit 6a605cc

Please sign in to comment.