|
| 1 | +#' Modify a plotly object inside a shiny app |
| 2 | +#' |
| 3 | +#' @param outputId single-element character vector indicating the output ID |
| 4 | +#' map to modify (if invoked from a Shiny module, the namespace will be added |
| 5 | +#' automatically) |
| 6 | +#' @param session the Shiny session object to which the map belongs; usually the |
| 7 | +#' default value will suffice. |
| 8 | +#' @param deferUntilFlush indicates whether actions performed against this |
| 9 | +#' instance should be carried out right away, or whether they should be held |
| 10 | +#' until after the next time all of the outputs are updated. |
| 11 | +#' @rdname plotlyProxy |
| 12 | +#' @export |
| 13 | +#' @examples |
| 14 | +#' |
| 15 | +#' demo("proxy-mapbox", package = "plotly") |
| 16 | +#' demo("proxy-relayout", package = "plotly") |
| 17 | +plotlyProxy <- function(outputId, session = shiny::getDefaultReactiveDomain(), |
| 18 | + deferUntilFlush = TRUE) { |
| 19 | + |
| 20 | + # implementation very similar to leaflet::leafletProxy & DT:dataTableProxy |
| 21 | + if (is.null(session)) { |
| 22 | + stop("plotlyProxy must be called from the server function of a Shiny app") |
| 23 | + } |
| 24 | + |
| 25 | + if (!is.null(session$ns) && nzchar(session$ns(NULL)) && |
| 26 | + # TODO: require a recent version of R and use startsWith()? |
| 27 | + substring(outputId, 1, nchar(session$ns(""))) != session$ns("")) { |
| 28 | + outputId <- session$ns(outputId) |
| 29 | + } |
| 30 | + structure( |
| 31 | + list( |
| 32 | + session = session, |
| 33 | + id = outputId, |
| 34 | + deferUntilFlush = deferUntilFlush |
| 35 | + # TODO: is there actually a use-case for this? |
| 36 | + #x = structure(list(), leafletData = data), |
| 37 | + #dependencies = NULL |
| 38 | + ), |
| 39 | + class = "plotly_proxy" |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +# ---------------------------------------------------------------------- |
| 45 | +# TODO: implement some higher-level functions, say `plotlyProxyLayout()`, |
| 46 | +# `plotlyProxyAddTraces()`, `plotlyProxyStyle()`, that pick the right |
| 47 | +# method, except formula/data mappings, and possibly some argument checking |
| 48 | +# ---------------------------------------------------------------------- |
| 49 | + |
| 50 | + |
| 51 | +#' @param p a plotly proxy object (created with \code{plotlyProxy}) |
| 52 | +#' @param method a plotlyjs method to invoke. For a list of options, |
| 53 | +#' visit the \href{https://plot.ly/javascript/plotlyjs-function-reference}{plotlyjs function reference} |
| 54 | +#' @param ... unnamed arguments passed onto the plotly.js method |
| 55 | +#' @rdname plotlyProxy |
| 56 | +#' @export |
| 57 | +plotlyProxyInvoke <- function(p, method, ...) { |
| 58 | + |
| 59 | + if (!is.proxy(p)) |
| 60 | + stop("p must be a proxy object. See `help(plotlyProxy)`", call. = FALSE) |
| 61 | + |
| 62 | + if (missing(method)) |
| 63 | + stop( |
| 64 | + "Must provide a plotly.js method (as a character string of length 1).\n", |
| 65 | + sprintf("Valid options include: '%s'", |
| 66 | + paste(plotlyjs_methods(), collapse = "', '")), |
| 67 | + call. = FALSE |
| 68 | + ) |
| 69 | + |
| 70 | + method <- match.arg(method, plotlyjs_methods()) |
| 71 | + |
| 72 | + msg <- list( |
| 73 | + id = p$id, |
| 74 | + method = method, |
| 75 | + # TODO: can we leverage the plotly_build() infrastructure in a smart way? |
| 76 | + # args = evalFormula(list(...), data) |
| 77 | + args = list(...) |
| 78 | + ) |
| 79 | + |
| 80 | + if (isTRUE(p$deferUntilFlushed)) { |
| 81 | + |
| 82 | + p$session$onFlushed(function() { |
| 83 | + p$session$sendCustomMessage("plotly-calls", msg) |
| 84 | + }, once = TRUE) |
| 85 | + |
| 86 | + } else { |
| 87 | + |
| 88 | + p$session$sendCustomMessage("plotly-calls", msg) |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + p |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +plotlyjs_methods <- function() { |
| 97 | + c( |
| 98 | + "restyle", "relayout", "update", "addTraces", "deleteTraces", "moveTraces", |
| 99 | + "extendTraces", "prependTraces", "purge", "toImage", "downloadImage" |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +is.proxy <- function(x) { |
| 105 | + inherits(x, "plotly_proxy") |
| 106 | +} |
0 commit comments