Skip to content

Commit

Permalink
Register S3 methods at load time
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Apr 21, 2017
1 parent e599015 commit e47cb5c
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 221 deletions.
97 changes: 0 additions & 97 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,137 +1,40 @@
# Generated by roxygen2: do not edit by hand


if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,arrange)
S3method(arrange,data.table)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,distinct)
S3method(distinct,data.table)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,do)
S3method(do,data.table)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,filter)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,group_by)
S3method(group_by,data.table)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,mutate)
S3method(mutate,data.table)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,rename)
S3method(rename,data.table)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,select)
S3method(select,data.table)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,slice)
S3method(slice,data.table)
}

if (utils::packageVersion("dplyr") > "0.5.0") {
importFrom(dplyr,summarise)
S3method(summarise,data.table)
}
S3method(all.equal,tbl_dt)
S3method(anti_join,data.table)
S3method(arrange_,data.table)
S3method(arrange_,grouped_dt)
S3method(arrange_,tbl_dt)
S3method(as.data.frame,tbl_dt)
S3method(as.tbl,data.table)
S3method(auto_copy,tbl_dt)
S3method(dimnames,tbl_dt)
S3method(distinct_,data.table)
S3method(distinct_,grouped_dt)
S3method(distinct_,tbl_dt)
S3method(do_,data.table)
S3method(do_,grouped_dt)
S3method(do_,tbl_dt)
S3method(filter_,data.table)
S3method(filter_,grouped_dt)
S3method(filter_,tbl_dt)
S3method(full_join,data.table)
S3method(group_by_,data.table)
S3method(group_size,grouped_dt)
S3method(groups,grouped_dt)
S3method(groups,tbl_dt)
S3method(head,tbl_dt)
S3method(inner_join,data.table)
S3method(left_join,data.table)
S3method(mutate_,data.table)
S3method(mutate_,grouped_dt)
S3method(mutate_,tbl_dt)
S3method(n_groups,grouped_dt)
S3method(print,grouped_dt)
S3method(print,tbl_dt)
S3method(rename_,data.table)
S3method(rename_,grouped_dt)
S3method(rename_,tbl_dt)
S3method(right_join,data.table)
S3method(same_src,tbl_dt)
S3method(sample_frac,grouped_dt)
S3method(sample_frac,tbl_dt)
S3method(sample_n,grouped_dt)
S3method(sample_n,tbl_dt)
S3method(select_,data.table)
S3method(select_,grouped_dt)
S3method(select_,tbl_dt)
S3method(semi_join,data.table)
S3method(slice_,data.table)
S3method(slice_,grouped_dt)
S3method(slice_,tbl_dt)
S3method(summarise_,data.table)
S3method(summarise_,grouped_dt)
S3method(summarise_,tbl_dt)
S3method(tail,tbl_dt)
S3method(tbl_vars,tbl_dt)
S3method(ungroup,data.table)
S3method(ungroup,grouped_dt)
S3method(ungroup,tbl_dt)
export(.datatable.aware)
export(grouped_dt)
export(is.grouped_dt)
export(src_dt)
export(tbl_dt)
importFrom(dplyr,anti_join)
importFrom(dplyr,arrange_)
importFrom(dplyr,as.tbl)
importFrom(dplyr,auto_copy)
importFrom(dplyr,distinct_)
importFrom(dplyr,do_)
importFrom(dplyr,filter_)
importFrom(dplyr,full_join)
importFrom(dplyr,group_by_)
importFrom(dplyr,group_size)
importFrom(dplyr,groups)
importFrom(dplyr,inner_join)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate_)
importFrom(dplyr,n_groups)
importFrom(dplyr,rename_)
importFrom(dplyr,right_join)
importFrom(dplyr,same_src)
importFrom(dplyr,sample_frac)
importFrom(dplyr,sample_n)
importFrom(dplyr,select_)
importFrom(dplyr,semi_join)
importFrom(dplyr,slice_)
importFrom(dplyr,summarise_)
importFrom(dplyr,tbl_vars)
Expand Down
67 changes: 66 additions & 1 deletion R/compat-dplyr-0.6.0.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,74 @@

