|
62 | 62 | #'
|
63 | 63 | plotly_data <- function(p, id = p$x$cur_data) {
|
64 | 64 | 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) |
66 | 66 | }
|
| 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) { |
67 | 74 | f <- p$x$visdat[[id]]
|
68 | 75 | # if data has been specified, f should be a closure that, when called,
|
69 | 76 | # 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()) |
83 | 79 | }
|
84 | 80 |
|
| 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 | + |
85 | 92 | #' @rdname plotly_data
|
86 | 93 | #' @export
|
87 | 94 | groups.plotly <- function(x) {
|
@@ -226,28 +233,3 @@ transmute_.plotly <- function(.data, ..., .dots) {
|
226 | 233 | # }
|
227 | 234 | #
|
228 | 235 | # 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 |
| -} |
0 commit comments