Skip to content

Commit 10ddeb3

Browse files
committed
Proper registration plotly methods for dplyr generics
1 parent fa7768a commit 10ddeb3

File tree

10 files changed

+179
-88
lines changed

10 files changed

+179
-88
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ Suggests:
7272
plotlyGeoAssets,
7373
forcats
7474
LazyData: true
75-
RoxygenNote: 7.1.0
75+
RoxygenNote: 7.1.1
7676
Encoding: UTF-8
7777
Roxygen: list(markdown = TRUE)

NAMESPACE

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
S3method(api_create,data.frame)
44
S3method(api_create,ggplot)
55
S3method(api_create,plotly)
6-
S3method(arrange_,plotly)
7-
S3method(distinct_,plotly)
8-
S3method(do_,plotly)
96
S3method(embed_notebook,plotly)
10-
S3method(filter_,plotly)
117
S3method(fortify,SharedData)
128
S3method(geom2trace,GeomBar)
139
S3method(geom2trace,GeomBlank)
@@ -25,12 +21,9 @@ S3method(ggplotly,"NULL")
2521
S3method(ggplotly,ggmatrix)
2622
S3method(ggplotly,ggplot)
2723
S3method(ggplotly,plotly)
28-
S3method(group_by_,plotly)
29-
S3method(groups,plotly)
3024
S3method(layout,matrix)
3125
S3method(layout,plotly)
3226
S3method(layout,shiny.tag.list)
33-
S3method(mutate_,plotly)
3427
S3method(plotly_build,"NULL")
3528
S3method(plotly_build,gg)
3629
S3method(plotly_build,list)
@@ -40,10 +33,6 @@ S3method(print,api_grid)
4033
S3method(print,api_grid_local)
4134
S3method(print,api_plot)
4235
S3method(print,plotly_data)
43-
S3method(rename_,plotly)
44-
S3method(select_,plotly)
45-
S3method(slice_,plotly)
46-
S3method(summarise_,plotly)
4736
S3method(to_basic,GeomAbline)
4837
S3method(to_basic,GeomAnnotationMap)
4938
S3method(to_basic,GeomArea)
@@ -78,8 +67,6 @@ S3method(to_basic,GeomTile)
7867
S3method(to_basic,GeomViolin)
7968
S3method(to_basic,GeomVline)
8069
S3method(to_basic,default)
81-
S3method(transmute_,plotly)
82-
S3method(ungroup,plotly)
8370
export("%>%")
8471
export(TeX)
8572
export(add_annotations)

R/onLoad.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.onLoad <- function(...) {
2+
dplyr_generics <- c(
3+
"groups", "ungroup", "group_by", "summarise", "mutate", "do", "arrange",
4+
"select", "filter", "distinct", "slice", "rename", "transmute"
5+
)
6+
for (generic in dplyr_generics) {
7+
register_s3_method("dplyr", generic, "plotly")
8+
}
9+
}
10+
11+
# copy/pasta from shiny:::register_s3_method
12+
register_s3_method <- function(pkg, generic, class, fun = NULL) {
13+
stopifnot(is.character(pkg), length(pkg) == 1)
14+
stopifnot(is.character(generic), length(generic) == 1)
15+
stopifnot(is.character(class), length(class) == 1)
16+
17+
if (is.null(fun)) {
18+
fun <- get(paste0(generic, ".", class), envir = parent.frame())
19+
} else {
20+
stopifnot(is.function(fun))
21+
}
22+
23+
if (pkg %in% loadedNamespaces()) {
24+
registerS3method(generic, class, fun, envir = asNamespace(pkg))
25+
}
26+
27+
# Always register hook in case pkg is loaded at some
28+
# point the future (or, potentially, but less commonly,
29+
# unloaded & reloaded)
30+
setHook(
31+
packageEvent(pkg, "onLoad"),
32+
function(...) {
33+
registerS3method(generic, class, fun, envir = asNamespace(pkg))
34+
}
35+
)
36+
}

R/plotly_data.R

Lines changed: 99 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -104,126 +104,159 @@ highlight_key <- function(...) {
104104
crosstalk::SharedData$new(...)
105105
}
106106

