|
| 1 | + |
| 2 | +#' Call a proxy method |
| 3 | +#' |
| 4 | +#' @param proxy A \code{proxy} \code{htmlwidget} object. |
| 5 | +#' @param name Proxy method. |
| 6 | +#' @param ... Arguments passed to method. |
| 7 | +#' |
| 8 | +#' @return A \code{htmlwidgetProxy} \code{htmlwidget} object. |
| 9 | +#' @noRd |
| 10 | +.call_proxy <- function(proxy, name, ...) { |
| 11 | + if (!"htmlwidgetProxy" %in% class(proxy)) |
| 12 | + stop("This function must be used with a htmlwidgetProxy object", call. = FALSE) |
| 13 | + proxy$session$sendCustomMessage( |
| 14 | + type = sprintf("uplot-%s", name), |
| 15 | + message = drop_nulls(list(id = proxy$id, ...)) |
| 16 | + ) |
| 17 | + proxy |
| 18 | +} |
| 19 | + |
| 20 | +#' Proxy for uPlot |
| 21 | +#' |
| 22 | +#' @param shinyId single-element character vector indicating the output ID of the |
| 23 | +#' chart to modify (if invoked from a Shiny module, the namespace will be added |
| 24 | +#' automatically). |
| 25 | +#' @param session the Shiny session object to which the chart belongs; usually the |
| 26 | +#' default value will suffice. |
| 27 | +#' |
| 28 | +#' @return A `uProxy` object. |
| 29 | +#' |
| 30 | +#' |
| 31 | +#' @export |
| 32 | +#' |
| 33 | +#' @importFrom shiny getDefaultReactiveDomain |
| 34 | +#' |
| 35 | +#' @examples |
| 36 | +#' \dontrun{ |
| 37 | +#' |
| 38 | +#' # Consider having created a editor widget with |
| 39 | +#' uPlotOutput("mychart") # UI |
| 40 | +#' output$mychart <- renderUPlot({}) # Server |
| 41 | +#' |
| 42 | +#' # Then you can call proxy methods in observer: |
| 43 | +#' |
| 44 | +#' |
| 45 | +#' } |
| 46 | +uProxy <- function(shinyId, session = shiny::getDefaultReactiveDomain()) { |
| 47 | + if (is.null(session)) { |
| 48 | + stop("uProxy must be called from the server function of a Shiny app") |
| 49 | + } |
| 50 | + if (!is.null(session$ns) && nzchar(session$ns(NULL)) && substring(shinyId, 1, nchar(session$ns(""))) != session$ns("")) { |
| 51 | + shinyId <- session$ns(shinyId) |
| 52 | + } |
| 53 | + structure( |
| 54 | + list( |
| 55 | + session = session, |
| 56 | + id = shinyId, |
| 57 | + x = list() |
| 58 | + ), |
| 59 | + class = c("uProxy", "htmlwidgetProxy") |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +is_proxy <- function(uplot) { |
| 65 | + inherits(uplot, "uProxy") |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +#' Redraw a chart |
| 70 | +#' |
| 71 | +#' @param uplot Proxy for a chart created with [uProxy()]. |
| 72 | +#' @param rebuildPaths If `FALSE` use cached series. |
| 73 | +#' @param recalcAxes Recalculate axes. |
| 74 | +#' |
| 75 | +#' @return An `htmlwidget` object of class `"uPlot"` or a `uProxy` object. |
| 76 | +#' @export |
| 77 | +#' |
| 78 | +#' |
| 79 | +#' @example examples/shiny-proxy-series-deladd.R |
| 80 | +uRedraw <- function(uplot, rebuildPaths = TRUE, recalcAxes = TRUE) { |
| 81 | + if (is_proxy(uplot)) { |
| 82 | + proxy <- .call_proxy( |
| 83 | + proxy = uplot, |
| 84 | + name = "redraw", |
| 85 | + rebuildPaths = rebuildPaths, |
| 86 | + recalcAxes = recalcAxes |
| 87 | + ) |
| 88 | + return(proxy) |
| 89 | + } |
| 90 | + check_uplot(uplot) |
| 91 | + warning("uRedraw: no method for uPlot object, can only be used with uProxy()") |
| 92 | + return(uplot) |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +#' Set data to a chart |
| 97 | +#' |
| 98 | +#' @param uplot Chart created with [uPlot()] or [uProxy()]. |
| 99 | +#' @param data The `data.frame` to use in chart. |
| 100 | +#' |
| 101 | +#' @return An `htmlwidget` object of class `"uPlot"` or a `uProxy` object. |
| 102 | +#' @export |
| 103 | +#' |
| 104 | +#' @examples |
| 105 | +#' uPlot() %>% |
| 106 | +#' uSetData(temperatures) |
| 107 | +#' |
| 108 | +#' @example examples/shiny-proxy-setdata.R |
| 109 | +uSetData <- function(uplot, data) { |
| 110 | + if (is_proxy(uplot)) { |
| 111 | + proxy <- .call_proxy( |
| 112 | + proxy = uplot, |
| 113 | + name = "setData", |
| 114 | + data = prepare_data(data) |
| 115 | + ) |
| 116 | + return(proxy) |
| 117 | + } |
| 118 | + check_uplot(uplot) |
| 119 | + data <- prepare_data(data) |
| 120 | + uplot$x$config$data <- data |
| 121 | + series_nms <- attr(data, ".nms") |
| 122 | + uplot$xseries_nms <- series_nms |
| 123 | + if (length(uplot$x$config$options$series) < 1) { |
| 124 | + strokes <- rep_len(palette(), length(series_nms)) |
| 125 | + uplot$x$config$options$series <- prepare_options_series( |
| 126 | + label = series_nms, |
| 127 | + stroke = strokes |
| 128 | + ) |
| 129 | + } |
| 130 | + return(uplot) |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +#' Update series to a chart via proxy |
| 135 | +#' |
| 136 | +#' @param uplot Chart created with [uPlot()] or [uProxy()]. |
| 137 | +#' @param seriesIdx Index of the serie to set. |
| 138 | +#' @param show,focus Toggles series visibility or focus. |
| 139 | +#' @param ... Options for the series, see [uSeries()]. |
| 140 | +#' |
| 141 | +#' @return An `htmlwidget` object of class `"uPlot"` or a `uProxy` object. |
| 142 | +#' @export |
| 143 | +#' |
| 144 | +#' @name proxy-series |
| 145 | +#' |
| 146 | +#' @examples |
| 147 | +#' uPlot() %>% |
| 148 | +#' uSetData(temperatures) |
| 149 | +#' |
| 150 | +#' @example examples/shiny-proxy-series-visibility.R |
| 151 | +#' @example examples/shiny-proxy-series-deladd.R |
| 152 | +uSetSeries <- function(uplot, seriesIdx, show = TRUE, focus = NULL) { |
| 153 | + if (is_proxy(uplot)) { |
| 154 | + proxy <- .call_proxy( |
| 155 | + proxy = uplot, |
| 156 | + name = "setSeries", |
| 157 | + seriesIdx = seriesIdx - 1, |
| 158 | + options = drop_nulls(list(show = show, focus = focus)) |
| 159 | + ) |
| 160 | + return(proxy) |
| 161 | + } |
| 162 | + check_uplot(uplot) |
| 163 | + warning("uSetSeries: no method for uPlot object, can only be used with uProxy()") |
| 164 | + return(uplot) |
| 165 | +} |
| 166 | + |
| 167 | +#' @export |
| 168 | +#' |
| 169 | +#' @rdname proxy-series |
| 170 | +uAddSeries <- function(uplot, seriesIdx, ...) { |
| 171 | + if (is_proxy(uplot)) { |
| 172 | + proxy <- .call_proxy( |
| 173 | + proxy = uplot, |
| 174 | + name = "addSeries", |
| 175 | + seriesIdx = seriesIdx - 1, |
| 176 | + options = list(...) |
| 177 | + ) |
| 178 | + return(proxy) |
| 179 | + } |
| 180 | + check_uplot(uplot) |
| 181 | + uplot <- uSeries(uSeries, serie = seriesIdx, ...) |
| 182 | + return(uplot) |
| 183 | +} |
| 184 | + |
| 185 | +#' @export |
| 186 | +#' |
| 187 | +#' @rdname proxy-series |
| 188 | +uDelSeries <- function(uplot, seriesIdx) { |
| 189 | + if (is_proxy(uplot)) { |
| 190 | + proxy <- .call_proxy( |
| 191 | + proxy = uplot, |
| 192 | + name = "delSeries", |
| 193 | + seriesIdx = seriesIdx - 1 |
| 194 | + ) |
| 195 | + return(proxy) |
| 196 | + } |
| 197 | + check_uplot(uplot) |
| 198 | + warning("uDelSeries: no method for uPlot object, can only be used with uProxy()") |
| 199 | + return(uplot) |
| 200 | +} |
0 commit comments