File: dplyr.R

package info (click to toggle)
r-cran-ggvis 0.4.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,716 kB
  • sloc: sh: 25; makefile: 2
file content (353 lines) | stat: -rw-r--r-- 10,372 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#' Divide data into groups.
#'
#' @param x a visualisation
#' @param ... variables to group by.
#' @param add By default, when \code{add = FALSE}, \code{group_by} will
#'   override existing groups. To instead add to the existing groups,
#'   use \code{add = TRUE}
#' @importFrom dplyr group_by
#' @name group_by
#' @export
NULL


#' Dplyr verbs for ggvis.
#'
#' Applying a dplyr verb to a ggvis object creates a reactive transformation:
#' whenever the underlying data changes the transformation will be recomputed.
#'
#' @section Non-standard evaluation:
#' Both dplyr and shiny do non-standard evaluation, so to help each package
#' figure out when it should evaluate its code, reactive components in
#' these functions must be wrapped in \code{eval()}.
#'
#' @name dplyr-ggvis
#' @keywords internal
#' @examples
#' library(dplyr)
#' base <- mtcars %>% ggvis(~mpg, ~cyl) %>% layer_points()
#' base %>% group_by(cyl) %>% summarise(mpg = mean(mpg)) %>%
#'   layer_points(fill := "red", size := 100)
#'
#' base %>% filter(mpg > 25) %>% layer_points(fill := "red")
#'
#' base %>% mutate(cyl = jitter(cyl)) %>% layer_points(fill := "red")
#'
#' \dontrun{
#' # Dynamically restrict range using filter
#' mtcars %>% ggvis(~disp, ~mpg) %>%
#'    filter(cyl > eval(input_slider(0, 10))) %>%
#'    layer_points()
#'
#' # Dynamically compute box-cox transformation with mutate
#' bc <- function(x, lambda) {
#'   if (abs(lambda) < 1e-6) log(x) else (x ^ lambda - 1) / lambda
#' }
#' bc_slider <- input_slider(-2, 2, 1, step = 0.1)
#' mtcars %>%
#'  ggvis(~disp, ~mpg) %>%
#'  mutate(disp = bc(disp, eval(bc_slider))) %>%
#'  layer_points()
#' }
NULL

# Methods for ggvis objects ----------------------------------------------------

#' @importFrom dplyr groups
#' @export
#' @rdname dplyr-ggvis
groups.ggvis <- function(x) {
  shiny::isolate(dplyr::groups(x$cur_data()))
}

#' @importFrom dplyr group_by_
#' @export
#' @rdname dplyr-ggvis
group_by_.ggvis <- function(.data, ..., .dots, add = FALSE) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "group_by", function(data, args) {
    dplyr::group_by_(data, .dots = lapply(pieces$lazy, add_args, args),
      add = add)
  })
}

#' @importFrom dplyr ungroup
#' @export
#' @rdname dplyr-ggvis
ungroup.ggvis <- function(x) {
  register_computation(x, list(), "ungroup", function(data, args) {
    dplyr::ungroup(data)
  })
}


#' @importFrom dplyr summarise_
#' @rdname dplyr-ggvis
#' @export
summarise_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "summarise", function(data, args) {
    dplyr::summarise_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}

#' @importFrom dplyr mutate_
#' @rdname dplyr-ggvis
#' @export
mutate_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "mutate", function(data, args) {
    dplyr::mutate_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}

#' @importFrom dplyr arrange_
#' @rdname dplyr-ggvis
#' @export
arrange_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "arrange", function(data, args) {
    dplyr::arrange_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}

#' @importFrom dplyr select_
#' @rdname dplyr-ggvis
#' @export
select_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "select", function(data, args) {
    dplyr::select_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}

#' @importFrom dplyr filter_
#' @rdname dplyr-ggvis
#' @export
filter_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "filter", function(data, args) {
    dplyr::filter_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}

#' @importFrom dplyr distinct_
#' @rdname dplyr-ggvis
#' @export
distinct_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "distinct", function(data, args) {
    dplyr::distinct_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}

#' @importFrom dplyr slice_
#' @rdname dplyr-ggvis
#' @export
slice_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "slice", function(data, args) {
    dplyr::slice_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}

#' @importFrom dplyr rename_
#' @rdname dplyr-ggvis
#' @export
rename_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "rename", function(data, args) {
    dplyr::rename_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}

#' @importFrom dplyr transmute_
#' @rdname dplyr-ggvis
#' @export
transmute_.ggvis <- function(.data, ..., .dots) {
  dots <- lazyeval::all_dots(.dots, ...)
  pieces <- extract_lazy_inputs(dots)

  register_computation(.data, pieces$inputs, "transmute", function(data, args) {
    dplyr::transmute_(data, .dots = lapply(pieces$lazy, add_args, args))
  })
}


