-
Notifications
You must be signed in to change notification settings - Fork 1k
fix first-last xts dispatch #4053 #4065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,84 @@ | ||
|
|
||
| # data.table defined last(x) with no arguments, just for last. If you need the last 10 then use tail(x,10). | ||
| # This last is implemented this way for compatibility with xts::last which is S3 generic taking 'n' and 'keep' arguments | ||
| # 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. | ||
| # for xts class objects it will dispatch to xts::last | ||
| # reworked to avoid loading xts namespace (#3857) then again to fix dispatching of xts class (#4053) | ||
| last = function(x, n=1L, ...) { | ||
| if (nargs()==1L) { | ||
| if (is.vector(x) || is.atomic(x)) { | ||
| if (!length(x)) x else x[[length(x)]] | ||
| } else if (is.data.frame(x)) { | ||
| x[NROW(x),] | ||
| verbose = isTRUE(getOption("datatable.verbose", FALSE)) | ||
| if (!inherits(x, "xts")) { | ||
| if (nargs()>1L) { | ||
| if ("package:xts" %chin% search()) { | ||
| if (verbose) | ||
| cat("last: using xts::last: !is.xts(x) & nargs>1 & 'package:xts'%in%search()\n") | ||
| xts::last(x, n=n, ...) | ||
| } else { | ||
| # nocov start | ||
| if (verbose) | ||
| cat("last: using utils::tail: !is.xts(x) & nargs>1 & !'package:xts'%in%search()\n") | ||
| utils::tail(x, n=n, ...) | ||
| # nocov end | ||
| } | ||
| } else { | ||
| dx = dim(x) | ||
| if (is.null(dx)) { | ||
| if (verbose) | ||
| cat("last: using 'x[[length(x)]]': !is.xts(x) & !nargs>1 & is.null(dim(x))\n") | ||
| lx = length(x) | ||
| if (!lx) x else x[[lx]] | ||
| } else if (is.data.frame(x)) { | ||
| if (verbose) | ||
| cat("last: using 'x[nrow(x),]': !is.xts(x) & !nargs>1 & is.data.frame(x)\n") | ||
| x[dx[1L], , drop=FALSE] | ||
| } else { | ||
| if (verbose) | ||
| cat("last: using utils::tail: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)\n") | ||
| utils::tail(x, n=n, ...) | ||
| } | ||
| } | ||
| } else if ("package:xts" %chin% search()) { | ||
| if (!requireNamespace("xts", quietly=TRUE)) | ||
| stop("internal error, package:xts is on search path but could not be loaded via requireNamespace") # nocov | ||
| if (isTRUE(getOption("datatable.verbose", FALSE))) | ||
| cat("last: using xts::last\n") | ||
| xts::last(x, n=n, ...) # UseMethod("last") doesn't find xts's methods, not sure what I did wrong. | ||
| } else { | ||
| tail(x, n=n, ...) # nocov | ||
| if (!requireNamespace("xts", quietly=TRUE)) | ||
| stop("'xts' class passed to data.table::last function but 'xts' is not available, you should have 'xts' installed already") # nocov | ||
| if (verbose) | ||
| cat("last: using xts::last: is.xts(x)\n") | ||
| xts::last(x, n=n, ...) | ||
| } | ||
| } | ||
|
|
||
| # first(), similar to last(), not sure why this wasn't exported in the first place... | ||
| first = function(x, n=1L, ...) { | ||
| if (nargs()==1L) { | ||
| if (is.vector(x) || is.atomic(x)) { | ||
| if (!length(x)) x else x[[1L]] | ||
| } else if (is.data.frame(x)) { | ||
| if (!NROW(x)) x else x[1L,] | ||
| verbose = isTRUE(getOption("datatable.verbose", FALSE)) | ||
| if (!inherits(x, "xts")) { | ||
| if (nargs()>1L) { | ||
| if ("package:xts" %chin% search()) { | ||
| if (verbose) | ||
| cat("first: using xts::first: !is.xts(x) & nargs>1 & 'package:xts'%in%search()\n") | ||
| xts::first(x, n=n, ...) | ||
| } else { | ||
| # nocov start | ||
| if (verbose) | ||
| cat("first: using utils::head: !is.xts(x) & nargs>1 & !'package:xts'%in%search()\n") | ||
| utils::head(x, n=n, ...) | ||
| # nocov end | ||
| } | ||
| } else { | ||
| dx = dim(x) | ||
| if (is.null(dx)) { | ||
| if (verbose) | ||
| cat("first: using 'x[[1L]]': !is.xts(x) & !nargs>1 & is.null(dim(x))\n") | ||
| lx = length(x) | ||
| if (!lx) x else x[[1L]] | ||
| } else if (is.data.frame(x)) { | ||
| if (verbose) | ||
| cat("first: using 'x[1L,]': !is.xts(x) & !nargs>1 & is.data.frame(x)\n") | ||
| if (!dx[1L]) x else x[1L, , drop=FALSE] | ||
| } else { | ||
| if (verbose) | ||
| cat("first: using utils::head: !is.xts(x) & !nargs>1 & !is.null(dim(x)) & !is.data.frame(x)\n") | ||
| utils::head(x, n=n, ...) | ||
| } | ||
| } | ||
| } else if ("package:xts" %chin% search()) { | ||
| } else { | ||
| if (!requireNamespace("xts", quietly=TRUE)) | ||
| stop("internal error, package:xts is on search path but could not be loaded via requireNamespace") # nocov | ||
| if (isTRUE(getOption("datatable.verbose", FALSE))) | ||
| cat("first: using xts::first\n") | ||
| stop("'xts' class passed to data.table::first function but 'xts' is not available, you should have 'xts' installed already") # nocov | ||
| if (verbose) | ||
| cat("first: using xts::first: is.xts(x)\n") | ||
| xts::first(x, n=n, ...) | ||
| } else { | ||
| head(x, n=n, ...) # nocov | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this
tail(df)was actually a mistake in old unit test, it was now amended tolast(df)as it should be in the first place