Skip to content

Commit

Permalink
Closes #1347. last() dispatches xts::last() properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsrinivasan committed Sep 23, 2015
1 parent d884e3b commit 483d02a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 6 additions & 4 deletions R/last.R
Expand Up @@ -4,10 +4,12 @@
# We'd like last() on vectors to be fast, so that's a direct x[NROW(x)] as it was in data.table, otherwise use xts's.
# If xts is loaded higher than data.table, xts::last will work but slower.
last <- function(x, ...) {
if (nargs()==1L || !"package:xts" %in% search()) {
if (is.data.frame(x)) return(x[NROW(x),])
if (!length(x)) return(x) else return(x[[length(x)]]) # for vectors, [[ works like [
if (nargs()==1L) {
if (is.vector(x)) {
if (!length(x)) return(x) else return(x[[length(x)]]) # for vectors, [[ works like [
} else if (is.data.frame(x)) return(x[NROW(x),])
}
# fix with suggestion from Joshua, #1347
if (!"package:xts" %in% search()) tail(x,...)
xts::last(x,...) # UseMethod("last") doesn't find xts's methods, not sure what I did wrong.
}

2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,8 @@

2. `as.ITime(NA)` works as intended, [#1354](https://github.com/Rdatatable/data.table/issues/1354). Thanks @geneorama.

3. `last()` dispatches `xts::last()` properly again, [#1347](https://github.com/Rdatatable/data.table/issues/1347). Thanks to @JoshuaUlrich for spotting and suggesting the fix.

#### NOTES


Expand Down
8 changes: 8 additions & 0 deletions inst/tests/tests.Rraw
Expand Up @@ -6952,6 +6952,14 @@ test(1557.4, dt[, .SD, .SDcols=paste0("index", 1:i)], dt[, .SD, .SDcols=index1:i
# fix for #1354
test(1558, as.ITime(NA), setattr(NA_integer_, 'class', 'ITime'))

if (!"package:xts" %in% search()) {
# #1347, xts issue from Joshua
x = as.Date(1:5, origin="2015-01-01")
test(1559.1, last(x), tail(x, 1L))
} else {
test(1559.2, last(.xts(1:3,1:3)), .xts(1:3, 1:3)[3, ])
}

##########################


Expand Down

1 comment on commit 483d02a

@jangorecki
Copy link
Member

Choose a reason for hiding this comment

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

@arunsrinivasan shouldn't be tail(x, 1L) instead of tail(x, ...)?

Please sign in to comment.