#' Extract reactive inputs from a lazy dots.
#'
#' This works by replacing each reactive expression (which must be wrapped
#' in \code{eval}), with a reference to \code{args$xyz}. Then this function
#' returns both the modified lazy dots, and a list of reactives that need
#' to be created.
#'
#' @noRd
#' @examples
#' # extract_inputs() works with language objects ------------------------------
#'
#' # Simple expressions are returned as is
#' extract_inputs(quote(1))
#' extract_inputs(quote(x))
#' extract_inputs(quote(x + y))
#'
#' # If the call contains eval, then the subexpression is evalutes, the
#' # original is replaced with a reference to args, and inputs gains
#' # a reactive broker
#' extract_inputs(quote(eval(input_slider(0, 100))))
#'
#' slider <- input_slider(0, 100)
#' extract_inputs(quote(eval(slider) + eval(slider)))
#'
#' # extract_lazy_inputs() works with lazy objects -----------------------------
#' library(lazyeval)
#' extract_lazy_inputs(lazy(x + y))
#' extract_lazy_inputs(lazy(x + eval(input_slider(0, 100))))
#'
#' s1 <- input_slider(0, 100)
#' s2 <- input_slider(0, 200)
#' extract_lazy_inputs(lazy_dots(x + eval(s1), eval(s2), eval(s1) / eval(s2)))
extract_lazy_inputs <- function(x) {
  if (inherits(x, "lazy_dots")) {
    pieces <- lapply(x, extract_lazy_inputs)

    lazy <- lazyeval::as.lazy_dots(pluck(pieces, "lazy"))
    inputs <- unlist(unname(pluck(pieces, "inputs")), recursive = FALSE)
    inputs <- inputs[!duplicated(names(inputs))]

    list(
      lazy = lazy,
      inputs = inputs
    )
  } else if (inherits(x, "lazy")) {
    new <- extract_inputs(x$expr, x$env)

    x$expr <- new$expr
    list(lazy = x, inputs = new$inputs)
  } else {
    stop("Unknown input type: ", paste0(class(x), collapse = "/"),
      call. = FALSE)
  }
}

extract_inputs <- function(expr, env = parent.frame()) {
  if (is.name(expr) || is.atomic(expr)) {
    # Base case
    list(expr = expr, inputs = list())
  } else if (is.call(expr) && identical(expr[[1]], quote(eval))) {
    # If it's a call to eval, it's an input and should be evaluated
    stopifnot(length(expr) == 2)

    input <- eval(expr[[2]], env)
    stopifnot(is.broker(input))
    nm <- names(attr(input, "broker", TRUE)$controls)

    list(
      expr = substitute(args$nm, list(nm = as.name(nm))),
      inputs = stats::setNames(list(input), nm)
    )
  } else if (is.call(expr)) {
    # Recursive over arguments and join back together again
    args_out <- lapply(expr[-1], extract_inputs, env = env)

    args <- pluck(args_out, "expr")
    inputs <- unlist(pluck(args_out, "inputs"), recursive = FALSE)
    inputs <- inputs[!duplicated(names(inputs))]

    list(
      expr = as.call(c(expr[[1]], args)),
      inputs = inputs
    )
  } else {
    stop("Don't know how to deal with input of type: ", class(expr)[[1]],
      call. = FALSE)
  }
}

# Given a lazy object, modify it so it's environment also gets to
# access the args list.
add_args <- function(x, args) {
  e <- new.env(parent = x$env)
  e$args <- args
  x$env <- e

  x
}



# Methods for reactive data frames --------------------------------------------

#' @rdname dplyr-ggvis
#' @export
groups.reactive <- function(x) reactive(dplyr::groups(x()))
#' @rdname dplyr-ggvis
#' @export
ungroup.reactive <- function(x) reactive(dplyr::ungroup(x()))
#' @rdname dplyr-ggvis
#' @export
group_by_.reactive <- function(.data, ..., .dots, add = FALSE) {
  reactive(dplyr::group_by_(.data(), ..., .dots = .dots, add = add))
}
#' @rdname dplyr-ggvis
#' @export
summarise_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::summarise_(.data(), ..., .dots = .dots))
}
#' @rdname dplyr-ggvis
#' @export
mutate_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::mutate_(.data(), ..., .dots = .dots))
}
#' @rdname dplyr-ggvis
#' @export
arrange_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::arrange_(.data(), ..., .dots = .dots))
}
#' @rdname dplyr-ggvis
#' @export
select_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::select_(.data(), ..., .dots = .dots))
}
#' @rdname dplyr-ggvis
#' @export
filter_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::filter_(.data(), ..., .dots = .dots))
}
#' @rdname dplyr-ggvis
#' @export
distinct_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::distinct_(.data(), ..., .dots = .dots))
}
#' @rdname dplyr-ggvis
#' @export
slice_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::slice_(.data(), ..., .dots = .dots))
}
#' @rdname dplyr-ggvis
#' @export
rename_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::rename_(.data(), ..., .dots = .dots))
}
#' @rdname dplyr-ggvis
#' @export
transmute_.reactive <- function(.data, ..., .dots) {
  reactive(dplyr::transmute_(.data(), ..., .dots = .dots))
}