.onLoad <- function(libname, pkgname) {
if (utils::packageVersion("dplyr") > "0.5.0") {
register_s3_method("dplyr", "filter", "data.table", filter.data.table)
register_s3_method("dplyr", "do", "data.table")
register_s3_method("dplyr", "group_by", "data.table")
register_s3_method("dplyr", "distinct", "data.table")
register_s3_method("dplyr", "filter", "data.table")
register_s3_method("dplyr", "summarise", "data.table")
register_s3_method("dplyr", "mutate", "data.table")
register_s3_method("dplyr", "arrange", "data.table")
register_s3_method("dplyr", "select", "data.table")
register_s3_method("dplyr", "rename", "data.table")
register_s3_method("dplyr", "slice", "data.table")
}

register_s3_method("dplyr", "do_", "data.table")
register_s3_method("dplyr", "do_", "tbl_dt")

register_s3_method("base", "print", "grouped_dt")
register_s3_method("dplyr", "groups", "grouped_dt")
register_s3_method("dplyr", "group_size", "grouped_dt")
register_s3_method("dplyr", "n_groups", "grouped_dt")
register_s3_method("dplyr", "ungroup", "grouped_dt")

register_s3_method("dplyr", "group_by_", "data.table")
register_s3_method("dplyr", "do_", "grouped_dt")
register_s3_method("dplyr", "distinct_", "grouped_dt")

register_s3_method("dplyr", "inner_join", "data.table")
register_s3_method("dplyr", "left_join", "data.table")
register_s3_method("dplyr", "right_join", "data.table")
register_s3_method("dplyr", "semi_join", "data.table")
register_s3_method("dplyr", "anti_join", "data.table")
register_s3_method("dplyr", "full_join", "data.table")

register_s3_method("dplyr", "sample_n", "tbl_dt")
register_s3_method("dplyr", "sample_n", "grouped_dt")
register_s3_method("dplyr", "sample_frac", "tbl_dt")
register_s3_method("dplyr", "sample_frac", "grouped_dt")

register_s3_method("dplyr", "distinct_", "data.table")
register_s3_method("dplyr", "distinct_", "tbl_dt")

register_s3_method("dplyr", "filter_", "grouped_dt")
register_s3_method("dplyr", "filter_", "tbl_dt")
register_s3_method("dplyr", "filter_", "data.table")

register_s3_method("dplyr", "summarise_", "grouped_dt")
register_s3_method("dplyr", "summarise_", "tbl_dt")
register_s3_method("dplyr", "summarise_", "data.table")

register_s3_method("dplyr", "mutate_", "grouped_dt")
register_s3_method("dplyr", "mutate_", "tbl_dt")
register_s3_method("dplyr", "mutate_", "data.table")

register_s3_method("dplyr", "arrange_", "grouped_dt")
register_s3_method("dplyr", "arrange_", "tbl_dt")
register_s3_method("dplyr", "arrange_", "data.table")

register_s3_method("dplyr", "select_", "grouped_dt")
register_s3_method("dplyr", "select_", "tbl_dt")
register_s3_method("dplyr", "select_", "data.table")

register_s3_method("dplyr", "rename_", "grouped_dt")
register_s3_method("dplyr", "rename_", "tbl_dt")
register_s3_method("dplyr", "rename_", "data.table")

register_s3_method("dplyr", "slice_", "grouped_dt")
register_s3_method("dplyr", "slice_", "tbl_dt")
register_s3_method("dplyr", "slice_", "data.table")

invisible()
}

Expand Down
3 changes: 0 additions & 3 deletions R/do.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ do.data.table <- function(.data, ...) {
}

#' @importFrom dplyr do_
#' @export
do_.data.table <- function(.data, ..., .dots) {
out <- do_(as.data.frame(.data), ..., .dots = .dots)
data.table::as.data.table(out)
}