107+
# ---------------------------------------------------------------------------
108+
# dplyr methods
109+
# ---------------------------------------------------------------------------
110+
107111
#' @rdname plotly_data
108-
#' @export
109112
groups.plotly <- function(x) {
110-
dplyr::groups(plotly_data(x))
113+
groups(plotly_data(x))
111114
}
112115

113116
#' @rdname plotly_data
114-
#' @export
115117
ungroup.plotly <- function(x, ...) {
116-
d <- dplyr::ungroup(plotly_data(x))
118+
d <- ungroup(plotly_data(x), ...)
117119
add_data(x, d)
118120
}
119121

120122
#' @rdname plotly_data
121-
#' @export
122-
group_by_.plotly <- function(.data, ..., .dots, add = FALSE) {
123-
d <- plotly_data(.data)
124-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
125-
d2 <- group_by_add(d, !!!additional_args, add = add)
126-
# add crosstalk key as a group (to enable examples like demos/highlight-pipeline.R)
123+
group_by.plotly <- function(.data, ...) {
124+
d <- group_by(plotly_data(.data), ...)
127125
if (crosstalk_key() %in% names(d)) {
128-
d2 <- group_by_add(d2, !!rlang::sym(crosstalk_key()), add = TRUE)
126+
d <- group_by_add(d, !!rlang::sym(crosstalk_key()), add = TRUE)
129127
}
130-
add_data(.data, d2)
128+
add_data(.data, d)
131129
}
132130

133131
#' @rdname plotly_data
134-
#' @export
135-
summarise_.plotly <- function(.data, ..., .dots) {
136-
d <- plotly_data(.data)
137-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
138-
d <- dplyr::summarise(d, !!!additional_args)
132+
mutate.plotly <- function(.data, ...) {
133+
d <- mutate(plotly_data(.data), ...)
134+
add_data(.data, d)
135+
}
136+
137+
#' @rdname plotly_data
138+
do.plotly <- function(.data, ...) {
139+
d <- do(plotly_data(.data), ...)
140+
add_data(.data, d)
141+
}
142+
143+
#' @rdname plotly_data
144+
summarise.plotly <- function(.data, ...) {
145+
d <- summarise(plotly_data(.data), ...)
146+
add_data(.data, d)
147+
}
148+
149+
#' @rdname plotly_data
150+
arrange.plotly <- function(.data, ...) {
151+
d <- arrange(plotly_data(.data), ...)
152+
add_data(.data, d)
153+
}
154+
155+
#' @rdname plotly_data
156+
select.plotly <- function(.data, ...) {
157+
d <- select(plotly_data(.data), ...)
158+
add_data(.data, d)
159+
}
160+
161+
#' @rdname plotly_data
162+
filter.plotly <- function(.data, ...) {
163+
d <- filter(plotly_data(.data), ...)
164+
add_data(.data, d)
165+
}
166+
167+
#' @rdname plotly_data
168+
distinct.plotly <- function(.data, ...) {
169+
d <- distinct(plotly_data(.data), ...)
170+
add_data(.data, d)
171+
}
172+
173+
#' @rdname plotly_data
174+
slice.plotly <- function(.data, ...) {
175+
d <- slice(plotly_data(.data), ...)
176+
add_data(.data, d)
177+
}
178+
179+
#' @rdname plotly_data
180+
rename.plotly <- function(.data, ...) {
181+
d <- rename(plotly_data(.data), ...)
182+
add_data(.data, d)
183+
}
184+
185+
#' @rdname plotly_data
186+
transmute.plotly <- function(.data, ...) {
187+
d <- transmute(plotly_data(.data), ...)
188+
add_data(.data, d)
189+
}
190+
191+
# ------------------------------------------------------------
192+
# Deprecated dplyr non-nse generics
193+
# ------------------------------------------------------------
194+
195+
#' @rdname plotly_data
196+
group_by_.plotly <- function(.data, ..., .dots, add = FALSE) {
197+
args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
198+
d <- group_by_add(plotly_data(.data), !!!args, add = add)
199+
# add crosstalk key as a group (to enable examples like demos/highlight-pipeline.R)
200+
if (crosstalk_key() %in% names(d)) {
201+
d <- group_by_add(d, !!rlang::sym(crosstalk_key()), add = TRUE)
202+
}
139203
add_data(.data, d)
140204
}
141205

142206
#' @rdname plotly_data
143-
#' @export
144207
mutate_.plotly <- function(.data, ..., .dots) {
145208
d <- plotly_data(.data)
146-
dotz <- lazyeval::all_dots(.dots, ...)
147-
# '.' in a pipeline should really reference the data!!
148-
lapply(dotz, function(x) { assign(".", d, x$env) })
149-
set <- attr(d, "set")
150-
d <- dplyr::mutate(d, !!!dots_as_quosures(dotz))
151-
add_data(.data, structure(d, set = set))
209+
d <- dplyr::mutate_(d, .dots = lazyeval::all_dots(.dots, ...))
210+
add_data(.data, d)
152211
}
153212

154213
#' @rdname plotly_data
155-
#' @export
156214
do_.plotly <- function(.data, ..., .dots) {
157215
d <- plotly_data(.data)
158-
dotz <- lazyeval::all_dots(.dots, ...)
159-
# '.' in a pipeline should really reference the data!!
160-
lapply(dotz, function(x) { assign(".", d, x$env) })
161-
set <- attr(d, "set")
162-
d <- dplyr::do(d, !!!dots_as_quosures(dotz))
163-
add_data(.data, structure(d, set = set))
216+
d <- dplyr::do_(d, .dots = lazyeval::all_dots(.dots, ...))
217+
add_data(.data, d)
164218
}
165219

166220
#' @rdname plotly_data
167-
#' @export
168221
arrange_.plotly <- function(.data, ..., .dots) {
169-
d <- plotly_data(.data)
170-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
171-
d <- dplyr::arrange(d, !!!additional_args)
172-
add_data(.data, d)
222+
args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
223+
arrange(d, !!!args)
173224
}
174225

175226
#' @rdname plotly_data
176-
#' @export
177227
select_.plotly <- function(.data, ..., .dots) {
178-
d <- plotly_data(.data)
179-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
180-
d <- dplyr::select(d, !!!additional_args)
181-
add_data(.data, d)
228+
args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
229+
select(d, !!!args)
182230
}
183231

184232
#' @rdname plotly_data
185-
#' @export
186233
filter_.plotly <- function(.data, ..., .dots) {
187-
d <- plotly_data(.data)
188-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
189-
d <- dplyr::filter(d, !!!additional_args)
190-
add_data(.data, d)
234+
args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
235+
filter(d, !!!args)
191236
}
192237

193238
#' @rdname plotly_data
194-
#' @export
195239
distinct_.plotly <- function(.data, ..., .dots) {
196-
d <- plotly_data(.data)
197-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
198-
d <- dplyr::distinct(d, .dots = !!!additional_args)
199-
add_data(.data, d)
240+
args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
241+
distinct(d, !!!args)
200242
}
201243

202244
#' @rdname plotly_data
203-
#' @export
204245
slice_.plotly <- function(.data, ..., .dots) {
205-
d <- plotly_data(.data)
206-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
207-
d <- dplyr::slice(d, !!!additional_args)
208-
add_data(.data, d)
246+
args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
247+
slice(d, !!!args)
209248
}
210249

211250
#' @rdname plotly_data
212-
#' @export
213251
rename_.plotly <- function(.data, ..., .dots) {
214-
d <- plotly_data(.data)
215-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
216-
d <- dplyr::rename(d, !!!additional_args)
217-
add_data(.data, d)
252+
args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
253+
rename(d, !!!args)
218254
}
219255

220256
#' @rdname plotly_data
221-
#' @export
222257
transmute_.plotly <- function(.data, ..., .dots) {
223-
d <- plotly_data(.data)
224-
additional_args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
225-
d <- dplyr::transmute(d, !!!additional_arg)
226-
add_data(.data, d)
258+
args <- dots_as_quosures(lazyeval::all_dots(.dots, ...))
259+
transmute(.data, !!!args)
227260
}
228261

229262
# ---------------------------------------------------------------------------

man/api.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotly_IMAGE.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotly_data.Rd

Lines changed: 33 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reexports.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)