Skip to content

Commit d10b5a9

Browse files
committed
make plotly_build a generic function
1 parent eb9c080 commit d10b5a9

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ S3method(geom2trace,GeomTile)
1313
S3method(geom2trace,default)
1414
S3method(ggplotly,ggmatrix)
1515
S3method(ggplotly,ggplot)
16+
S3method(plotly_build,gg)
17+
S3method(plotly_build,plotly_built)
18+
S3method(plotly_build,plotly_hash)
1619
S3method(print,figure)
1720
S3method(print,plotly_built)
1821
S3method(print,plotly_hash)

R/ggplotly.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
448448
gglayout$annotations,
449449
make_label(
450450
faced(axisTitleText, axisTitle$face), x, y, el = axisTitle,
451-
xanchor = "center", yanchor = "middle"
451+
xanchor = "center", yanchor = "middle", annotationType = "axis"
452452
)
453453
)
454454
}

R/plotly.R

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,46 @@ style <- function(p = last_plot(), ..., traces = 1, evaluate = FALSE) {
221221
hash_plot(data, p)
222222
}
223223

224-
#' Build a plotly object before viewing it
224+
#' Create a 'plotly_built' object
225225
#'
226-
#' For convenience and efficiency purposes, plotly objects are subject to lazy
227-
#' evaluation. That is, the actual content behind a plotly object is not
228-
#' created until it is absolutely necessary. In some instances, you may want
229-
#' to perform this evaluation yourself, and work directly with the resulting
230-
#' list.
226+
#' This generic function creates the list object sent to plotly.js
227+
#' for rendering. Using this function can be useful for overriding defaults
228+
#' provided by \code{ggplotly}/\code{plot_ly} or for debugging rendering
229+
#' errors.
231230
#'
232-
#' @param l a ggplot object, or a plotly object, or a list.
231+
#' @param l a ggplot object, or a plotly_hash object, or a list.
233232
#' @export
233+
#' @examples
234+
#'
235+
#' p <- plot_ly()
236+
#' # data frame
237+
#' str(p)
238+
#' # the actual list of options sent to plotly.js
239+
#' str(plotly_build(p))
240+
#'
241+
#' p <- qplot(data = mtcars, wt, mpg, geom = c("point", "smooth"))
242+
#' l <- plotly_build(p)
243+
#' # turn off hoverinfo for the smooth (but keep it for the points)
244+
#' l$data[[2]]$hoverinfo <- "none"
245+
#' l$data[[3]]$hoverinfo <- "none"
246+
#' l
247+
#'
234248
plotly_build <- function(l = last_plot()) {
235-
# ggplot objects (including ggmatrix) don't need any special type of handling
236-
if (inherits(l, "gg")) {
237-
return(structure(get_plot(ggplotly(l)), class = "plotly"))
238-
}
249+
UseMethod("plotly_build")
250+
}
251+
252+
#' @export
253+
plotly_build.plotly_built <- function(l = last_plot()) {
254+
l
255+
}
256+
257+
#' @export
258+
plotly_build.gg <- function(l = last_plot()) {
259+
structure(get_plot(ggplotly(l)), class = "plotly_built")
260+
}
261+
262+
#' @export
263+
plotly_build.plotly_hash <- function(l = last_plot()) {
239264
l <- get_plot(l)
240265
# assume unnamed list elements are data/traces
241266
nms <- names(l)

man/plotly_build.Rd

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

0 commit comments

Comments
 (0)