forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.r
47 lines (41 loc) · 1.09 KB
/
view.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
# data(baseball, package = "plyr")
# players <- group_by(baseball, id)
# v <- view(players$obj, players$index)
# v$set_group(1)
# v$eval(quote(id))
view <- function(data, index, parent = parent.frame()) {
# Current group and rows
i <- 1
rows <- index[[1]]
set_group <- function(value) {
if (i == value) return(rows)
i <<- value
rows <<- index[[value]]
rows
}
# Tools to manage extra functions
fun_env <- new.env(parent = parent)
add_function <- function(name, fun) {
fun_env[[name]] <- fun
}
# Tools to manage active bindings
grp_env <- new.env(parent = fun_env, size = nrow(data))
add_binding <- function(name, fun) {
makeActiveBinding(name, fun, grp_env)
}
from_data <- function(col) {
force(col)
function(v) {
if (!missing(v)) stop("Immutable view")
.subset2(data, col)[rows]
}
}
for (name in names(data)) {
add_binding(name, from_data(name))
}
local_eval <- function(expr) {
eval(expr, grp_env)
}
list(set_group = set_group, eval = local_eval, add_function = add_function,
add_binding = add_binding)
}