Skip to content

Commit

Permalink
common_by() is S3 method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Müller committed Jun 15, 2016
1 parent 34c83be commit f3f6c0b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ S3method(collapse,data.frame)
S3method(collapse,tbl_sql)
S3method(collect,data.frame)
S3method(collect,tbl_sql)
S3method(common_by,"NULL")
S3method(common_by,character)
S3method(common_by,list)
S3method(compute,data.frame)
S3method(compute,tbl_sql)
S3method(copy_to,src_local)
Expand Down
19 changes: 9 additions & 10 deletions R/join.r
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,12 @@ anti_join <- function(x, y, by = NULL, copy = FALSE, ...) {
#'
#' @export
#' @keywords internal
common_by <- function(by = NULL, x, y) {
if (!is.null(by)) {
if (!is.list(by))
by <- common_by_from_vector(by)
common_by <- function(by = NULL, x, y) UseMethod("common_by", by)

check_by_list(by, x, y)
}
else
common_by_auto(x, y)
#' @export
common_by.character <- function(by, x, y) {
by <- common_by_from_vector(by)
common_by.list(by, x, y)
}

common_by_from_vector <- function(by) {
Expand All @@ -126,7 +123,8 @@ common_by_from_vector <- function(by) {
list(x = by_x, y = by_y)
}

check_by_list <- function(by, x, y) {
#' @export
common_by.list <- function(by, x, y) {
x_vars <- tbl_vars(x)
if (!all(by$x %in% x_vars)) {
stop("Join column not found in lhs: ", paste(setdiff(by$x, x_vars), collapse = ", "), call. = FALSE)
Expand All @@ -140,7 +138,8 @@ check_by_list <- function(by, x, y) {
by
}

common_by_auto <- function(x, y) {
#' @export
common_by.NULL <- function(by, x, y) {
by <- intersect(tbl_vars(x), tbl_vars(y))
if (length(by) == 0) {
stop("No common variables. Please specify `by` param.", call. = FALSE)
Expand Down

0 comments on commit f3f6c0b

Please sign in to comment.