-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.R
103 lines (97 loc) · 3.09 KB
/
index.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
#' index and related functions
#'
#' @export
#' @template args
#' @details
#' \itemize{
#' \item \code{index}/\code{index_} - queries like: \code{.[]}, \code{.[0]},
#' \code{.[1:5]},
#' \code{.["foo"]}
#' \item \code{indexif}/\code{indexif_} - queries like: \code{.["foo"]?}
#' \item \code{dotindex}/\code{dotindex_} - queries like: \code{.[].foo},
#' \code{.[].foo.bar}
#' }
#' @examples
#' str <- '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
#' str %>% index
#' '{"name":"JSON", "good":true}' %>% indexif(name)
#' '{"name":"JSON", "good":true}' %>% indexif(good)
#' '{"name":"JSON", "good":true}' %>% indexif(that)
#' '{"a": 1, "b": 1}' %>% index
#' '[]' %>% index
#' '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' %>% index(0)
#' '["a","b","c","d","e"]' %>% index(2)
#' '["a","b","c","d","e"]' %>% index('2:4')
#' '["a","b","c","d","e"]' %>% index('2:5')
#' '["a","b","c","d","e"]' %>% index(':3')
#' '["a","b","c","d","e"]' %>% index('-2:')
#'
#' str %>% index %>% select(bad = .name)
#'
#' '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' %>%
#' dotindex(name)
#' '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' %>%
#' dotindex(good)
#' '[{"name":"JSON", "good":{"foo":5}}, {"name":"XML", "good":{"foo":6}}]' %>%
#' dotindex(good)
#' '[{"name":"JSON", "good":{"foo":5}}, {"name":"XML", "good":{"foo":6}}]' %>%
#' dotindex(good.foo)
index <- function(.data, ...) {
index_(.data, .dots = lazyeval::lazy_dots(...))
}
#' @export
#' @rdname index
index_ <- function(.data, ..., .dots) {
pipe_autoexec(toggle = TRUE)
tmp <- lazyeval::all_dots(.dots, ...)
if (length(tmp) == 0) {
z <- '.[]'
} else {
z <- paste0(".[", collapse_vec(tmp[[1]]$expr), "]")
}
dots <- comb(tryargs(.data), structure(z, type = "index"))
structure(list(data = getdata(.data), args = dots), class = "jqr")
}
#' @export
#' @rdname index
indexif <- function(.data, ...) {
indexif_(.data, .dots = lazyeval::lazy_dots(...))
}
#' @export
#' @rdname index
indexif_ <- function(.data, ..., .dots) {
pipe_autoexec(toggle = TRUE)
tmp <- lazyeval::all_dots(.dots, ...)
if (length(tmp) == 0) {
z <- '.[]'
} else {
z <- sprintf('.["%s"]?', as.character(tmp[[1]]$expr))
# z <- sprintf('.[%s?]', as.character(tmp[[1]]$expr))
}
dots <- comb(tryargs(.data), structure(z, type = "indexif"))
structure(list(data = getdata(.data), dots), class = "jqr")
}
#' @export
#' @rdname index
dotindex <- function(.data, ...) {
dotindex_(.data, .dots = lazyeval::lazy_dots(...))
}
#' @export
#' @rdname index
dotindex_ <- function(.data, ..., .dots) {
pipe_autoexec(toggle = TRUE)
tmp <- lazyeval::all_dots(.dots, ...)
if (length(tmp) == 0) stop("no input supplied")
z <- sprintf(".[].%s", as.character(tmp[[1]]$expr))
dots <- comb(tryargs(.data), structure(z, type = "dotindex"))
structure(list(data = getdata(.data), dots), class = "jqr")
}
# helpers ----------------------
collapse_vec <- function(x) {
if (length(x) > 1) {
if (!any(sapply(x, is.numeric))) stop("Only supports numeric vectors", call. = FALSE)
paste0(min(x), ":", max(x))
} else {
x
}
}