forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbl-lazy.R
130 lines (105 loc) · 3.22 KB
/
tbl-lazy.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
tbl_lazy <- function(df) {
make_tbl("lazy", ops = op_base_local(df, env = parent.frame()))
}
lazy_frame <- function(...) {
tbl_lazy(data_frame(...))
}
#' @export
same_src.tbl_lazy <- function(x, y) {
inherits(y, "tbl_lazy")
}
#' @export
tbl_vars.tbl_lazy <- function(x) {
op_vars(x$ops)
}
#' @export
groups.tbl_lazy <- function(x) {
lapply(op_grps(x$ops), as.name)
}
#' @export
print.tbl_lazy <- function(x, ...) {
cat("Source: lazy\n")
cat("Vars : ", commas(op_vars(x$ops)), "\n", sep = "")
cat("Groups: ", commas(op_grps(x$ops)), "\n", sep = "")
cat("\n")
print(x$ops)
}
# Single table methods ----------------------------------------------------
#' @export
filter_.tbl_lazy <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
add_op_single("filter", .data, dots = dots)
}
#' @export
arrange_.tbl_lazy <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
add_op_single("arrange", .data, dots = dots)
}
#' @export
select_.tbl_lazy <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
add_op_single("select", .data, dots = dots)
}
#' @export
rename_.tbl_lazy <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
add_op_single("rename", .data, dots = dots)
}
#' @export
summarise_.tbl_lazy <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
add_op_single("summarise", .data, dots = dots)
}
#' @export
mutate_.tbl_lazy <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
add_op_single("mutate", .data, dots = dots)
}
#' @export
group_by_.tbl_lazy <- function(.data, ..., .dots, add = TRUE) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
add_op_single("group_by", .data, dots = dots, args = list(add = add))
}
#' @export
head.tbl_lazy <- function(x, n = 6L, ...) {
add_op_single("head", x, args = list(n = n))
}
#' @export
ungroup.tbl_lazy <- function(x, ...) {
add_op_single("ungroup", x)
}
#' @export
distinct_.tbl_lazy <- function(.data, ..., .dots, .keep_all = FALSE) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
add_op_single("distinct", .data, dots = dots, args = list(.keep_all = .keep_all))
}
# Dual table verbs ------------------------------------------------------------
add_op_join <- function(x, y, type, by = NULL, copy = FALSE,
suffix = c(".x", ".y"),
auto_index = FALSE, ...) {
by <- common_by(by, x, y)
y <- auto_copy(x, y, copy, indexes = if (auto_index) list(by$y))
x$ops <- op_double("join", x, y, args = list(
type = type,
by = by,
suffix = suffix
))
x
}
add_op_semi_join <- function(x, y, anti = FALSE, by = NULL, copy = FALSE,
auto_index = FALSE, ...) {
by <- common_by(by, x, y)
y <- auto_copy(x, y, copy, indexes = if (auto_index) list(by$y))
x$ops <- op_double("semi_join", x, y, args = list(
anti = anti,
by = by
))
x
}
add_op_set_op <- function(x, y, type, copy = FALSE, ...) {
y <- auto_copy(x, y, copy)
x$ops <- op_double("set_op", x, y, args = list(type = type))
x
}
# Currently the dual table verbs are defined on tbl_sql, because the
# because they definitions are bit too tightly connected to SQL.