#' @export
do_.tbl_dt <- function(.data, ..., .dots) {
out <- do_(as.data.frame(.data), ..., .dots = .dots)
tbl_dt(out)
Expand Down
19 changes: 1 addition & 18 deletions R/grouped-dt.r
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ grouped_dt <- function(data, vars, copy = TRUE) {
data
}

#' @export
groups.grouped_dt <- function(x) {
attr(x, "vars")
}
Expand All @@ -47,7 +46,6 @@ groups.grouped_dt <- function(x) {
#' @export
is.grouped_dt <- function(x) inherits(x, "grouped_dt")

#' @export
print.grouped_dt <- function(x, ..., n = NULL, width = NULL) {
cat("Source: local data table ", dplyr::dim_desc(x), "\n", sep = "")
cat("Groups: ", commas(deparse_all(dplyr::groups(x))), "\n", sep = "")
Expand All @@ -56,36 +54,23 @@ print.grouped_dt <- function(x, ..., n = NULL, width = NULL) {
invisible(x)
}

#' @importFrom dplyr group_size
#' @export
group_size.grouped_dt <- function(x) {
dplyr::summarise_(x, n = ~n())$n
}

#' @importFrom dplyr n_groups
#' @export
n_groups.grouped_dt <- function(x) {
nrow(dt_subset(x, , quote(list(1))))
}

#' @rawNamespace
#' if (utils::packageVersion("dplyr") > "0.5.0") {
#' importFrom(dplyr,group_by)
#' S3method(group_by,data.table)
#' }
#' @importFrom dplyr group_by_
group_by.data.table <- function(.data, ..., add = FALSE) {
group_by_(.data, .dots = lazyeval::lazy_dots(...), add = add)
}

#' @export
#' @importFrom dplyr group_by_
group_by_.data.table <- function(.data, ..., .dots, add = FALSE) {
groups <- dplyr::group_by_prepare(.data, ..., .dots = .dots, add = add)
grouped_dt(groups$data, groups$groups)
}

#' @export
#' @importFrom dplyr ungroup
ungroup.grouped_dt <- function(x, ...) {
data.table::setattr(x, "vars", NULL)
data.table::setattr(x, "class", setdiff(class(x), "grouped_dt"))
Expand All @@ -95,7 +80,6 @@ ungroup.grouped_dt <- function(x, ...) {

# Do ---------------------------------------------------------------------------

#' @export
do_.grouped_dt <- function(.data, ..., .dots) {
args <- lazyeval::all_dots(.dots, ...)
env <- lazyeval::common_env(args)
Expand Down Expand Up @@ -139,7 +123,6 @@ named_args <- function(args) {

# Set operations ---------------------------------------------------------------

#' @export
distinct_.grouped_dt <- function(.data, ..., .dots) {
groups <- lazyeval::as.lazy_dots(groups(.data))
dist <- distinct_vars(.data, ..., .dots = c(.dots, groups))
Expand Down
12 changes: 0 additions & 12 deletions R/joins.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,15 @@ join_dt <- function(op) {
# nocov end
}

#' @export
#' @rdname join.tbl_dt
#' @importFrom dplyr inner_join
inner_join.data.table <- join_dt({merge(x, y, by = by$x, allow.cartesian = TRUE)})

#' @export
#' @importFrom dplyr left_join
#' @rdname join.tbl_dt
left_join.data.table <- join_dt({merge(x, y, by = by$x, all.x = TRUE, allow.cartesian = TRUE)})

#' @export
#' @importFrom dplyr right_join
#' @rdname join.tbl_dt
right_join.data.table <- join_dt(merge(x, y, by = by$x, all.y = TRUE, allow.cartesian = TRUE))

#' @export
#' @importFrom dplyr semi_join
#' @rdname join.tbl_dt
semi_join.data.table <- join_dt({
# http://stackoverflow.com/questions/18969420/perform-a-semi-join-with-data-table
Expand All @@ -79,13 +71,9 @@ semi_join.data.table <- join_dt({
x[w]
})

#' @export
#' @importFrom dplyr anti_join
#' @rdname join.tbl_dt
anti_join.data.table <- join_dt({x[!y, allow.cartesian = TRUE]})

#' @export
#' @importFrom dplyr full_join
#' @rdname join.tbl_dt
# http://stackoverflow.com/a/15170956/946850
full_join.data.table <- join_dt({merge(x, y, by = by$x, all = TRUE, allow.cartesian = TRUE)})
6 changes: 0 additions & 6 deletions R/sample.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#' @importFrom dplyr sample_n
#' @export
sample_n.tbl_dt <- function(tbl, size, replace = FALSE, weight = NULL,
.env = parent.frame()) {
tbl_dt(NextMethod())
}

#' @export
sample_n.grouped_dt <- function(tbl, size, replace = FALSE, weight = NULL,
.env = parent.frame()) {

Expand All @@ -18,14 +15,11 @@ sample_n.grouped_dt <- function(tbl, size, replace = FALSE, weight = NULL,
grouped_dt(tbl[idx], groups(tbl))
}

#' @importFrom dplyr sample_frac
#' @export
sample_frac.tbl_dt <- function(tbl, size = 1, replace = FALSE, weight = NULL,
.env = parent.frame()) {
tbl_dt(NextMethod())
}

#' @export
sample_frac.grouped_dt <- function(tbl, size = 1, replace = FALSE, weight = NULL,
.env = parent.frame()) {

Expand Down
8 changes: 0 additions & 8 deletions R/sets.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@

# Set operations ---------------------------------------------------------------

#' @rawNamespace
#' if (utils::packageVersion("dplyr") > "0.5.0") {
#' importFrom(dplyr,distinct)
#' S3method(distinct,data.table)
#' }
distinct.data.table <- function(.data, ..., .keep_all = FALSE) {
distinct_(.data, .dots = lazyeval::lazy_dots(...), .keep_all = .keep_all)
}

#' @export
#' @importFrom dplyr distinct_
distinct_.data.table <- function(.data, ..., .dots, .keep_all = FALSE) {
dist <- distinct_vars(.data, ..., .dots = .dots)
Expand All @@ -27,8 +21,6 @@ distinct_.data.table <- function(.data, ..., .dots, .keep_all = FALSE) {

res
}

#' @export
distinct_.tbl_dt <- function(.data, ..., .dots) {
tbl_dt(NextMethod(), copy = FALSE)
}
Expand Down
Loading

0 comments on commit e47cb5c

Please sign in to comment.