Skip to content

Commit

Permalink
Fix Rdatatable#1347, dispatch if last masks xts::last
Browse files Browse the repository at this point in the history
data.table::last is intended to work with xts::last, which is a S3
generic. e011351 appears to have caused a regression that prevents
xts::last from dispatching.

Revert e011351, but keep the is.data.frame check on the happy path, so
it will be fast for data.table objects. Return xts::last if xts is on
the search path. Otherwise, return tail(x, ...) instead of throwing an
error (the previous behavior).
  • Loading branch information
joshuaulrich committed Sep 23, 2015
1 parent 8c5418b commit bd9f1b9
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions R/last.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
# 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 (nargs()==1L) {
if (is.data.frame(x)) return(x[NROW(x),])
if (!length(x)) return(x) else return(x[[length(x)]]) # for vectors, [[ works like [
if (is.vector(x)) {
if (!length(x)) return(x) else return(x[[length(x)]]) # for vectors, [[ works like [
}
}
xts::last(x,...) # UseMethod("last") doesn't find xts's methods, not sure what I did wrong.
if ("package:xts" %in% search())
return(xts::last(x,...))
tail(x, ...)
}

0 comments on commit bd9f1b9

Please sign in to comment.