Skip to content

Commit 078736c

Browse files
committed
make sure plotly_data returns sf class and start on sensible trace defaults
1 parent 2b5a82d commit 078736c

File tree

4 files changed

+35
-42
lines changed

4 files changed

+35
-42
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ S3method(do_,plotly)
99
S3method(embed_notebook,plotly)
1010
S3method(filter_,plotly)
1111
S3method(fortify,SharedData)
12+
S3method(fortify,plotly)
1213
S3method(geom2trace,GeomBar)
1314
S3method(geom2trace,GeomBlank)
1415
S3method(geom2trace,GeomBoxplot)
@@ -20,7 +21,6 @@ S3method(geom2trace,GeomPolygon)
2021
S3method(geom2trace,GeomText)
2122
S3method(geom2trace,GeomTile)
2223
S3method(geom2trace,default)
23-
S3method(ggplot,plotly)
2424
S3method(ggplotly,ggmatrix)
2525
S3method(ggplotly,ggplot)
2626
S3method(ggplotly,plotly)

R/plotly_build.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
114114

115115
# perform the evaluation
116116
dat <- plotly_data(p, y)
117+
118+
# set special defaults for sf
119+
if (inherits(dat, "sf")) {
120+
dat <- fortify_sf(dat)
121+
x$x <- ~x
122+
x$y <- ~y
123+
}
124+
117125
trace <- structure(
118126
rapply(x, eval_attr, data = dat, how = "list"),
119127
class = oldClass(x)
@@ -527,6 +535,7 @@ train_data <- function(data, trace) {
527535
dat[idx2, "y"] <- data[["yend"]]
528536
data <- dplyr::group_by_(dat, ".plotlyGroupIndex", add = TRUE)
529537
}
538+
530539
# TODO: a lot more geoms!!!
531540
data
532541
}

R/plotly_data.R

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,33 @@
6262
#'
6363
plotly_data <- function(p, id = p$x$cur_data) {
6464
if (!is.plotly(p)) {
65-
stop("This function can only retrieve data from plotly objects.")
65+
stop("`plotly_data()` expects a plotly object as it's first argument.", call. = FALSE)
6666
}
67+
# TODO: attach a special class here so that it gets printed as a tibble?
68+
# Or, maybe, add an `as_tibble = TRUE` argument and use FALSE internally
69+
fortify(p)
70+
}
71+
72+
#' @export
73+
fortify.plotly <- function(p, id = p$x$cur_data) {
6774
f <- p$x$visdat[[id]]
6875
# if data has been specified, f should be a closure that, when called,
6976
# returns data
70-
if (is.null(f)) return(f)
71-
if (!is.function(f)) stop("Expected a closure", call. = FALSE)
72-
dat <- f()
73-
if (crosstalk::is.SharedData(dat)) {
74-
key <- dat$key()
75-
set <- dat$groupName()
76-
dat <- dat$origData()
77-
dat[[crosstalk_key()]] <- key
78-
# not allowed for list-columns!
79-
#dat <- dplyr::group_by_(dat, crosstalk_key(), add = TRUE)
80-
dat <- structure(dat, set = set)
81-
}
82-
if (is.data.frame(dat)) tibble::as_tibble(dat) else dat
77+
if (!is.function(f)) stop("Expected a closure function", call. = FALSE)
78+
fortify(f())
8379
}
8480

81+
#' @export
82+
fortify.SharedData <- function(model, data, ...) {
83+
key <- model$key()
84+
set <- model$groupName()
85+
data <- model$origData()
86+
# need a consistent name so we know how to access it ggplotly()
87+
data[[crosstalk_key()]] <- key
88+
structure(data, set = set)
89+
}
90+
91+
8592
#' @rdname plotly_data
8693
#' @export
8794
groups.plotly <- function(x) {
@@ -226,28 +233,3 @@ transmute_.plotly <- function(.data, ..., .dots) {
226233
# }
227234
#
228235
# n_dots <- function(...) nargs()
229-
230-
231-
# ---------------------------------------------------------------------------
232-
# miscellanous methods
233-
# ---------------------------------------------------------------------------
234-
235-
# Avoid errors when passing a shared data to ggplot2
236-
# qplot(data = crosstalk::SharedData$new(mtcars), mpg, wt)
237-
238-
#' @export
239-
fortify.SharedData <- function(model, data, ...) {
240-
key <- model$key()
241-
set <- model$groupName()
242-
data <- model$origData()
243-
# need a consistent name so we know how to access it ggplotly()
244-
data[[crosstalk_key()]] <- key
245-
structure(data, set = set)
246-
}
247-
248-
# yes, you can feed a plotly object into ggplot %^)
249-
#' @export
250-
ggplot.plotly <- function(data, mapping = aes(), ...,
251-
environment = parent.frame()) {
252-
ggplot(plotly_data(data), mapping = mapping, ..., environment = environment)
253-
}

demo/00Index

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
animation-tour-basic A basic example of a grand tour
2+
animation-tour-USArrests Linking a dendrogram to a tour
13
crosstalk-highlight-intro An introduction to linked views
24
crosstalk-highlight-subplot Basic scatterplot brushing with subplot()
35
crosstalk-highlight-ggplotly Linking ggplot2 plots
@@ -8,7 +10,7 @@ crosstalk-highlight-binned-target Let plotly.js dynamically bin the raw data
810
crosstalk-highlight-epl An example of linked facets
911
crosstalk-filter-lines Using crosstalk's filter_select() to filter lines
1012
crosstalk-filter-dynamic-axis Using crosstalk's filter_select() to dynamically change the y-axis
11-
animation-tour-basic A basic example of a grand tour
12-
animation-tour-USArrests Linking a dendrogram to a tour
1313
rotate Using htmlwidgets::onRender() to rotate the camera of a 3D graph
1414
ternary A basic ternary plot
15+
sf-data-scattermapbox Mapping sf objects with mapbox
16+
sf-layout-scattermapbox Mapping sf objects with mapbox (as a layer)

0 commit comments

Comments
 (0)