| 1 |
#' @rdname e_bar |
|
| 2 |
#' @export |
|
| 3 |
e_bar_ <- function( |
|
| 4 |
e, |
|
| 5 |
serie, |
|
| 6 |
bind = NULL, |
|
| 7 |
name = NULL, |
|
| 8 |
legend = TRUE, |
|
| 9 |
y_index = 0, |
|
| 10 |
x_index = 0, |
|
| 11 |
coord_system = "cartesian2d", |
|
| 12 |
... |
|
| 13 |
) {
|
|
| 14 | 43x |
if (missing(e)) {
|
| 15 | 1x |
stop("must pass e", call. = FALSE)
|
| 16 |
} |
|
| 17 | ||
| 18 | 42x |
if (missing(serie)) {
|
| 19 | 1x |
stop("must pass serie", call. = FALSE)
|
| 20 |
} |
|
| 21 | ||
| 22 | 41x |
for (i in seq_along(e$x$data)) {
|
| 23 | 61x |
vector <- .build_data2(e$x$data[[i]], e$x$mapping$x, serie) |
| 24 | ||
| 25 | 61x |
if (!is.null(bind)) {
|
| 26 | ! |
vector <- .add_bind2(e, vector, bind, i = i) |
| 27 |
} |
|
| 28 | ||
| 29 | 61x |
e_serie <- list(data = vector) |
| 30 | ||
| 31 | 61x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 32 | 4x |
grp_val <- names(e$x$data)[i] |
| 33 | 4x |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 34 | 4x |
e_serie$datasetId <- id |
| 35 | 4x |
e_serie$data <- NULL |
| 36 | 4x |
e_serie$encode <- list(x = e$x$mapping$x, y = serie) |
| 37 |
} |
|
| 38 | ||
| 39 | 61x |
if (y_index != 0) {
|
| 40 | 1x |
e <- .set_y_axis(e, serie, y_index, i) |
| 41 |
} |
|
| 42 | ||
| 43 | 61x |
if (x_index != 0) {
|
| 44 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 45 |
} |
|
| 46 | ||
| 47 | 61x |
if (coord_system == "polar") {
|
| 48 | 8x |
e_serie$data <- e$x$data[[i]] |> |
| 49 | 8x |
dplyr::select(dplyr::all_of(serie)) |> |
| 50 | 8x |
unlist() |> |
| 51 | 8x |
unname() |> |
| 52 | 8x |
as.list() |
| 53 |
} |
|
| 54 | ||
| 55 |
# timeline |
|
| 56 | 61x |
if (!e$x$tl) {
|
| 57 | 35x |
nm <- .name_it(e, serie, name, i) |
| 58 | ||
| 59 | 35x |
opts <- list( |
| 60 | 35x |
name = nm, |
| 61 | 35x |
type = "bar", |
| 62 | 35x |
yAxisIndex = y_index, |
| 63 | 35x |
xAxisIndex = x_index, |
| 64 | 35x |
coordinateSystem = coord_system, |
| 65 |
... |
|
| 66 |
) |
|
| 67 | ||
| 68 | 35x |
e_serie <- append(e_serie, opts) |
| 69 | ||
| 70 | 35x |
if (isTRUE(legend)) {
|
| 71 | 35x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 72 |
} |
|
| 73 | ||
| 74 | 35x |
e$x$opts$series <- append(e$x$opts$series, list(e_serie)) |
| 75 |
} else {
|
|
| 76 | 26x |
if (isTRUE(legend)) {
|
| 77 | 26x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(name)) |
| 78 |
} |
|
| 79 | 26x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(e_serie)) |
| 80 |
} |
|
| 81 |
} |
|
| 82 | ||
| 83 | 41x |
if (isTRUE(e$x$tl)) {
|
| 84 | 8x |
if (is.null(name)) name <- serie |
| 85 | ||
| 86 | 8x |
series_opts <- list( |
| 87 | 8x |
name = name, |
| 88 | 8x |
type = "bar", |
| 89 | 8x |
yAxisIndex = y_index, |
| 90 | 8x |
xAxisIndex = x_index, |
| 91 | 8x |
coordinateSystem = coord_system, |
| 92 |
... |
|
| 93 |
) |
|
| 94 | ||
| 95 | 8x |
if (isTRUE(legend)) {
|
| 96 | 8x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 97 |
} |
|
| 98 | ||
| 99 | 8x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(series_opts)) |
| 100 |
} |
|
| 101 | ||
| 102 | 41x |
e |
| 103 |
} |
|
| 104 | ||
| 105 |
#' @rdname e_line |
|
| 106 |
#' @export |
|
| 107 |
e_line_ <- function( |
|
| 108 |
e, |
|
| 109 |
serie, |
|
| 110 |
bind = NULL, |
|
| 111 |
name = NULL, |
|
| 112 |
legend = TRUE, |
|
| 113 |
y_index = 0, |
|
| 114 |
x_index = 0, |
|
| 115 |
coord_system = "cartesian2d", |
|
| 116 |
... |
|
| 117 |
) {
|
|
| 118 | 87x |
if (missing(e)) {
|
| 119 | 1x |
stop("must pass e", call. = FALSE)
|
| 120 |
} |
|
| 121 | ||
| 122 | 86x |
if (missing(serie)) {
|
| 123 | 1x |
stop("must pass serie", call. = FALSE)
|
| 124 |
} |
|
| 125 | ||
| 126 | 85x |
if (length(e$x$facets)) {
|
| 127 | ! |
x_index <- e$x$facets$current |
| 128 | ! |
y_index <- e$x$facets$current |
| 129 | ! |
e$x$facets$current <- e$x$facets$current + 1 |
| 130 |
} |
|
| 131 | ||
| 132 | 85x |
for (i in seq_along(e$x$data)) {
|
| 133 |
# build JSON data |
|
| 134 | 134x |
vector <- .build_data2(e$x$data[[i]], e$x$mapping$x, serie) |
| 135 | ||
| 136 | 134x |
if (!is.null(bind)) {
|
| 137 | ! |
vector <- .add_bind2(e, vector, bind, i = i) |
| 138 |
} |
|
| 139 | ||
| 140 | 134x |
l <- list( |
| 141 | 134x |
data = vector |
| 142 |
) |
|
| 143 | ||
| 144 | 134x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 145 | ! |
grp_val <- names(e$x$data)[i] |
| 146 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 147 | ! |
l$datasetId <- id |
| 148 | ! |
l$data <- NULL |
| 149 | ! |
l$encode <- list(x = e$x$mapping$x, y = serie) |
| 150 |
} |
|
| 151 | ||
| 152 | 134x |
if (coord_system == "cartesian2d") {
|
| 153 | 132x |
if (y_index != 0) {
|
| 154 | 1x |
e <- .set_y_axis(e, serie, y_index, i) |
| 155 |
} |
|
| 156 | ||
| 157 | 132x |
if (x_index != 0) {
|
| 158 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 159 |
} |
|
| 160 | ||
| 161 | 132x |
if (!e$x$tl) {
|
| 162 | 103x |
l$yAxisIndex <- y_index |
| 163 | 103x |
l$xAxisIndex <- x_index |
| 164 |
} |
|
| 165 | 2x |
} else if (coord_system == "polar") {
|
| 166 | 2x |
l$data <- e$x$data[[i]] |> |
| 167 | 2x |
dplyr::select(dplyr::all_of(serie)) |> |
| 168 | 2x |
unlist() |> |
| 169 | 2x |
unname() |> |
| 170 | 2x |
as.list() |
| 171 |
} |
|
| 172 | ||
| 173 | 134x |
if (!e$x$tl) {
|
| 174 | 105x |
nm <- .name_it(e, serie, name, i) |
| 175 | ||
| 176 | 105x |
opts <- list( |
| 177 | 105x |
name = nm, |
| 178 | 105x |
type = "line", |
| 179 | 105x |
coordinateSystem = coord_system, |
| 180 |
... |
|
| 181 |
) |
|
| 182 | ||
| 183 | 105x |
l <- append(l, opts) |
| 184 | ||
| 185 | 105x |
if (isTRUE(legend)) {
|
| 186 | 99x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 187 |
} |
|
| 188 | ||
| 189 | 105x |
e$x$opts$series <- append(e$x$opts$series, list(l)) |
| 190 |
} else {
|
|
| 191 | 29x |
if (isTRUE(legend)) {
|
| 192 | 29x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(name)) |
| 193 |
} |
|
| 194 | ||
| 195 | 29x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(l)) |
| 196 |
} |
|
| 197 |
} |
|
| 198 | ||
| 199 | 85x |
if (isTRUE(e$x$tl)) {
|
| 200 | 11x |
if (is.null(name)) name <- serie |
| 201 | ||
| 202 | 11x |
series_opts <- list( |
| 203 | 11x |
name = name, |
| 204 | 11x |
type = "line", |
| 205 | 11x |
selectedMode = "single", |
| 206 | 11x |
yAxisIndex = y_index, |
| 207 | 11x |
xAxisIndex = x_index, |
| 208 | 11x |
coordinateSystem = coord_system, |
| 209 |
... |
|
| 210 |
) |
|
| 211 | ||
| 212 | 11x |
if (isTRUE(legend)) {
|
| 213 | 11x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 214 |
} |
|
| 215 | ||
| 216 | 11x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(series_opts)) |
| 217 |
} |
|
| 218 | ||
| 219 | 85x |
e |
| 220 |
} |
|
| 221 | ||
| 222 |
#' @rdname e_area |
|
| 223 |
#' @export |
|
| 224 |
e_area_ <- function( |
|
| 225 |
e, |
|
| 226 |
serie, |
|
| 227 |
bind = NULL, |
|
| 228 |
name = NULL, |
|
| 229 |
legend = TRUE, |
|
| 230 |
y_index = 0, |
|
| 231 |
x_index = 0, |
|
| 232 |
coord_system = "cartesian2d", |
|
| 233 |
... |
|
| 234 |
) {
|
|
| 235 | 11x |
if (missing(e)) {
|
| 236 | 1x |
stop("must pass e", call. = FALSE)
|
| 237 |
} |
|
| 238 | ||
| 239 | 10x |
if (missing(serie)) {
|
| 240 | 1x |
stop("must pass serie", call. = FALSE)
|
| 241 |
} |
|
| 242 | ||
| 243 | 9x |
for (i in seq_along(e$x$data)) {
|
| 244 | ||
| 245 |
# build JSON data |
|
| 246 | 11x |
vector <- .build_data2(e$x$data[[i]], e$x$mapping$x, serie) |
| 247 | ||
| 248 | 11x |
if (!is.null(bind)) {
|
| 249 | ! |
vector <- .add_bind2(e, vector, bind, i = i) |
| 250 |
} |
|
| 251 | ||
| 252 | 11x |
l <- list( |
| 253 | 11x |
data = vector |
| 254 |
) |
|
| 255 | ||
| 256 | 11x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 257 | ! |
grp_val <- names(e$x$data)[i] |
| 258 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 259 | ! |
l$datasetId <- id |
| 260 | ! |
l$data <- NULL |
| 261 | ! |
l$encode <- list(x = e$x$mapping$x, y = serie) |
| 262 |
} |
|
| 263 | ||
| 264 | 11x |
if (coord_system == "cartesian2d") {
|
| 265 | 10x |
if (y_index != 0) {
|
| 266 | 1x |
e <- .set_y_axis(e, serie, y_index, i) |
| 267 |
} |
|
| 268 | ||
| 269 | 10x |
if (x_index != 0) {
|
| 270 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 271 |
} |
|
| 272 | ||
| 273 | 10x |
l$yAxisIndex <- y_index |
| 274 | 10x |
l$xAxisIndex <- x_index |
| 275 | 1x |
} else if (coord_system == "polar") {
|
| 276 | 1x |
l$data <- e$x$data[[i]] |> |
| 277 | 1x |
dplyr::select(dplyr::all_of(serie)) |> |
| 278 | 1x |
unlist() |> |
| 279 | 1x |
unname() |> |
| 280 | 1x |
as.list() |
| 281 |
} |
|
| 282 | ||
| 283 | 11x |
if (!e$x$tl) {
|
| 284 | 8x |
nm <- .name_it(e, serie, name, i) |
| 285 | ||
| 286 | 8x |
opts <- list( |
| 287 | 8x |
name = nm, |
| 288 | 8x |
type = "line", |
| 289 | 8x |
coordinateSystem = coord_system, |
| 290 | 8x |
areaStyle = list(), |
| 291 |
... |
|
| 292 |
) |
|
| 293 | ||
| 294 | 8x |
l <- append(l, opts) |
| 295 | ||
| 296 | 8x |
if (isTRUE(legend)) {
|
| 297 | 8x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 298 |
} |
|
| 299 | ||
| 300 | 8x |
e$x$opts$series <- append(e$x$opts$series, list(l)) |
| 301 |
} else {
|
|
| 302 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(l)) |
| 303 |
} |
|
| 304 |
} |
|
| 305 | ||
| 306 | 9x |
if (isTRUE(e$x$tl)) {
|
| 307 | 1x |
if (is.null(name)) name <- serie |
| 308 | ||
| 309 | 1x |
series_opts <- list( |
| 310 | 1x |
name = name, |
| 311 | 1x |
type = "line", |
| 312 | 1x |
yAxisIndex = y_index, |
| 313 | 1x |
xAxisIndex = x_index, |
| 314 | 1x |
coordinateSystem = coord_system, |
| 315 | 1x |
areaStyle = list(), |
| 316 |
... |
|
| 317 |
) |
|
| 318 | ||
| 319 | 1x |
if (isTRUE(legend)) {
|
| 320 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 321 |
} |
|
| 322 | ||
| 323 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(series_opts)) |
| 324 |
} |
|
| 325 | ||
| 326 | 9x |
e |
| 327 |
} |
|
| 328 | ||
| 329 |
#' @rdname e_step |
|
| 330 |
#' @export |
|
| 331 |
e_step_ <- function( |
|
| 332 |
e, |
|
| 333 |
serie, |
|
| 334 |
bind = NULL, |
|
| 335 |
step = c("start", "middle", "end"),
|
|
| 336 |
fill = FALSE, |
|
| 337 |
name = NULL, |
|
| 338 |
legend = TRUE, |
|
| 339 |
y_index = 0, |
|
| 340 |
x_index = 0, |
|
| 341 |
coord_system = "cartesian2d", |
|
| 342 |
... |
|
| 343 |
) {
|
|
| 344 | 10x |
if (missing(e)) {
|
| 345 | 1x |
stop("must pass e", call. = FALSE)
|
| 346 |
} |
|
| 347 | ||
| 348 | 9x |
if (missing(serie)) {
|
| 349 | 1x |
stop("must pass serie", call. = FALSE)
|
| 350 |
} |
|
| 351 | ||
| 352 | 8x |
if (!step[1] %in% c("start", "middle", "end")) {
|
| 353 | 2x |
stop("wrong step", call. = FALSE)
|
| 354 |
} |
|
| 355 | ||
| 356 | 6x |
for (i in seq_along(e$x$data)) {
|
| 357 | ||
| 358 |
# build JSON data |
|
| 359 | 8x |
vector <- .build_data2(e$x$data[[i]], e$x$mapping$x, serie) |
| 360 | ||
| 361 | 8x |
if (!is.null(bind)) {
|
| 362 | ! |
vector <- .add_bind2(e, vector, bind, i = i) |
| 363 |
} |
|
| 364 | ||
| 365 | 8x |
l <- list( |
| 366 | 8x |
data = vector |
| 367 |
) |
|
| 368 | 8x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 369 | ! |
grp_val <- names(e$x$data)[i] |
| 370 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 371 | ! |
l$datasetId <- id |
| 372 | ! |
l$data <- NULL |
| 373 | ! |
l$encode <- list(x = e$x$mapping$x, y = serie) |
| 374 |
} |
|
| 375 | ||
| 376 | ||
| 377 | 8x |
if (coord_system == "cartesian2d") {
|
| 378 | 7x |
if (y_index != 0) {
|
| 379 | 1x |
e <- .set_y_axis(e, serie, y_index, i) |
| 380 |
} |
|
| 381 | ||
| 382 | 7x |
if (x_index != 0) {
|
| 383 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 384 |
} |
|
| 385 | ||
| 386 | 7x |
l$yAxisIndex <- y_index |
| 387 | 7x |
l$xAxisIndex <- x_index |
| 388 | 1x |
} else if (coord_system == "polar") {
|
| 389 | 1x |
l$data <- e$x$data[[i]] |> |
| 390 | 1x |
dplyr::select(dplyr::all_of(serie)) |> |
| 391 | 1x |
unlist() |> |
| 392 | 1x |
unname() |> |
| 393 | 1x |
as.list() |
| 394 |
} |
|
| 395 | ||
| 396 | 8x |
if (!e$x$tl) {
|
| 397 | 5x |
nm <- .name_it(e, serie, name, i) |
| 398 | ||
| 399 | 5x |
opts <- list( |
| 400 | 5x |
name = nm, |
| 401 | 5x |
type = "line", |
| 402 | 5x |
coordinateSystem = coord_system, |
| 403 | 5x |
step = step[1], |
| 404 |
... |
|
| 405 |
) |
|
| 406 | ||
| 407 | 5x |
l <- append(l, opts) |
| 408 | ||
| 409 | 5x |
if (isTRUE(legend)) {
|
| 410 | 5x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 411 |
} |
|
| 412 | ||
| 413 | 5x |
e$x$opts$series <- append(e$x$opts$series, list(l)) |
| 414 |
} else {
|
|
| 415 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(l)) |
| 416 |
} |
|
| 417 |
} |
|
| 418 | ||
| 419 | 6x |
if (isTRUE(e$x$tl)) {
|
| 420 | 1x |
if (is.null(name)) name <- serie |
| 421 | ||
| 422 | 1x |
series_opts <- list( |
| 423 | 1x |
name = name, |
| 424 | 1x |
type = "line", |
| 425 | 1x |
yAxisIndex = y_index, |
| 426 | 1x |
xAxisIndex = x_index, |
| 427 | 1x |
coordinateSystem = coord_system, |
| 428 | 1x |
areaStyle = list(), |
| 429 | 1x |
step = step[1], |
| 430 |
... |
|
| 431 |
) |
|
| 432 | ||
| 433 | 1x |
if (isTRUE(legend)) {
|
| 434 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 435 |
} |
|
| 436 | ||
| 437 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(series_opts)) |
| 438 |
} |
|
| 439 | ||
| 440 | 6x |
e |
| 441 |
} |
|
| 442 | ||
| 443 |
#' @rdname scatter |
|
| 444 |
#' @export |
|
| 445 |
e_scale <- function(x) {
|
|
| 446 | 12x |
if (!inherits(x, "numeric")) {
|
| 447 | 1x |
stop("x must be numeric")
|
| 448 |
} |
|
| 449 | ||
| 450 | 11x |
scales::rescale(x, to = c(1, 20)) |
| 451 |
} |
|
| 452 | ||
| 453 |
#' @rdname scatter |
|
| 454 |
#' @export |
|
| 455 |
e_scatter_ <- function( |
|
| 456 |
e, |
|
| 457 |
serie, |
|
| 458 |
size = NULL, |
|
| 459 |
bind = NULL, |
|
| 460 |
symbol = NULL, |
|
| 461 |
symbol_size = 1, |
|
| 462 |
scale = e_scale, |
|
| 463 |
scale_js = "function(data){ return data[3];}",
|
|
| 464 |
name = NULL, |
|
| 465 |
coord_system = "cartesian2d", |
|
| 466 |
jitter_factor = 0, |
|
| 467 |
jitter_amount = NULL, |
|
| 468 |
legend = TRUE, |
|
| 469 |
y_index = 0, |
|
| 470 |
x_index = 0, |
|
| 471 |
rm_x = TRUE, |
|
| 472 |
rm_y = TRUE, |
|
| 473 |
... |
|
| 474 |
) {
|
|
| 475 | 132x |
if (missing(e)) {
|
| 476 | 1x |
stop("must pass e", call. = FALSE)
|
| 477 |
} |
|
| 478 | ||
| 479 | 131x |
if (missing(serie)) {
|
| 480 | 1x |
stop("must pass serie", call. = FALSE)
|
| 481 |
} |
|
| 482 | ||
| 483 | 130x |
if(coord_system == "matrix"){
|
| 484 | ! |
e <- e_scatter_matrix(e, z = serie, ...) |
| 485 | ! |
return(e) |
| 486 |
} |
|
| 487 | ||
| 488 | 130x |
for (i in seq_along(e$x$data)) {
|
| 489 | 176x |
if (y_index != 0) {
|
| 490 | 1x |
e <- .set_y_axis(e, serie, y_index, i) |
| 491 |
} |
|
| 492 | ||
| 493 | 176x |
if (x_index != 0) {
|
| 494 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 495 |
} |
|
| 496 | ||
| 497 | 176x |
if (!is.null(size)) {
|
| 498 | 8x |
xy <- .build_data_size( |
| 499 | 8x |
e$x$data[[i]], |
| 500 | 8x |
e$x$mapping$x, |
| 501 | 8x |
serie, |
| 502 | 8x |
size, |
| 503 | 8x |
scale, |
| 504 | 8x |
symbol_size, |
| 505 | 8x |
jitter_factor, |
| 506 | 8x |
jitter_amount |
| 507 |
) |
|
| 508 |
} else {
|
|
| 509 | 168x |
xy <- .build_data_jitter(e$x$data[[i]], e$x$mapping$x, serie, jitter_factor, jitter_amount) |
| 510 |
} |
|
| 511 | ||
| 512 | 176x |
if (!is.null(bind)) {
|
| 513 | ! |
xy <- .add_bind2(e, xy, bind, i = i) |
| 514 |
} |
|
| 515 | ||
| 516 | 176x |
e.serie <- list(data = xy) |
| 517 | ||
| 518 | 176x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 519 | ! |
grp_val <- names(e$x$data)[i] |
| 520 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 521 | ! |
e.serie$datasetId <- id |
| 522 | ! |
e.serie$data <- NULL |
| 523 | ! |
e.serie$encode <- list(x = e$x$mapping$x, y = serie) |
| 524 |
} |
|
| 525 | ||
| 526 | 176x |
if (coord_system == "polar") {
|
| 527 | 1x |
e.serie$data <- e$x$data[[i]] |> |
| 528 | 1x |
dplyr::select(dplyr::all_of(serie)) |> |
| 529 | 1x |
unlist() |> |
| 530 | 1x |
unname() |> |
| 531 | 1x |
as.list() |
| 532 |
} |
|
| 533 | ||
| 534 | 176x |
opts <- list( |
| 535 | 176x |
coordinateSystem = coord_system, |
| 536 |
... |
|
| 537 |
) |
|
| 538 | ||
| 539 | 176x |
if (coord_system == "cartesian2d") {
|
| 540 | 169x |
opts$yAxisIndex <- y_index |
| 541 | 169x |
opts$xAxisIndex <- x_index |
| 542 | 7x |
} else if (coord_system == "singleAxis") {
|
| 543 | 6x |
opts$singleAxisIndex <- x_index |
| 544 |
} |
|
| 545 | ||
| 546 | 176x |
if (!is.null(size)) {
|
| 547 | 8x |
opts$symbolSize <- htmlwidgets::JS( |
| 548 | 8x |
scale_js |
| 549 |
) |
|
| 550 |
} else {
|
|
| 551 | 168x |
size_total <- sum(symbol_size) |
| 552 | ||
| 553 | 168x |
if (size_total == 1 || size_total == 0) {
|
| 554 | 124x |
symbol_size <- 3 |
| 555 |
} |
|
| 556 | ||
| 557 | 168x |
opts$symbolSize <- symbol_size |
| 558 |
} |
|
| 559 | ||
| 560 | 176x |
if (!e$x$tl) {
|
| 561 | 119x |
nm <- .name_it(e, serie, name, i) |
| 562 | ||
| 563 | 119x |
if (is.null(name) && jitter_factor != 0) {
|
| 564 | ! |
nm <- paste0(nm, " jitter") |
| 565 |
} |
|
| 566 | ||
| 567 | 119x |
add_opts <- list( |
| 568 | 119x |
name = nm, |
| 569 | 119x |
type = "scatter", |
| 570 | 119x |
symbol = symbol, |
| 571 |
... |
|
| 572 |
) |
|
| 573 | ||
| 574 | 119x |
e.serie <- append(e.serie, add_opts) |
| 575 | 119x |
e.serie <- append(e.serie, opts) |
| 576 | ||
| 577 | 119x |
if (!coord_system %in% c("cartesian2d", "singleAxis", "polar", "matrix")) {
|
| 578 | ! |
e <- .rm_axis(e, rm_x, "x") |
| 579 | ! |
e <- .rm_axis(e, rm_y, "y") |
| 580 |
} |
|
| 581 | ||
| 582 | 119x |
if (isTRUE(legend)) {
|
| 583 | 119x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 584 |
} |
|
| 585 | ||
| 586 | 119x |
e$x$opts$series <- append(e$x$opts$series, list(e.serie)) |
| 587 |
} else {
|
|
| 588 | 57x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(e.serie)) |
| 589 |
} |
|
| 590 |
} |
|
| 591 | ||
| 592 | 130x |
if (isTRUE(e$x$tl)) {
|
| 593 | 20x |
if (is.null(name)) name <- serie |
| 594 | ||
| 595 | 20x |
add_opts <- list( |
| 596 | 20x |
name = name, |
| 597 | 20x |
type = "scatter", |
| 598 | 20x |
symbol = symbol, |
| 599 |
... |
|
| 600 |
) |
|
| 601 | ||
| 602 | 20x |
if (isTRUE(legend)) {
|
| 603 | 20x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 604 |
} |
|
| 605 | ||
| 606 | 20x |
series_opts <- append(opts, add_opts) |
| 607 | ||
| 608 | 20x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(series_opts)) |
| 609 |
} |
|
| 610 | ||
| 611 | 130x |
e |
| 612 |
} |
|
| 613 | ||
| 614 |
#' @rdname scatter |
|
| 615 |
#' @export |
|
| 616 |
e_effect_scatter_ <- function( |
|
| 617 |
e, |
|
| 618 |
serie, |
|
| 619 |
size = NULL, |
|
| 620 |
bind = NULL, |
|
| 621 |
symbol = NULL, |
|
| 622 |
symbol_size = 1, |
|
| 623 |
scale = e_scale, |
|
| 624 |
scale_js = "function(data){ return data[3];}",
|
|
| 625 |
name = NULL, |
|
| 626 |
coord_system = "cartesian2d", |
|
| 627 |
legend = TRUE, |
|
| 628 |
y_index = 0, |
|
| 629 |
x_index = 0, |
|
| 630 |
rm_x = TRUE, |
|
| 631 |
rm_y = TRUE, |
|
| 632 |
... |
|
| 633 |
) {
|
|
| 634 | 10x |
if (missing(e)) {
|
| 635 | 1x |
stop("must pass e", call. = FALSE)
|
| 636 |
} |
|
| 637 | ||
| 638 | 9x |
if (missing(serie)) {
|
| 639 | 1x |
stop("must pass serie", call. = FALSE)
|
| 640 |
} |
|
| 641 | ||
| 642 | 8x |
for (i in seq_along(e$x$data)) {
|
| 643 | 10x |
if (y_index != 0) {
|
| 644 | 1x |
e <- .set_y_axis(e, serie, y_index, i) |
| 645 |
} |
|
| 646 | ||
| 647 | 10x |
if (x_index != 0) {
|
| 648 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 649 |
} |
|
| 650 | ||
| 651 | 10x |
if (!is.null(size)) {
|
| 652 | ! |
xy <- .build_data_size(e$x$data[[i]], e$x$mapping$x, serie, size, scale, symbol_size) |
| 653 |
} else {
|
|
| 654 | 10x |
xy <- .build_data2(e$x$data[[i]], e$x$mapping$x, serie) |
| 655 |
} |
|
| 656 | ||
| 657 | 10x |
if (!is.null(bind)) {
|
| 658 | ! |
xy <- .add_bind2(e, xy, bind, i = i) |
| 659 |
} |
|
| 660 | ||
| 661 | 10x |
e.serie <- list(data = xy) |
| 662 | ||
| 663 | 10x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 664 | ! |
grp_val <- names(e$x$data)[i] |
| 665 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 666 | ! |
e.serie$datasetId <- id |
| 667 | ! |
e.serie$data <- NULL |
| 668 | ! |
e.serie$encode <- list(x = e$x$mapping$x, y = serie) |
| 669 |
} |
|
| 670 | ||
| 671 | ||
| 672 | 10x |
if (coord_system == "polar") {
|
| 673 | 1x |
e.serie$data <- e$x$data[[i]] |> |
| 674 | 1x |
dplyr::select(dplyr::all_of(serie)) |> |
| 675 | 1x |
unlist() |> |
| 676 | 1x |
unname() |> |
| 677 | 1x |
as.list() |
| 678 |
} |
|
| 679 | ||
| 680 | 10x |
opts <- list( |
| 681 | 10x |
coordinateSystem = coord_system, |
| 682 |
... |
|
| 683 |
) |
|
| 684 | ||
| 685 | 10x |
if (coord_system == "cartesian2d") {
|
| 686 | 8x |
opts$yAxisIndex <- y_index |
| 687 | 8x |
opts$xAxisIndex <- x_index |
| 688 | 2x |
} else if (coord_system == "singleAxis") {
|
| 689 | 1x |
opts$singleAxisIndex <- x_index |
| 690 |
} |
|
| 691 | ||
| 692 | 10x |
if (!is.null(size)) {
|
| 693 | ! |
opts$symbolSize <- htmlwidgets::JS( |
| 694 | ! |
scale_js |
| 695 |
) |
|
| 696 |
} else {
|
|
| 697 | 10x |
size_total <- sum(symbol_size) |
| 698 | ||
| 699 | 10x |
if (size_total == 1) {
|
| 700 | 8x |
symbol_size <- 10 |
| 701 |
} |
|
| 702 | ||
| 703 | 10x |
opts$symbolSize <- symbol_size |
| 704 |
} |
|
| 705 | ||
| 706 | 10x |
if (!e$x$tl) {
|
| 707 | 7x |
nm <- .name_it(e, serie, name, i) |
| 708 | ||
| 709 | 7x |
add_opts <- list( |
| 710 | 7x |
name = nm, |
| 711 | 7x |
type = "effectScatter", |
| 712 | 7x |
symbol = symbol, |
| 713 |
... |
|
| 714 |
) |
|
| 715 | ||
| 716 | 7x |
e.serie <- append(e.serie, add_opts) |
| 717 | 7x |
e.serie <- append(e.serie, opts) |
| 718 | ||
| 719 | 7x |
if (!coord_system %in% c("cartesian2d", "singleAxis", "polar", "matrix")) {
|
| 720 | ! |
e <- .rm_axis(e, rm_x, "x") |
| 721 | ! |
e <- .rm_axis(e, rm_y, "y") |
| 722 |
} |
|
| 723 | ||
| 724 | 7x |
if (isTRUE(legend)) {
|
| 725 | 7x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 726 |
} |
|
| 727 | ||
| 728 | 7x |
e$x$opts$series <- append(e$x$opts$series, list(e.serie)) |
| 729 |
} else {
|
|
| 730 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(e.serie)) |
| 731 |
} |
|
| 732 |
} |
|
| 733 | ||
| 734 | 8x |
if (isTRUE(e$x$tl)) {
|
| 735 | 1x |
if (is.null(name)) name <- serie |
| 736 | ||
| 737 | 1x |
add_opts <- list( |
| 738 | 1x |
name = name, |
| 739 | 1x |
type = "effectScatter", |
| 740 | 1x |
symbol = symbol, |
| 741 |
... |
|
| 742 |
) |
|
| 743 | ||
| 744 | 1x |
if (isTRUE(legend)) {
|
| 745 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 746 |
} |
|
| 747 | ||
| 748 | 1x |
series_opts <- append(opts, add_opts) |
| 749 | ||
| 750 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(series_opts)) |
| 751 |
} |
|
| 752 | ||
| 753 | 8x |
e |
| 754 |
} |
|
| 755 | ||
| 756 |
#' @rdname e_candle |
|
| 757 |
#' @export |
|
| 758 |
e_candle_ <- function(e, opening, closing, low, high, bind = NULL, name = "candle", legend = TRUE, ...) {
|
|
| 759 | 8x |
if (missing(e)) {
|
| 760 | 2x |
stop("must pass e", call. = FALSE)
|
| 761 |
} |
|
| 762 | ||
| 763 | 6x |
if (missing(opening) || missing(closing) || missing(low) || missing(high)) {
|
| 764 | 2x |
stop("missing inputs", call. = FALSE)
|
| 765 |
} |
|
| 766 | ||
| 767 | 4x |
for (i in seq_along(e$x$data)) {
|
| 768 | 5x |
data <- .build_data2(e$x$data[[i]], opening, closing, low, high) |
| 769 | ||
| 770 | 5x |
if (!is.null(bind)) {
|
| 771 | ! |
data <- .add_bind2(e, data, bind, i = i) |
| 772 |
} |
|
| 773 | ||
| 774 | 5x |
e.serie <- list( |
| 775 | 5x |
data = data |
| 776 |
) |
|
| 777 | ||
| 778 | 5x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 779 | ! |
grp_val <- names(e$x$data)[i] |
| 780 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 781 | ! |
e.serie$datasetId <- id |
| 782 | ! |
e.serie$data <- NULL |
| 783 |
} |
|
| 784 | ||
| 785 | 5x |
if (!e$x$tl) {
|
| 786 | 3x |
nm <- .name_it(e, NULL, name, i) |
| 787 | ||
| 788 | 3x |
opts <- list( |
| 789 | 3x |
name = nm, |
| 790 | 3x |
type = "candlestick", |
| 791 |
... |
|
| 792 |
) |
|
| 793 | ||
| 794 | 3x |
e.serie <- append(e.serie, opts) |
| 795 | ||
| 796 | 3x |
if (isTRUE(legend)) {
|
| 797 | 3x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 798 |
} |
|
| 799 | ||
| 800 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(e.serie)) |
| 801 |
} else {
|
|
| 802 | 2x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(e.serie)) |
| 803 |
} |
|
| 804 |
} |
|
| 805 | ||
| 806 | ||
| 807 | 4x |
if (isTRUE(e$x$tl)) {
|
| 808 | 1x |
add_opts <- list( |
| 809 | 1x |
name = name, |
| 810 | 1x |
type = "candlestick", |
| 811 |
... |
|
| 812 |
) |
|
| 813 | ||
| 814 | 1x |
if (isTRUE(legend)) {
|
| 815 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 816 |
} |
|
| 817 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(add_opts)) |
| 818 |
} |
|
| 819 | ||
| 820 | 4x |
e |
| 821 |
} |
|
| 822 | ||
| 823 |
#' @rdname e_radar |
|
| 824 |
#' @export |
|
| 825 |
e_radar_ <- function( |
|
| 826 |
e, |
|
| 827 |
serie, |
|
| 828 |
max = 100, |
|
| 829 |
name = NULL, |
|
| 830 |
legend = TRUE, |
|
| 831 |
rm_x = TRUE, |
|
| 832 |
rm_y = TRUE, |
|
| 833 |
..., |
|
| 834 |
radar = list() |
|
| 835 |
) {
|
|
| 836 | 14x |
r.index <- 0 |
| 837 | ||
| 838 | 14x |
if (missing(e)) {
|
| 839 | 1x |
stop("must pass e", call. = FALSE)
|
| 840 |
} |
|
| 841 | ||
| 842 | 13x |
if (missing(serie)) {
|
| 843 | 1x |
stop("must pass serie", call. = FALSE)
|
| 844 |
} |
|
| 845 | 1x |
if(e$x$tl)( |
| 846 | 1x |
stop("timeline not supported")
|
| 847 |
) |
|
| 848 | ||
| 849 |
# remove axis |
|
| 850 | 11x |
e <- .rm_axis(e, rm_x, "x") |
| 851 | 11x |
e <- .rm_axis(e, rm_y, "y") |
| 852 | ||
| 853 | 11x |
if (is.null(name)) { # defaults to column name
|
| 854 | 7x |
name <- serie |
| 855 |
} |
|
| 856 | ||
| 857 |
# build JSON data |
|
| 858 | 11x |
vector <- .get_data(e, serie) |
| 859 | ||
| 860 | 11x |
series <- purrr::map(e$x$opts$series, "type") |> |
| 861 | 11x |
unlist() |
| 862 | ||
| 863 | 11x |
if (!"radar" %in% series) {
|
| 864 | 8x |
serie <- list( |
| 865 | 8x |
type = "radar", |
| 866 | 8x |
data = list(list(value = vector, name = name)), |
| 867 | 8x |
radarIndex = r.index, |
| 868 |
... |
|
| 869 |
) |
|
| 870 | ||
| 871 | 8x |
e <- .add_indicators(e, r.index, max, radar = radar) |
| 872 | ||
| 873 |
# add serie |
|
| 874 | 8x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 875 |
} else { # append to radar
|
|
| 876 | 3x |
e$x$opts$series[[grep("radar", series)]]$data <- append(
|
| 877 | 3x |
e$x$opts$series[[grep("radar", series)]]$data,
|
| 878 | 3x |
list(list(value = vector, name = name)) |
| 879 |
) |
|
| 880 |
} |
|
| 881 | ||
| 882 | 11x |
if (isTRUE(legend)) {
|
| 883 | 11x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(name)) |
| 884 |
} |
|
| 885 | ||
| 886 | 11x |
e |
| 887 |
} |
|
| 888 | ||
| 889 |
#' @rdname e_funnel |
|
| 890 |
#' @export |
|
| 891 |
e_funnel_ <- function(e, values, labels, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 892 | 12x |
if (missing(e)) {
|
| 893 | 1x |
stop("must pass e", call. = FALSE)
|
| 894 |
} |
|
| 895 | ||
| 896 | 11x |
if (missing(values) || missing(labels)) {
|
| 897 | 1x |
stop("missing values or labels", call. = FALSE)
|
| 898 |
} |
|
| 899 | ||
| 900 | 10x |
for (i in seq_along(e$x$data)) {
|
| 901 | ||
| 902 |
# build JSON data |
|
| 903 | 12x |
funnel <- .build_data2(e$x$data[[i]], values) |
| 904 | ||
| 905 | 12x |
funnel <- .add_bind2(e, funnel, labels, i = i) |
| 906 | ||
| 907 | 12x |
serie <- list(data = funnel) |
| 908 | ||
| 909 | 12x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 910 | ! |
grp_val <- names(e$x$data)[i] |
| 911 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 912 | ! |
serie$datasetId <- id |
| 913 | ! |
serie$data <- NULL |
| 914 |
} |
|
| 915 | ||
| 916 | 12x |
opts <- list( |
| 917 | 12x |
name = name, |
| 918 | 12x |
type = "funnel", |
| 919 |
... |
|
| 920 |
) |
|
| 921 | ||
| 922 |
# remove axis |
|
| 923 | 12x |
e <- .rm_axis(e, rm_x, "x") |
| 924 | 12x |
e <- .rm_axis(e, rm_y, "y") |
| 925 | ||
| 926 | 12x |
if (!e$x$tl) {
|
| 927 | 9x |
serie <- append(serie, opts) |
| 928 | ||
| 929 |
# addlegend |
|
| 930 | 9x |
if (isTRUE(legend)) {
|
| 931 | 9x |
legend <- .get_data(e, labels) |> |
| 932 | 9x |
as.character() |> |
| 933 | 9x |
unique() |
| 934 | 9x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, legend) |
| 935 |
} |
|
| 936 | ||
| 937 | 9x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 938 |
} else {
|
|
| 939 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie)) |
| 940 |
} |
|
| 941 |
} |
|
| 942 | ||
| 943 | 10x |
if (isTRUE(e$x$tl)) {
|
| 944 | 1x |
if (isTRUE(legend)) {
|
| 945 | 1x |
legend <- .get_data(e, labels) |> |
| 946 | 1x |
as.character() |> |
| 947 | 1x |
unique() |
| 948 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, legend) |
| 949 |
} |
|
| 950 | ||
| 951 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(opts)) |
| 952 |
} |
|
| 953 | ||
| 954 | 10x |
e |
| 955 |
} |
|
| 956 | ||
| 957 |
#' @rdname e_sankey |
|
| 958 |
#' @export |
|
| 959 |
e_sankey_ <- function(e, source, target, value, layout = "none", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 960 | 6x |
if (missing(e)) {
|
| 961 | 1x |
stop("must pass e", call. = FALSE)
|
| 962 |
} |
|
| 963 | 5x |
if (missing(source) || missing(target) || missing(value)) {
|
| 964 | 1x |
stop("missing source, target or values", call. = FALSE)
|
| 965 |
} |
|
| 966 | 1x |
if(e$x$tl)( |
| 967 | 1x |
stop("timeline not supported")
|
| 968 |
) |
|
| 969 | ||
| 970 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 971 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 972 | ||
| 973 |
# build JSON data |
|
| 974 | 3x |
nodes <- .build_sankey_nodes(e$x$data[[1]], source, target) |
| 975 | ||
| 976 |
# build JSON data |
|
| 977 | 3x |
edges <- .build_sankey_edges(e$x$data[[1]], source, target, value) |
| 978 | ||
| 979 | 3x |
serie <- list( |
| 980 | 3x |
type = "sankey", |
| 981 | 3x |
orient = layout, |
| 982 | 3x |
data = nodes, |
| 983 | 3x |
links = edges, |
| 984 |
... |
|
| 985 |
) |
|
| 986 | ||
| 987 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 988 | 3x |
e |
| 989 |
} |
|
| 990 | ||
| 991 |
#' @rdname e_heatmap |
|
| 992 |
#' @export |
|
| 993 |
e_heatmap_ <- function( |
|
| 994 |
e, |
|
| 995 |
y, |
|
| 996 |
z = NULL, |
|
| 997 |
bind = NULL, |
|
| 998 |
name = NULL, |
|
| 999 |
coord_system = "cartesian2d", |
|
| 1000 |
rm_x = TRUE, |
|
| 1001 |
rm_y = TRUE, |
|
| 1002 |
calendar = NULL, |
|
| 1003 |
... |
|
| 1004 |
) {
|
|
| 1005 | 14x |
if (missing(e)) {
|
| 1006 | 2x |
stop("must pass e", call. = FALSE)
|
| 1007 |
} |
|
| 1008 | 12x |
if (missing(y)) {
|
| 1009 | 2x |
stop("must pass y", call. = FALSE)
|
| 1010 |
} |
|
| 1011 | ||
| 1012 | 10x |
if(coord_system == "matrix"){
|
| 1013 | ! |
e <- e |> e_heatmap_matrix(y, ...) |
| 1014 | ! |
return(e) |
| 1015 |
} |
|
| 1016 | ||
| 1017 | 10x |
for (i in seq_along(e$x$data)) {
|
| 1018 | 11x |
if (!is.null(z)) {
|
| 1019 | 8x |
xyz <- .build_data2(e$x$data[[i]], e$x$mapping$x, y, z) |
| 1020 |
} else {
|
|
| 1021 | 3x |
xyz <- .build_data2(e$x$data[[i]], e$x$mapping$x, y) |
| 1022 |
} |
|
| 1023 | ||
| 1024 | 11x |
if (!is.null(bind)) {
|
| 1025 | ! |
xyz <- .add_bind2(e, xyz, bind, i = i) |
| 1026 |
} |
|
| 1027 | ||
| 1028 | 11x |
serie <- list(data = xyz) |
| 1029 | ||
| 1030 | 11x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 1031 | ! |
grp_val <- names(e$x$data)[i] |
| 1032 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 1033 | ! |
serie$datasetId <- id |
| 1034 | ! |
serie$data <- NULL |
| 1035 | ! |
serie$encode <- list(x = e$x$mapping$x, y = y) |
| 1036 |
} |
|
| 1037 | ||
| 1038 | 11x |
series_opts <- list( |
| 1039 | 11x |
name = name, |
| 1040 | 11x |
type = "heatmap", |
| 1041 | 11x |
coordinateSystem = coord_system, |
| 1042 |
... |
|
| 1043 |
) |
|
| 1044 | ||
| 1045 | 11x |
if (coord_system == "calendar") {
|
| 1046 | 3x |
if (!is.null(calendar)) {
|
| 1047 | ! |
series_opts$calendarIndex <- calendar[i] |
| 1048 | 3x |
} else if (length(e$x$opts$calendar) == length(e$x$data)) {
|
| 1049 | 1x |
series_opts$calendarIndex <- i - 1 |
| 1050 |
} |
|
| 1051 |
} |
|
| 1052 | ||
| 1053 | 11x |
if (coord_system != "cartesian2d") {
|
| 1054 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 1055 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 1056 |
} else {
|
|
| 1057 | ||
| 1058 | 8x |
xdata <- unique(.get_data(e, e$x$mapping$x, i)) |
| 1059 | ||
| 1060 | 8x |
if (length(xdata) == 1) {
|
| 1061 | ! |
xdata <- list(xdata) |
| 1062 |
} |
|
| 1063 | ||
| 1064 | 8x |
if (!e$x$tl) {
|
| 1065 | 7x |
e$x$opts$xAxis <- list(list(data = xdata)) |
| 1066 |
} else {
|
|
| 1067 | 1x |
e$x$opts$baseOption$xAxis <- list(list(data = xdata)) |
| 1068 |
} |
|
| 1069 | ||
| 1070 | 8x |
ydata <- unique(.get_data(e, y, i)) |
| 1071 | ||
| 1072 | 8x |
if (length(ydata) == 1) {
|
| 1073 | ! |
ydata <- list(ydata) |
| 1074 |
} |
|
| 1075 | ||
| 1076 | 8x |
if (!e$x$tl) {
|
| 1077 | 7x |
e$x$opts$yAxis <- list(list(data = ydata)) |
| 1078 |
} else {
|
|
| 1079 | 1x |
e$x$opts$baseOption$yAxis <- list(list(data = ydata)) |
| 1080 |
} |
|
| 1081 |
} |
|
| 1082 | ||
| 1083 | 11x |
if (!e$x$tl) {
|
| 1084 | 8x |
serie <- append(serie, series_opts) |
| 1085 | ||
| 1086 | 8x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 1087 |
} else { # timeline
|
|
| 1088 | ||
| 1089 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie)) |
| 1090 |
} |
|
| 1091 |
} |
|
| 1092 | ||
| 1093 | 10x |
if (isTRUE(e$x$tl)) {
|
| 1094 | 2x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(series_opts)) |
| 1095 |
} |
|
| 1096 | ||
| 1097 | 10x |
e |
| 1098 |
} |
|
| 1099 | ||
| 1100 |
#' @rdname e_parallel |
|
| 1101 |
#' @export |
|
| 1102 |
e_parallel_ <- function(e, ..., name = NULL, rm_x = TRUE, rm_y = TRUE, opts = list()) {
|
|
| 1103 | 3x |
if (missing(e)) {
|
| 1104 | 1x |
stop("must pass e", call. = FALSE)
|
| 1105 |
} |
|
| 1106 | 1x |
if(e$x$tl)( |
| 1107 | 1x |
stop("timeline not supported")
|
| 1108 |
) |
|
| 1109 | ||
| 1110 | 1x |
e <- .rm_axis(e, rm_x, "x") |
| 1111 | 1x |
e <- .rm_axis(e, rm_y, "y") |
| 1112 | ||
| 1113 | 1x |
data <- e$x$data[[1]][, c(...), drop = FALSE] |
| 1114 | ||
| 1115 |
# remove names |
|
| 1116 | 1x |
df <- data |
| 1117 | 1x |
row.names(data) <- NULL |
| 1118 | 1x |
data <- unname(data) |
| 1119 | ||
| 1120 | 1x |
data <- apply(data, 1, as.list) |
| 1121 | ||
| 1122 |
# remove white spaces (ex: in " 1") |
|
| 1123 | 1x |
data <- purrr::map(data, function(x) {
|
| 1124 | 3x |
gsub(" ", "", x) |>
|
| 1125 | 3x |
as.list() |
| 1126 |
}) |
|
| 1127 | ||
| 1128 | 1x |
serie <- list( |
| 1129 | 1x |
name = name, |
| 1130 | 1x |
type = "parallel", |
| 1131 | 1x |
data = data |
| 1132 |
) |
|
| 1133 | ||
| 1134 | 1x |
serie <- append(serie, opts) |
| 1135 | ||
| 1136 | 1x |
para <- list() |
| 1137 | 1x |
for (i in seq_len(ncol(df))) {
|
| 1138 | 3x |
line <- list() |
| 1139 | 3x |
line$dim <- i - 1 |
| 1140 | 3x |
line$name <- names(df)[i] |
| 1141 | 3x |
if (inherits(df[, i], "character") || inherits(df[, i], "factor")) {
|
| 1142 | 1x |
line$type <- "category" |
| 1143 | 1x |
line$data <- unique(df[, i]) |
| 1144 |
} |
|
| 1145 | ||
| 1146 | 3x |
para[[i]] <- line |
| 1147 |
} |
|
| 1148 | ||
| 1149 | 1x |
e$x$opts$series <- append(e$x$opts$series, serie) |
| 1150 | 1x |
e$x$opts$parallelAxis <- para |
| 1151 | 1x |
e |
| 1152 |
} |
|
| 1153 | ||
| 1154 |
#' @rdname e_pie |
|
| 1155 |
#' @export |
|
| 1156 |
e_pie_ <- function(e, serie, name = NULL, legend = TRUE, coord_system = "", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1157 | 10x |
if (missing(e)) {
|
| 1158 | 1x |
stop("must pass e", call. = FALSE)
|
| 1159 |
} |
|
| 1160 | ||
| 1161 | 9x |
if (missing(serie)) {
|
| 1162 | 1x |
stop("must pass serie", call. = FALSE)
|
| 1163 |
} |
|
| 1164 | ||
| 1165 | 8x |
e <- .rm_axis(e, rm_x, "x") |
| 1166 | 8x |
e <- .rm_axis(e, rm_y, "y") |
| 1167 | ||
| 1168 | 8x |
if(coord_system == "matrix"){
|
| 1169 | ! |
e <- e |> e_pie_matrix(e$x$mapping$x, serie, legend, ...) |
| 1170 | ! |
return(e) |
| 1171 |
} |
|
| 1172 | ||
| 1173 | 8x |
for (i in seq_along(e$x$data)) {
|
| 1174 | ||
| 1175 |
# build JSON data |
|
| 1176 | 11x |
data <- .build_data2(e$x$data[[i]], serie) |
| 1177 | 11x |
data <- .add_bind2(e, data, e$x$mapping$x, i = i) |
| 1178 | ||
| 1179 | 11x |
serie_data <- list(data = data) |
| 1180 | ||
| 1181 | 11x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 1182 | ! |
grp_val <- names(e$x$data)[i] |
| 1183 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 1184 | ! |
serie_data$datasetId <- id |
| 1185 | ! |
serie_data$data <- NULL |
| 1186 | ! |
serie_data$encode <- list(x = e$x$mapping$x, y = serie) |
| 1187 |
} |
|
| 1188 | ||
| 1189 | 11x |
serie_opts <- list( |
| 1190 | 11x |
name = name, |
| 1191 | 11x |
type = "pie", |
| 1192 |
... |
|
| 1193 |
) |
|
| 1194 | ||
| 1195 | 11x |
if (!e$x$tl) {
|
| 1196 | 8x |
if (is.null(name)) { # defaults to column name
|
| 1197 | 7x |
name <- serie |
| 1198 |
} |
|
| 1199 | ||
| 1200 | 8x |
serie_opts$name <- name |
| 1201 | ||
| 1202 | 8x |
serie_opts <- append(serie_opts, serie_data) |
| 1203 | ||
| 1204 | 8x |
if (isTRUE(legend)) {
|
| 1205 | 8x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, e$x$data[[i]][[e$x$mapping$x]]) |
| 1206 |
} |
|
| 1207 | ||
| 1208 | 8x |
e$x$opts$series <- append(e$x$opts$series, list(serie_opts)) |
| 1209 |
} else {
|
|
| 1210 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie_data)) |
| 1211 |
} |
|
| 1212 |
} |
|
| 1213 | ||
| 1214 | 8x |
if (isTRUE(e$x$tl)) {
|
| 1215 | 1x |
if (isTRUE(legend)) {
|
| 1216 | 1x |
e$x$opts$baseOption$legend$data <- append( |
| 1217 | 1x |
e$x$opts$baseOption$legend$data, |
| 1218 | 1x |
purrr::map(e$x$data, "model") |> |
| 1219 | 1x |
unlist() |> |
| 1220 | 1x |
unique() |
| 1221 |
) |
|
| 1222 |
} |
|
| 1223 | ||
| 1224 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 1225 |
} |
|
| 1226 | ||
| 1227 | 8x |
e |
| 1228 |
} |
|
| 1229 | ||
| 1230 |
#' @rdname e_sunburst |
|
| 1231 |
#' @export |
|
| 1232 |
e_sunburst_ <- function(e, styles = NULL, names = NULL, levels = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1233 | 6x |
if (missing(e)) {
|
| 1234 | 1x |
stop("must pass e", call. = FALSE)
|
| 1235 |
} |
|
| 1236 | 1x |
if(e$x$tl)( |
| 1237 | 1x |
stop("timeline not supported")
|
| 1238 |
) |
|
| 1239 | 4x |
e <- .rm_axis(e, rm_x, "x") |
| 1240 | 4x |
e <- .rm_axis(e, rm_y, "y") |
| 1241 | ||
| 1242 |
# build JSON data |
|
| 1243 | 4x |
data <- .build_sun(e, styles, names, levels) |
| 1244 | ||
| 1245 | 4x |
serie <- list( |
| 1246 | 4x |
type = "sunburst", |
| 1247 | 4x |
data = data, |
| 1248 |
... |
|
| 1249 |
) |
|
| 1250 | ||
| 1251 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 1252 | 4x |
e |
| 1253 |
} |
|
| 1254 | ||
| 1255 |
#' @rdname e_treemap |
|
| 1256 |
#' @export |
|
| 1257 |
e_treemap_ <- function(e, styles = NULL, names = NULL, levels = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1258 | 5x |
if (missing(e)) {
|
| 1259 | 1x |
stop("must pass e", call. = FALSE)
|
| 1260 |
} |
|
| 1261 | 1x |
if(e$x$tl)( |
| 1262 | 1x |
stop("timeline not supported")
|
| 1263 |
) |
|
| 1264 | ||
| 1265 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 1266 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 1267 | ||
| 1268 |
# build JSON data |
|
| 1269 | 3x |
data <- .build_sun(e, styles, names, levels) |
| 1270 | ||
| 1271 | 3x |
serie <- list( |
| 1272 | 3x |
type = "treemap", |
| 1273 | 3x |
data = data, |
| 1274 |
... |
|
| 1275 |
) |
|
| 1276 | ||
| 1277 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 1278 | 3x |
e |
| 1279 |
} |
|
| 1280 | ||
| 1281 |
#' @rdname e_river |
|
| 1282 |
#' @export |
|
| 1283 |
e_river_ <- function(e, serie, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1284 | 8x |
if (missing(e)) {
|
| 1285 | 1x |
stop("must pass e", call. = FALSE)
|
| 1286 |
} |
|
| 1287 | ||
| 1288 | 7x |
if (missing(serie)) {
|
| 1289 | 1x |
stop("must pass serie", call. = FALSE)
|
| 1290 |
} |
|
| 1291 | 1x |
if(e$x$tl)( |
| 1292 | 1x |
stop("timeline not supported")
|
| 1293 |
) |
|
| 1294 | ||
| 1295 | 5x |
for (i in seq_along(e$x$data)) {
|
| 1296 | 5x |
nm <- .name_it(e, serie, name, i) |
| 1297 | ||
| 1298 | 5x |
if (length(e$x$opts$xAxis$data)) {
|
| 1299 | ! |
e$X <- e$x$opts$xAxis$data |
| 1300 |
} |
|
| 1301 | ||
| 1302 | 5x |
data <- .build_river(e, serie, nm, i) |
| 1303 | ||
| 1304 | 5x |
if (!length(e$x$opts$series)) {
|
| 1305 | 3x |
e.serie <- list( |
| 1306 | 3x |
type = "themeRiver", |
| 1307 | 3x |
data = data, |
| 1308 |
... |
|
| 1309 |
) |
|
| 1310 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(e.serie)) |
| 1311 |
} else {
|
|
| 1312 | 2x |
e$x$opts$series[[1]]$data <- append(e$x$opts$series[[1]]$data, data) |
| 1313 |
} |
|
| 1314 | ||
| 1315 | 5x |
e <- .rm_axis(e, rm_x, "x") |
| 1316 | 5x |
e <- .rm_axis(e, rm_y, "y") |
| 1317 | ||
| 1318 | 5x |
e$x$opts$singleAxis <- list(type = "time") |
| 1319 | ||
| 1320 | 5x |
if (isTRUE(legend)) {
|
| 1321 | 5x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 1322 |
} |
|
| 1323 |
} |
|
| 1324 | ||
| 1325 | 5x |
e |
| 1326 |
} |
|
| 1327 | ||
| 1328 |
#' @rdname e_boxplot |
|
| 1329 |
#' @export |
|
| 1330 |
e_boxplot_ <- function(e, serie, name = NULL, outliers = TRUE, ...) {
|
|
| 1331 | 5x |
if (missing(e)) {
|
| 1332 | 1x |
stop("must pass e", call. = FALSE)
|
| 1333 |
} |
|
| 1334 | 4x |
if (missing(serie)) {
|
| 1335 | 1x |
stop("must pass serie", call. = FALSE)
|
| 1336 |
} |
|
| 1337 | ||
| 1338 | 3x |
for (i in seq_along(e$x$data)) {
|
| 1339 | ||
| 1340 |
# build JSON data |
|
| 1341 | 7x |
vector <- .build_boxplot(e, serie, i) |
| 1342 | ||
| 1343 | 7x |
if (!e$x$tl) {
|
| 1344 | 7x |
nm <- .name_it(e, serie, name, i) |
| 1345 | ||
| 1346 | 7x |
if (length(e$x$opts$series) >= 1) {
|
| 1347 | 4x |
e$x$opts$series[[1]]$data <- append( |
| 1348 | 4x |
e$x$opts$series[[1]]$data, |
| 1349 | 4x |
list(vector) |
| 1350 |
) |
|
| 1351 |
} else {
|
|
| 1352 |
# boxplot + opts |
|
| 1353 | 3x |
box <- list( |
| 1354 | 3x |
name = nm, |
| 1355 | 3x |
type = "boxplot", |
| 1356 | 3x |
data = list(vector), |
| 1357 |
... |
|
| 1358 |
) |
|
| 1359 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(box)) |
| 1360 |
} |
|
| 1361 | ||
| 1362 |
# data/outliers |
|
| 1363 | 7x |
if (isTRUE(outliers)) {
|
| 1364 | 7x |
e <- .add_outliers(e, serie, i) |
| 1365 |
} |
|
| 1366 | ||
| 1367 |
# xaxis |
|
| 1368 | 7x |
xs <- names(e$x$data)[i] |
| 1369 | 7x |
if (is.null(xs)) {
|
| 1370 | 1x |
xs <- serie |
| 1371 |
} |
|
| 1372 | 7x |
e$x$opts$xAxis[[1]]$data <- append(e$x$opts$xAxis[[1]]$data, list(xs)) |
| 1373 | 7x |
e$x$opts$xAxis[[1]]$type <- "category" |
| 1374 |
} else {
|
|
| 1375 | ! |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(list(data = vector))) |
| 1376 |
} |
|
| 1377 |
} |
|
| 1378 | ||
| 1379 | 3x |
if (isTRUE(e$x$tl)) {
|
| 1380 | ! |
serie_opts <- list( |
| 1381 | ! |
type = "boxplot", |
| 1382 |
... |
|
| 1383 |
) |
|
| 1384 | ||
| 1385 | ! |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 1386 |
} |
|
| 1387 | ||
| 1388 | 3x |
e |
| 1389 |
} |
|
| 1390 | ||
| 1391 |
#' @rdname e_tree |
|
| 1392 |
#' @export |
|
| 1393 |
e_tree_ <- function(e, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1394 | 5x |
if (missing(e)) {
|
| 1395 | 1x |
stop("must pass e", call. = FALSE)
|
| 1396 |
} |
|
| 1397 | 1x |
if(e$x$tl)( |
| 1398 | 1x |
stop("timeline not supported")
|
| 1399 |
) |
|
| 1400 | ||
| 1401 |
# remove axis |
|
| 1402 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 1403 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 1404 | ||
| 1405 |
# build JSON data |
|
| 1406 | 3x |
data <- .build_tree(e) |
| 1407 | ||
| 1408 | 3x |
serie <- list( |
| 1409 | 3x |
type = "tree", |
| 1410 | 3x |
data = data, |
| 1411 |
... |
|
| 1412 |
) |
|
| 1413 | ||
| 1414 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 1415 | 3x |
e |
| 1416 |
} |
|
| 1417 | ||
| 1418 |
#' @rdname line3D |
|
| 1419 |
#' @export |
|
| 1420 |
e_lines_3d_ <- function( |
|
| 1421 |
e, |
|
| 1422 |
source_lon, |
|
| 1423 |
source_lat, |
|
| 1424 |
target_lon, |
|
| 1425 |
target_lat, |
|
| 1426 |
source_name = NULL, |
|
| 1427 |
target_name = NULL, |
|
| 1428 |
value = NULL, |
|
| 1429 |
name = NULL, |
|
| 1430 |
coord_system = "globe", |
|
| 1431 |
rm_x = TRUE, |
|
| 1432 |
rm_y = TRUE, |
|
| 1433 |
... |
|
| 1434 |
) {
|
|
| 1435 | ||
| 1436 | 8x |
if (missing(e)) {
|
| 1437 | 1x |
stop("must pass e", call. = FALSE)
|
| 1438 |
} |
|
| 1439 | ||
| 1440 | 7x |
if (missing(source_lat) || missing(source_lon) || missing(target_lat) || missing(target_lon)) {
|
| 1441 | 1x |
stop("missing coordinates", call. = FALSE)
|
| 1442 |
} |
|
| 1443 | ||
| 1444 |
# In proxy, these were turning into "NULL" |
|
| 1445 | 6x |
if (missing(source_name) || identical(source_name, "NULL")) {
|
| 1446 | 2x |
source_name <- NULL |
| 1447 |
} |
|
| 1448 | ||
| 1449 | 6x |
if (missing(target_name) || identical(target_name, "NULL")) {
|
| 1450 | 2x |
target_name <- NULL |
| 1451 |
} |
|
| 1452 | ||
| 1453 | 6x |
if (missing(value) || identical(value, "NULL")) {
|
| 1454 | 2x |
value <- NULL |
| 1455 |
} |
|
| 1456 | ||
| 1457 |
# remove axis |
|
| 1458 | 6x |
e <- .rm_axis(e, rm_x, "x") |
| 1459 | 6x |
e <- .rm_axis(e, rm_y, "y") |
| 1460 | ||
| 1461 | 6x |
for (i in seq_along(e$x$data)) {
|
| 1462 | 8x |
data <- .map_lines( |
| 1463 | 8x |
e, |
| 1464 | 8x |
source_lon, |
| 1465 | 8x |
source_lat, |
| 1466 | 8x |
target_lon, |
| 1467 | 8x |
target_lat, |
| 1468 | 8x |
source_name, |
| 1469 | 8x |
target_name, |
| 1470 | 8x |
value, |
| 1471 | 8x |
i = i |
| 1472 |
) |
|
| 1473 | ||
| 1474 | 8x |
serie_data <- list(data = data) |
| 1475 | ||
| 1476 | 8x |
serie_opts <- list( |
| 1477 | 8x |
type = "lines3D", |
| 1478 | 8x |
coordinateSystem = coord_system, |
| 1479 | ||
| 1480 |
# Adding this fixed: "Uncaught TypeError: Cannot read properties of undefined (reading 'getSource')" |
|
| 1481 | 8x |
data = list(), |
| 1482 |
... |
|
| 1483 |
) |
|
| 1484 | ||
| 1485 | 8x |
if (!e$x$tl) {
|
| 1486 | 5x |
serie <- append(serie_data, serie_opts) |
| 1487 | ||
| 1488 | 5x |
nm <- .name_it(e, NULL, name, i) |
| 1489 | ||
| 1490 | 5x |
if (!is.null(nm)) {
|
| 1491 | 4x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 1492 | 4x |
serie$name <- nm |
| 1493 |
} |
|
| 1494 | ||
| 1495 | 5x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 1496 |
} else {
|
|
| 1497 | 3x |
if (!is.null(name)) {
|
| 1498 | 3x |
serie_opts$name <- name |
| 1499 |
} |
|
| 1500 | ||
| 1501 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie_data)) |
| 1502 |
} |
|
| 1503 |
} |
|
| 1504 | ||
| 1505 | 6x |
if (isTRUE(e$x$tl)) {
|
| 1506 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 1507 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 1508 |
} |
|
| 1509 | ||
| 1510 |
# add dependency |
|
| 1511 | 6x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 1512 | 6x |
dep <- htmltools::htmlDependency( |
| 1513 | 6x |
name = "echarts-gl", |
| 1514 | 6x |
version = "1.1.2", |
| 1515 | 6x |
src = c(file = path), |
| 1516 | 6x |
script = "echarts-gl.min.js" |
| 1517 |
) |
|
| 1518 | ||
| 1519 | 6x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 1520 | ||
| 1521 | 6x |
e |
| 1522 |
} |
|
| 1523 | ||
| 1524 |
#' @rdname line3D |
|
| 1525 |
#' @export |
|
| 1526 |
e_line_3d_ <- function(e, y, z, name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1527 | 5x |
if (missing(e)) {
|
| 1528 | 1x |
stop("must pass e", call. = FALSE)
|
| 1529 |
} |
|
| 1530 | ||
| 1531 | 4x |
if (missing(y) || missing(z)) {
|
| 1532 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 1533 |
} |
|
| 1534 | ||
| 1535 |
# remove axis |
|
| 1536 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 1537 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 1538 | ||
| 1539 | 3x |
for (i in seq_along(e$x$data)) {
|
| 1540 | ||
| 1541 |
# build JSON data |
|
| 1542 | 4x |
data <- .build_data2(e$x$data[[i]], e$x$mapping$x, y, z) |
| 1543 | ||
| 1544 | 4x |
e <- .set_axis_3D(e, "x", e$x$mapping$x, 0) |
| 1545 | 4x |
e <- .set_axis_3D(e, "y", y, 0) |
| 1546 | 4x |
e <- .set_axis_3D(e, "z", z, 0) |
| 1547 | ||
| 1548 | 4x |
if (!e$x$tl) {
|
| 1549 | 2x |
nm <- .name_it(e, NULL, name, i) |
| 1550 | ||
| 1551 | 2x |
if (!is.null(nm)) {
|
| 1552 | ! |
e$x$opts$legend$data <- append(e$x$opts$legend$data, nm) |
| 1553 |
} |
|
| 1554 | ||
| 1555 | 2x |
if (!length(e$x$opts$zAxis3D)) {
|
| 1556 | ! |
e$x$opts$zAxis3D <- list(list(show = TRUE)) |
| 1557 |
} |
|
| 1558 | ||
| 1559 | 2x |
if (!length(e$x$opts$grid3D)) {
|
| 1560 | 2x |
e$x$opts$grid3D <- list(list(show = TRUE)) |
| 1561 |
} |
|
| 1562 | ||
| 1563 | 2x |
e.serie <- list( |
| 1564 | 2x |
type = "line3D", |
| 1565 | 2x |
data = data, |
| 1566 | 2x |
name = nm, |
| 1567 |
... |
|
| 1568 |
) |
|
| 1569 | ||
| 1570 | 2x |
if (!is.null(coord_system)) {
|
| 1571 | ! |
e.serie$coordinateSystem <- coord_system |
| 1572 |
} |
|
| 1573 | ||
| 1574 | 2x |
e$x$opts$series <- append(e$x$opts$series, list(e.serie)) |
| 1575 |
} else {
|
|
| 1576 | 2x |
if (!length(e$x$opts$zAxis3D)) {
|
| 1577 | 2x |
e$x$opts$baseOption$zAxis3D <- list(list(show = TRUE)) |
| 1578 |
} |
|
| 1579 | ||
| 1580 | 2x |
if (!length(e$x$opts$grid3D)) {
|
| 1581 | 2x |
e$x$opts$baseOption$grid3D <- list(list(show = TRUE)) |
| 1582 |
} |
|
| 1583 | ||
| 1584 | 2x |
e_serie <- list(data = data) |
| 1585 | ||
| 1586 | 2x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(e_serie)) |
| 1587 |
} |
|
| 1588 |
} |
|
| 1589 | ||
| 1590 | 3x |
if (e$x$tl) {
|
| 1591 | 1x |
serie_opts <- list( |
| 1592 | 1x |
type = "line3D", |
| 1593 | 1x |
name = name, |
| 1594 |
... |
|
| 1595 |
) |
|
| 1596 | ||
| 1597 | 1x |
if (!is.null(coord_system)) {
|
| 1598 | ! |
serie_opts$coordinateSystem <- coord_system |
| 1599 |
} |
|
| 1600 | ||
| 1601 | 1x |
if (!is.null(name)) {
|
| 1602 | ! |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, name) |
| 1603 |
} |
|
| 1604 | ||
| 1605 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 1606 |
} |
|
| 1607 | ||
| 1608 |
# add dependency |
|
| 1609 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 1610 | 3x |
dep <- htmltools::htmlDependency( |
| 1611 | 3x |
name = "echarts-gl", |
| 1612 | 3x |
version = "1.1.2", |
| 1613 | 3x |
src = c(file = path), |
| 1614 | 3x |
script = "echarts-gl.min.js" |
| 1615 |
) |
|
| 1616 | ||
| 1617 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 1618 | ||
| 1619 | 3x |
e |
| 1620 |
} |
|
| 1621 | ||
| 1622 |
#' @rdname e_bar_3d |
|
| 1623 |
#' @export |
|
| 1624 |
e_bar_3d_ <- function(e, y, z, bind = NULL, coord_system = "cartesian3D", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1625 | 17x |
if (missing(e)) {
|
| 1626 | 1x |
stop("must pass e", call. = FALSE)
|
| 1627 |
} |
|
| 1628 | ||
| 1629 | 16x |
if (missing(y) || missing(z)) {
|
| 1630 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 1631 |
} |
|
| 1632 | ||
| 1633 | 15x |
if(is.null(e$x$mapping$x)){
|
| 1634 | ! |
stop("e$x$mapping$x is NULL")
|
| 1635 |
} |
|
| 1636 | ||
| 1637 |
# remove axis |
|
| 1638 | 15x |
e <- .rm_axis(e, rm_x, "x") |
| 1639 | 15x |
e <- .rm_axis(e, rm_y, "y") |
| 1640 | 15x |
e <- .set_axis_3D(e, "x", e$x$mapping$x, 0) |
| 1641 | 15x |
e <- .set_axis_3D(e, "y", y, 0) |
| 1642 | 15x |
e <- .set_axis_3D(e, "z", z, 0) |
| 1643 | ||
| 1644 | 15x |
for (i in seq_along(e$x$data)) {
|
| 1645 | 18x |
if (coord_system != "cartesian3D") {
|
| 1646 | ! |
data <- .build_data2(e$x$data[[i]], e$x$mapping$x, {{y}}, {{z}})
|
| 1647 | ||
| 1648 | ! |
if (!is.null(bind)) {
|
| 1649 | ! |
data <- .add_bind2(e, data, bind, i = i) |
| 1650 |
} |
|
| 1651 |
} else {
|
|
| 1652 | 18x |
data <- .build_cartesian3D(e, e$x$mapping$x, {{y}}, {{z}}, i = i)
|
| 1653 |
} |
|
| 1654 | ||
| 1655 | 18x |
if (!e$x$tl) {
|
| 1656 | ||
| 1657 |
# globe |
|
| 1658 | 12x |
if (coord_system == "cartesian3D") { # cartesian
|
| 1659 | ||
| 1660 | 12x |
if (!length(e$x$opts$zAxis3D)) {
|
| 1661 | ! |
e$x$opts$zAxis3D <- list(list(show = TRUE)) |
| 1662 |
} |
|
| 1663 | ||
| 1664 | 12x |
if (!length(e$x$opts$grid3D)) {
|
| 1665 | 12x |
e$x$opts$grid3D <- list(list(show = TRUE)) |
| 1666 |
} |
|
| 1667 |
} |
|
| 1668 | ||
| 1669 | 12x |
nm <- .name_it(e, NULL, name, i) |
| 1670 | ||
| 1671 | 12x |
if (!is.null(nm)) {
|
| 1672 | 1x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, nm) |
| 1673 |
} |
|
| 1674 | ||
| 1675 | 12x |
e.serie <- list( |
| 1676 | 12x |
name = nm, |
| 1677 | 12x |
type = "bar3D", |
| 1678 | 12x |
coordinateSystem = coord_system, |
| 1679 | 12x |
data = data, |
| 1680 |
... |
|
| 1681 |
) |
|
| 1682 | ||
| 1683 | 12x |
e$x$opts$series <- append(e$x$opts$series, list(e.serie)) |
| 1684 |
} else {
|
|
| 1685 | ||
| 1686 |
# globe |
|
| 1687 | 6x |
if (coord_system == "cartesian3D") { # cartesian
|
| 1688 | 6x |
if (!length(e$x$opts$zAxis3D)) {
|
| 1689 | 6x |
e$x$opts$baseOption$zAxis3D <- list(list(show = TRUE)) |
| 1690 |
} |
|
| 1691 | ||
| 1692 | 6x |
if (!length(e$x$opts$grid3D)) {
|
| 1693 | 6x |
e$x$opts$baseOption$grid3D <- list(list(show = TRUE)) |
| 1694 |
} |
|
| 1695 |
} |
|
| 1696 | ||
| 1697 | 6x |
e_serie <- list(data = data) |
| 1698 | ||
| 1699 | 6x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(e_serie)) |
| 1700 |
} |
|
| 1701 |
} |
|
| 1702 | 15x |
if (e$x$tl) {
|
| 1703 | 3x |
serie_opts <- list( |
| 1704 | 3x |
name = name, |
| 1705 | 3x |
type = "bar3D", |
| 1706 | 3x |
coordinateSystem = coord_system, |
| 1707 |
... |
|
| 1708 |
) |
|
| 1709 | ||
| 1710 | 3x |
if (!is.null(name)) {
|
| 1711 | ! |
e$x$opts$baseOption$legend$data <- append(e$x$opts$legend$data, name) |
| 1712 |
} |
|
| 1713 | 3x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 1714 |
} |
|
| 1715 | ||
| 1716 |
# add dependency |
|
| 1717 | 15x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 1718 | 15x |
dep <- htmltools::htmlDependency( |
| 1719 | 15x |
name = "echarts-gl", |
| 1720 | 15x |
version = "1.1.2", |
| 1721 | 15x |
src = c(file = path), |
| 1722 | 15x |
script = "echarts-gl.min.js" |
| 1723 |
) |
|
| 1724 | ||
| 1725 | 15x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 1726 | ||
| 1727 | 15x |
e |
| 1728 |
} |
|
| 1729 | ||
| 1730 |
#' @rdname e_surface |
|
| 1731 |
#' @export |
|
| 1732 |
e_surface_ <- function(e, y, z, bind = NULL, name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1733 | 6x |
if (missing(e)) {
|
| 1734 | 1x |
stop("must pass e", call. = FALSE)
|
| 1735 |
} |
|
| 1736 | ||
| 1737 | 5x |
if (missing(y) || missing(z)) {
|
| 1738 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 1739 |
} |
|
| 1740 | 1x |
if(e$x$tl)( |
| 1741 | 1x |
stop("timeline not supported")
|
| 1742 |
) |
|
| 1743 | ||
| 1744 |
# remove axis |
|
| 1745 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 1746 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 1747 | ||
| 1748 | 3x |
if (!length(e$x$opts$zAxis3D)) {
|
| 1749 | 3x |
e$x$opts$zAxis3D <- list(type = "value") |
| 1750 |
} |
|
| 1751 | ||
| 1752 | 3x |
if (!length(e$x$opts$yAxis3D)) {
|
| 1753 | 3x |
e$x$opts$yAxis3D <- list(type = "value") |
| 1754 |
} |
|
| 1755 | ||
| 1756 | 3x |
if (!length(e$x$opts$xAxis3D)) {
|
| 1757 | 3x |
e$x$opts$xAxis3D <- list(type = "value") |
| 1758 |
} |
|
| 1759 | ||
| 1760 | 3x |
if (!length(e$x$opts$grid3D)) {
|
| 1761 | 3x |
e$x$opts$grid3D <- list(show = TRUE) |
| 1762 |
} |
|
| 1763 | ||
| 1764 | 3x |
for (i in seq_along(e$x$data)) {
|
| 1765 | 3x |
row.names(e$x$data[[i]]) <- NULL |
| 1766 | ||
| 1767 | 3x |
data <- e$x$data[[i]] |> |
| 1768 | 3x |
dplyr::select(dplyr::all_of(c(e$x$mapping$x, y, z))) |
| 1769 | ||
| 1770 | 3x |
data <- unname(data) |
| 1771 | ||
| 1772 | 3x |
data <- apply(data, 1, as.list) |
| 1773 | ||
| 1774 | 3x |
e.serie <- list( |
| 1775 | 3x |
type = "surface", |
| 1776 | 3x |
data = data, |
| 1777 |
... |
|
| 1778 |
) |
|
| 1779 | ||
| 1780 | ! |
if (!is.null(name)) e.serie$name <- name |
| 1781 | ||
| 1782 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(e.serie)) |
| 1783 |
} |
|
| 1784 | ||
| 1785 |
# add dependency |
|
| 1786 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 1787 | 3x |
dep <- htmltools::htmlDependency( |
| 1788 | 3x |
name = "echarts-gl", |
| 1789 | 3x |
version = "1.1.2", |
| 1790 | 3x |
src = c(file = path), |
| 1791 | 3x |
script = "echarts-gl.min.js" |
| 1792 |
) |
|
| 1793 | ||
| 1794 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 1795 | ||
| 1796 | 3x |
e |
| 1797 |
} |
|
| 1798 | ||
| 1799 |
#' @rdname e_lines |
|
| 1800 |
#' @export |
|
| 1801 |
e_lines_ <- function( |
|
| 1802 |
e, |
|
| 1803 |
source_lon, |
|
| 1804 |
source_lat, |
|
| 1805 |
target_lon, |
|
| 1806 |
target_lat, |
|
| 1807 |
source_name = NULL, |
|
| 1808 |
target_name = NULL, |
|
| 1809 |
value = NULL, |
|
| 1810 |
coord_system = "geo", |
|
| 1811 |
name = NULL, |
|
| 1812 |
rm_x = TRUE, |
|
| 1813 |
rm_y = TRUE, |
|
| 1814 |
... |
|
| 1815 |
) {
|
|
| 1816 | 6x |
if (missing(e)) {
|
| 1817 | 1x |
stop("must pass e", call. = FALSE)
|
| 1818 |
} |
|
| 1819 | ||
| 1820 | 5x |
if (missing(source_lat) || missing(source_lon) || missing(target_lat) || missing(target_lon)) {
|
| 1821 | 1x |
stop("missing coordinates", call. = FALSE)
|
| 1822 |
} |
|
| 1823 | ||
| 1824 | 4x |
if (missing(source_name) || identical(source_name, "NULL")) {
|
| 1825 | 1x |
source_name <- NULL |
| 1826 |
} |
|
| 1827 | ||
| 1828 | 4x |
if (missing(target_name) || identical(target_name, "NULL")) {
|
| 1829 | 1x |
target_name <- NULL |
| 1830 |
} |
|
| 1831 | ||
| 1832 | 4x |
if (missing(value) || identical(value, "NULL")) {
|
| 1833 | 1x |
value <- NULL |
| 1834 |
} |
|
| 1835 | ||
| 1836 |
# remove axis |
|
| 1837 | 4x |
e <- .rm_axis(e, rm_x, "x") |
| 1838 | 4x |
e <- .rm_axis(e, rm_y, "y") |
| 1839 | ||
| 1840 | 4x |
for (i in seq_along(e$x$data)) {
|
| 1841 | 4x |
data <- .map_lines(e, source_lon, source_lat, target_lon, target_lat, source_name, target_name, value, i) |
| 1842 | ||
| 1843 | 4x |
e.serie <- list(data = data) |
| 1844 | ||
| 1845 | 4x |
e.opts <- list( |
| 1846 | 4x |
type = "lines", |
| 1847 | 4x |
coordinateSystem = coord_system, |
| 1848 |
... |
|
| 1849 |
) |
|
| 1850 | ||
| 1851 | 4x |
if (!e$x$tl) {
|
| 1852 | 4x |
e.opts$name <- .name_it(e, NULL, name, i) |
| 1853 | ||
| 1854 | 4x |
e_serie <- append(e.serie, e.opts) |
| 1855 | ||
| 1856 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(e_serie)) |
| 1857 |
} else {
|
|
| 1858 | ! |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(e.serie)) |
| 1859 |
} |
|
| 1860 |
} |
|
| 1861 | ||
| 1862 | 4x |
if (e$x$tl) {
|
| 1863 | ! |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(e.opts)) |
| 1864 |
} |
|
| 1865 | ||
| 1866 | 4x |
e |
| 1867 |
} |
|
| 1868 | ||
| 1869 |
#' @rdname e_scatter_3d |
|
| 1870 |
#' @export |
|
| 1871 |
e_scatter_3d_ <- function( |
|
| 1872 |
e, |
|
| 1873 |
y, |
|
| 1874 |
z, |
|
| 1875 |
color = NULL, |
|
| 1876 |
size = NULL, |
|
| 1877 |
bind = NULL, |
|
| 1878 |
coord_system = "cartesian3D", |
|
| 1879 |
name = NULL, |
|
| 1880 |
rm_x = TRUE, |
|
| 1881 |
rm_y = TRUE, |
|
| 1882 |
legend = FALSE, |
|
| 1883 |
... |
|
| 1884 |
) {
|
|
| 1885 | 8x |
if (missing(e)) {
|
| 1886 | 1x |
stop("must pass e", call. = FALSE)
|
| 1887 |
} |
|
| 1888 | ||
| 1889 | 7x |
if (missing(y) || missing(z)) {
|
| 1890 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 1891 |
} |
|
| 1892 | ||
| 1893 | 6x |
if(is.null(e$x$mapping$x)){
|
| 1894 | ! |
stop("e$x$mapping$x is NULL")
|
| 1895 |
} |
|
| 1896 | ||
| 1897 |
# remove axis |
|
| 1898 | 6x |
e <- .rm_axis(e, rm_x, "x") |
| 1899 | 6x |
e <- .rm_axis(e, rm_y, "y") |
| 1900 | ||
| 1901 | 6x |
for (i in seq_along(e$x$data)) {
|
| 1902 | ||
| 1903 |
# globe |
|
| 1904 | 8x |
if (coord_system != "cartesian3D") {
|
| 1905 | ! |
data <- .build_data2(e$x$data[[i]], e$x$mapping$x, y, z) |
| 1906 | ||
| 1907 | ! |
if (!is.null(bind)) {
|
| 1908 | ! |
data <- .add_bind2(e, data, bind, i = i) |
| 1909 |
} |
|
| 1910 |
} else { # cartesian
|
|
| 1911 | ||
| 1912 | 8x |
if (!e$x$tl) {
|
| 1913 | 5x |
if (!length(e$x$opts$zAxis3D)) {
|
| 1914 | 5x |
e$x$opts$zAxis3D <- list(list(show = TRUE)) |
| 1915 |
} |
|
| 1916 | ||
| 1917 | 5x |
if (!length(e$x$opts$grid3D)) {
|
| 1918 | 5x |
e$x$opts$grid3D <- list(list(show = TRUE)) |
| 1919 |
} |
|
| 1920 |
} else {
|
|
| 1921 | 3x |
if (!length(e$x$opts$zAxis3D)) {
|
| 1922 | 3x |
e$x$opts$baseOption$zAxis3D <- list(list(show = TRUE)) |
| 1923 |
} |
|
| 1924 | ||
| 1925 | 3x |
if (!length(e$x$opts$grid3D)) {
|
| 1926 | 3x |
e$x$opts$baseOption$grid3D <- list(list(show = TRUE)) |
| 1927 |
} |
|
| 1928 |
} |
|
| 1929 | ||
| 1930 | 8x |
e <- .set_axis_3D(e, "x", e$x$mapping$x, 0) |
| 1931 | 8x |
e <- .set_axis_3D(e, "y", y, 0) |
| 1932 | 8x |
e <- .set_axis_3D(e, "z", z, 0) |
| 1933 | ||
| 1934 | 8x |
if (is.null(color)) {
|
| 1935 | 8x |
data <- .build_data2(e$x$data[[i]], e$x$mapping$x, y, z) |
| 1936 | ! |
} else if (!is.null(color) && is.null(size)) {
|
| 1937 | ! |
data <- .build_data2(e$x$data[[i]], e$x$mapping$x, y, z, color) |
| 1938 | ! |
} else if (!is.null(color) && !is.null(size)) {
|
| 1939 | ! |
data <- .build_data2(e$x$data[[i]], e$x$mapping$x, y, z, color, size) |
| 1940 |
} |
|
| 1941 | ||
| 1942 | 8x |
if (!is.null(bind)) {
|
| 1943 | ! |
data <- .add_bind2(e, data, bind, i = i) |
| 1944 |
} |
|
| 1945 |
} |
|
| 1946 | ||
| 1947 | 8x |
e_data <- list(data = data) |
| 1948 | ||
| 1949 | 8x |
e_serie <- list( |
| 1950 | 8x |
name = name, |
| 1951 | 8x |
type = "scatter3D", |
| 1952 | 8x |
coordinateSystem = coord_system, |
| 1953 |
... |
|
| 1954 |
) |
|
| 1955 | ||
| 1956 | 8x |
if (!e$x$tl) {
|
| 1957 | 5x |
nm <- .name_it(e, NULL, name, i) |
| 1958 | 5x |
e_serie$name <- nm |
| 1959 | ||
| 1960 | 5x |
e_serie <- append(e_serie, e_data) |
| 1961 | ||
| 1962 | 5x |
e$x$opts$series <- append(e$x$opts$series, list(e_serie)) |
| 1963 |
} else {
|
|
| 1964 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(e_data)) |
| 1965 |
} |
|
| 1966 |
} |
|
| 1967 | ||
| 1968 | 6x |
if (e$x$tl) {
|
| 1969 | 1x |
if (isTRUE(legend)) {
|
| 1970 | ! |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(nm)) |
| 1971 |
} |
|
| 1972 | ||
| 1973 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(e_serie)) |
| 1974 |
} |
|
| 1975 | ||
| 1976 |
# add dependency |
|
| 1977 | 6x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 1978 | 6x |
dep <- htmltools::htmlDependency( |
| 1979 | 6x |
name = "echarts-gl", |
| 1980 | 6x |
version = "1.1.2", |
| 1981 | 6x |
src = c(file = path), |
| 1982 | 6x |
script = "echarts-gl.min.js" |
| 1983 |
) |
|
| 1984 | ||
| 1985 | 6x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 1986 | ||
| 1987 | 6x |
e |
| 1988 |
} |
|
| 1989 | ||
| 1990 |
#' @rdname e_flow_gl |
|
| 1991 |
#' @export |
|
| 1992 |
e_flow_gl_ <- function(e, y, sx, sy, color = NULL, name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1993 | 6x |
if (missing(e)) {
|
| 1994 | 1x |
stop("must pass e", call. = FALSE)
|
| 1995 |
} |
|
| 1996 | 1x |
if(e$x$tl)( |
| 1997 | 1x |
stop("timeline not supported")
|
| 1998 |
) |
|
| 1999 | ||
| 2000 | 4x |
e$x$mainOpts$renderer <- "webgl" |
| 2001 | ||
| 2002 | 4x |
if (missing(y) || missing(sx) || missing(sy)) {
|
| 2003 | 1x |
stop("must pass y, sx, and sy", call. = FALSE)
|
| 2004 |
} |
|
| 2005 | ||
| 2006 | 3x |
e$x$opts$xAxis <- list(type = "value") |
| 2007 | 3x |
e$x$opts$yAxis <- list(type = "value") |
| 2008 | ||
| 2009 | 3x |
if (is.null(color)) {
|
| 2010 | 2x |
data <- .build_data(e, e$x$mapping$x, y, sx, sy) |
| 2011 |
} else {
|
|
| 2012 | 1x |
data <- .build_data(e, e$x$mapping$x, y, sx, sy, color) |
| 2013 |
} |
|
| 2014 | ||
| 2015 | 3x |
serie <- list( |
| 2016 | 3x |
type = "flowGL", |
| 2017 | 3x |
data = data, |
| 2018 |
... |
|
| 2019 |
) |
|
| 2020 | ||
| 2021 | 3x |
if (!is.null(name)) {
|
| 2022 | ! |
serie$name <- name |
| 2023 |
} |
|
| 2024 | ||
| 2025 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 2026 | ||
| 2027 |
# add dependency |
|
| 2028 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 2029 | 3x |
dep <- htmltools::htmlDependency( |
| 2030 | 3x |
name = "echarts-gl", |
| 2031 | 3x |
version = "1.1.2", |
| 2032 | 3x |
src = c(file = path), |
| 2033 | 3x |
script = "echarts-gl.min.js" |
| 2034 |
) |
|
| 2035 | ||
| 2036 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 2037 | ||
| 2038 | 3x |
e |
| 2039 |
} |
|
| 2040 | ||
| 2041 |
#' @rdname e_scatter_gl |
|
| 2042 |
#' @export |
|
| 2043 |
e_scatter_gl_ <- function(e, y, z, name = NULL, coord_system = "geo", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2044 | 7x |
if (missing(e)) {
|
| 2045 | 1x |
stop("must pass e", call. = FALSE)
|
| 2046 |
} |
|
| 2047 | ||
| 2048 | 6x |
e$x$mainOpts$renderer <- "webgl" |
| 2049 | ||
| 2050 | 6x |
if (missing(y) || missing(z)) {
|
| 2051 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 2052 |
} |
|
| 2053 | ||
| 2054 | 5x |
for (i in seq_along(e$x$data)) {
|
| 2055 | ||
| 2056 |
# remove axis |
|
| 2057 | 9x |
e <- .rm_axis(e, rm_x, "x") |
| 2058 | 9x |
e <- .rm_axis(e, rm_y, "y") |
| 2059 | ||
| 2060 | 9x |
data <- .build_data2(e$x$data[[i]], e$x$mapping$x, y, z) |
| 2061 | ||
| 2062 | 9x |
serie <- list(data = data) |
| 2063 | ||
| 2064 | 9x |
serie_opts <- list( |
| 2065 | 9x |
name = name, |
| 2066 | 9x |
type = "scatterGL", |
| 2067 | 9x |
coordinateSystem = coord_system, |
| 2068 |
... |
|
| 2069 |
) |
|
| 2070 | ||
| 2071 |
# globe |
|
| 2072 | 9x |
if (coord_system == "cartesian3D") {
|
| 2073 | 1x |
if (!e$x$tl) {
|
| 2074 | 1x |
if (!length(e$x$opts$zAxis3D)) {
|
| 2075 | 1x |
e$x$opts$zAxis3D <- list(list(show = TRUE)) |
| 2076 |
} |
|
| 2077 | ||
| 2078 | 1x |
if (!length(e$x$opts$grid3D)) {
|
| 2079 | 1x |
e$x$opts$grid3D <- list(list(show = TRUE)) |
| 2080 |
} |
|
| 2081 |
} else {
|
|
| 2082 | ! |
if (!length(e$x$opts$zAxis3D)) {
|
| 2083 | ! |
e$x$opts$baseOption$zAxis3D <- list(list(show = TRUE)) |
| 2084 |
} |
|
| 2085 | ||
| 2086 | ! |
if (!length(e$x$opts$grid3D)) {
|
| 2087 | ! |
e$x$opts$baseOption$grid3D <- list(list(show = TRUE)) |
| 2088 |
} |
|
| 2089 |
} |
|
| 2090 | 1x |
e <- .set_axis_3D(e, "x", e$x$mapping$x, 0) |
| 2091 | 1x |
e <- .set_axis_3D(e, "y", y, 0) |
| 2092 | 1x |
e <- .set_axis_3D(e, "z", z, 0) |
| 2093 |
} |
|
| 2094 | ||
| 2095 | 9x |
if (!e$x$tl) {
|
| 2096 | 4x |
serie_opts$name <- .name_it(e, z, name, i) |
| 2097 | ||
| 2098 | 4x |
serie <- append(serie, serie_opts) |
| 2099 | ||
| 2100 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 2101 |
} else {
|
|
| 2102 | 5x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie)) |
| 2103 |
} |
|
| 2104 |
} |
|
| 2105 | ||
| 2106 | 5x |
if (e$x$tl) {
|
| 2107 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 2108 |
} |
|
| 2109 | ||
| 2110 |
# add dependency |
|
| 2111 | 5x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 2112 | 5x |
dep <- htmltools::htmlDependency( |
| 2113 | 5x |
name = "echarts-gl", |
| 2114 | 5x |
version = "1.1.2", |
| 2115 | 5x |
src = c(file = path), |
| 2116 | 5x |
script = "echarts-gl.min.js" |
| 2117 |
) |
|
| 2118 | ||
| 2119 | 5x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 2120 | ||
| 2121 | 5x |
e |
| 2122 |
} |
|
| 2123 | ||
| 2124 |
#' @rdname e_pictorial |
|
| 2125 |
#' @export |
|
| 2126 |
e_pictorial_ <- function(e, serie, symbol, bind = NULL, name = NULL, legend = TRUE, y_index = 0, x_index = 0, ...) {
|
|
| 2127 | 7x |
if (missing(e)) {
|
| 2128 | 1x |
stop("must pass e", call. = FALSE)
|
| 2129 |
} |
|
| 2130 | ||
| 2131 | 6x |
if (missing(serie) || missing(symbol)) {
|
| 2132 | 1x |
stop("must pass serie and symbol", call. = FALSE)
|
| 2133 |
} |
|
| 2134 | ||
| 2135 | 5x |
if (y_index != 0) {
|
| 2136 | 1x |
e <- .set_y_axis(e, serie, y_index) |
| 2137 |
} |
|
| 2138 | ||
| 2139 | 5x |
if (x_index != 0) {
|
| 2140 | 1x |
e <- .set_x_axis(e, x_index) |
| 2141 |
} |
|
| 2142 | ||
| 2143 | 5x |
for (i in seq_along(e$x$data)) {
|
| 2144 | 14x |
if (is.null(name)) { # defaults to column name
|
| 2145 | 4x |
name <- serie |
| 2146 |
} |
|
| 2147 | ||
| 2148 |
# build JSON data |
|
| 2149 | 14x |
vector <- .build_data2(e$x$data[[i]], e$x$mapping$x, serie) |
| 2150 | ||
| 2151 | 14x |
if (!is.null(bind)) {
|
| 2152 | ! |
vector <- .add_bind2(e, vector, bind, i = i) |
| 2153 |
} |
|
| 2154 | ||
| 2155 | 14x |
if (symbol %in% colnames(e$x$data[[1]])) {
|
| 2156 | 1x |
vector <- .add_bind2(e, vector, symbol, col = "symbol", i = i) |
| 2157 |
} |
|
| 2158 | ||
| 2159 | 14x |
serie_data <- list(data = vector) |
| 2160 | ||
| 2161 | 14x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 2162 | ! |
grp_val <- names(e$x$data)[i] |
| 2163 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 2164 | ! |
serie_data$datasetId <- id |
| 2165 | ! |
serie_data$data <- NULL |
| 2166 | ! |
serie_data$encode <- list(x = e$x$mapping$x, y = serie) |
| 2167 |
} |
|
| 2168 | ||
| 2169 | 14x |
serie_opts <- list( |
| 2170 | 14x |
name = name, |
| 2171 | 14x |
type = "pictorialBar", |
| 2172 | 14x |
yAxisIndex = y_index, |
| 2173 | 14x |
xAxisIndex = x_index, |
| 2174 |
... |
|
| 2175 |
) |
|
| 2176 | ||
| 2177 | 14x |
if (!e$x$tl) {
|
| 2178 | 4x |
serie_opts <- append(serie_data, serie_opts) |
| 2179 | ||
| 2180 | 4x |
if (!symbol %in% colnames(e$x$data[[i]])) {
|
| 2181 | 3x |
serie_opts$symbol <- symbol |
| 2182 |
} |
|
| 2183 | ||
| 2184 | 4x |
if (isTRUE(legend)) {
|
| 2185 | 4x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(name)) |
| 2186 |
} |
|
| 2187 | ||
| 2188 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(serie_opts)) |
| 2189 |
} else {
|
|
| 2190 | 10x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie_data)) |
| 2191 |
} |
|
| 2192 |
} |
|
| 2193 | ||
| 2194 | 5x |
if (isTRUE(e$x$tl)) {
|
| 2195 | 1x |
if (isTRUE(legend)) {
|
| 2196 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 2197 |
} |
|
| 2198 | ||
| 2199 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 2200 |
} |
|
| 2201 | ||
| 2202 | ||
| 2203 | 5x |
e |
| 2204 |
} |
|
| 2205 | ||
| 2206 |
#' @rdname histogram |
|
| 2207 |
#' @export |
|
| 2208 |
e_histogram_ <- function( |
|
| 2209 |
e, |
|
| 2210 |
serie, |
|
| 2211 |
breaks = "Sturges", |
|
| 2212 |
name = "histogram", |
|
| 2213 |
legend = TRUE, |
|
| 2214 |
bar_width = "90%", |
|
| 2215 |
x_index = 0, |
|
| 2216 |
y_index = 0, |
|
| 2217 |
... |
|
| 2218 |
) {
|
|
| 2219 | 8x |
if (missing(e)) {
|
| 2220 | 1x |
stop("must pass e", call. = FALSE)
|
| 2221 |
} |
|
| 2222 | ||
| 2223 | 7x |
if (missing(serie)) {
|
| 2224 | 1x |
stop("must pass serie", call. = FALSE)
|
| 2225 |
} |
|
| 2226 | ||
| 2227 | 6x |
for (i in seq_along(e$x$data)) {
|
| 2228 | 7x |
data <- .get_data(e, serie, i) |
| 2229 | 7x |
histogram <- hist(data, plot = FALSE, breaks) |
| 2230 | ||
| 2231 | 7x |
hist <- data.frame( |
| 2232 | 7x |
histogram$mids, |
| 2233 | 7x |
histogram$counts |
| 2234 |
) |
|
| 2235 | ||
| 2236 | 7x |
hist <- apply(unname(hist), 1, as.list) |
| 2237 | ||
| 2238 | 7x |
if (y_index != 0) {
|
| 2239 | 1x |
e <- .set_y_axis(e, serie, y_index, i) |
| 2240 |
} |
|
| 2241 | ||
| 2242 | 7x |
if (x_index != 0) {
|
| 2243 | ||
| 2244 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 2245 |
} |
|
| 2246 | ||
| 2247 | 7x |
serie_data <- list(data = hist) |
| 2248 | ||
| 2249 | 7x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 2250 | ! |
grp_val <- names(e$x$data)[i] |
| 2251 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 2252 | ! |
serie_data$datasetId <- id |
| 2253 | ! |
serie_data$data <- NULL |
| 2254 | ! |
serie_data$encode <- list(x = e$x$mapping$x, y = serie) |
| 2255 |
} |
|
| 2256 | ||
| 2257 | 7x |
serie_opts <- list( |
| 2258 | 7x |
name = name, |
| 2259 | 7x |
type = "bar", |
| 2260 | 7x |
barWidth = bar_width, |
| 2261 | 7x |
yAxisIndex = y_index, |
| 2262 | 7x |
xAxisIndex = x_index, |
| 2263 | 7x |
stack = "stackedHistogram", |
| 2264 |
... |
|
| 2265 |
) |
|
| 2266 | ||
| 2267 | 7x |
if (!e$x$tl) {
|
| 2268 | 5x |
nm <- .name_it(e, NULL, name, i) |
| 2269 | ||
| 2270 | 5x |
serie_opts$name <- nm |
| 2271 | ||
| 2272 | 5x |
if (!length(e$x$opts$xAxis)) {
|
| 2273 | 3x |
e$x$opts$xAxis <- list( |
| 2274 | 3x |
list( |
| 2275 | 3x |
type = "value", |
| 2276 | 3x |
scale = TRUE |
| 2277 |
) |
|
| 2278 |
) |
|
| 2279 |
} |
|
| 2280 | ||
| 2281 | 5x |
e.serie <- append(serie_data, serie_opts) |
| 2282 | ||
| 2283 | 5x |
if (isTRUE(legend)) {
|
| 2284 | 5x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 2285 |
} |
|
| 2286 | ||
| 2287 | 5x |
e$x$opts$series <- append(e$x$opts$series, list(e.serie)) |
| 2288 |
} else {
|
|
| 2289 | 2x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie_data)) |
| 2290 |
} |
|
| 2291 |
} |
|
| 2292 | ||
| 2293 | 6x |
if (isTRUE(e$x$tl)) {
|
| 2294 | 1x |
if (!length(e$x$opts$baseOption$xAxis)) {
|
| 2295 | 1x |
e$x$opts$baseOption$xAxis <- list( |
| 2296 | 1x |
list( |
| 2297 | 1x |
type = "value", |
| 2298 | 1x |
scale = TRUE |
| 2299 |
) |
|
| 2300 |
) |
|
| 2301 |
} |
|
| 2302 | ||
| 2303 | 1x |
if (isTRUE(legend)) {
|
| 2304 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 2305 |
} |
|
| 2306 | ||
| 2307 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 2308 |
} |
|
| 2309 | ||
| 2310 | 6x |
e |
| 2311 |
} |
|
| 2312 | ||
| 2313 |
#' @rdname histogram |
|
| 2314 |
#' @export |
|
| 2315 |
e_density_ <- function( |
|
| 2316 |
e, |
|
| 2317 |
serie, |
|
| 2318 |
breaks = "Sturges", |
|
| 2319 |
name = NULL, |
|
| 2320 |
legend = TRUE, |
|
| 2321 |
x_index = 0, |
|
| 2322 |
y_index = 0, |
|
| 2323 |
smooth = TRUE, |
|
| 2324 |
... |
|
| 2325 |
) {
|
|
| 2326 | 7x |
if (missing(e)) {
|
| 2327 | 1x |
stop("must pass e", call. = FALSE)
|
| 2328 |
} |
|
| 2329 | ||
| 2330 | 6x |
if (missing(serie)) {
|
| 2331 | 1x |
stop("must pass serie", call. = FALSE)
|
| 2332 |
} |
|
| 2333 | ||
| 2334 | 5x |
for (i in seq_along(e$x$data)) {
|
| 2335 | 6x |
data <- .get_data(e, serie, i = i) |
| 2336 | 6x |
histogram <- hist(data, plot = FALSE, breaks) |
| 2337 | ||
| 2338 | 6x |
hist <- data.frame( |
| 2339 | 6x |
histogram$mids, |
| 2340 | 6x |
histogram$density |
| 2341 |
) |
|
| 2342 | ||
| 2343 | 6x |
hist <- apply(unname(hist), 1, as.list) |
| 2344 | ||
| 2345 | 6x |
if (y_index != 0) {
|
| 2346 | 4x |
e <- .set_y_axis(e, serie, y_index) |
| 2347 |
} |
|
| 2348 | ||
| 2349 | 6x |
if (x_index != 0) {
|
| 2350 | 1x |
e <- .set_x_axis(e, x_index) |
| 2351 |
} |
|
| 2352 | ||
| 2353 | 6x |
serie_data <- list(data = hist) |
| 2354 | ||
| 2355 | 6x |
if (!is.null(e$x$settings$crosstalk_group) && !isTRUE(e$x$tl)) {
|
| 2356 | ! |
grp_val <- names(e$x$data)[i] |
| 2357 | ! |
id <- if (!is.null(e$x$crosstalk_grpvar)) paste0("Xtalk_", grp_val) else "Xtalk"
|
| 2358 | ! |
serie_data$datasetId <- id |
| 2359 | ! |
serie_data$data <- NULL |
| 2360 | ! |
serie_data$encode <- list(x = e$x$mapping$x, y = serie) |
| 2361 |
} |
|
| 2362 | ||
| 2363 | 6x |
serie_opts <- list( |
| 2364 | 6x |
name = name, |
| 2365 | 6x |
type = "line", |
| 2366 | 6x |
yAxisIndex = y_index, |
| 2367 | 6x |
xAxisIndex = x_index, |
| 2368 | 6x |
smooth = smooth, |
| 2369 |
... |
|
| 2370 |
) |
|
| 2371 | ||
| 2372 | 6x |
if (!"areaStyle" %in% names(serie_opts)) {
|
| 2373 | 3x |
serie_opts$areaStyle <- list() |
| 2374 |
} |
|
| 2375 | ||
| 2376 | 6x |
if (!e$x$tl) {
|
| 2377 | 4x |
nm <- .name_it(e, serie, name, i) |
| 2378 | ||
| 2379 | 4x |
serie_opts$name <- nm |
| 2380 | ||
| 2381 | 4x |
if (!length(e$x$opts$xAxis)) {
|
| 2382 | 1x |
e$x$opts$xAxis <- list( |
| 2383 | 1x |
list( |
| 2384 | 1x |
type = "value", |
| 2385 | 1x |
scale = TRUE |
| 2386 |
) |
|
| 2387 |
) |
|
| 2388 |
} |
|
| 2389 | ||
| 2390 | 4x |
series <- append(serie_opts, serie_data) |
| 2391 | ||
| 2392 | 4x |
if (isTRUE(legend)) {
|
| 2393 | 4x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 2394 |
} |
|
| 2395 | ||
| 2396 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(series)) |
| 2397 |
} else {
|
|
| 2398 | 2x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie_data)) |
| 2399 |
} |
|
| 2400 |
} |
|
| 2401 | ||
| 2402 | 5x |
if (isTRUE(e$x$tl)) {
|
| 2403 | 1x |
if (!length(e$x$opts$baseOption$xAxis)) {
|
| 2404 | ! |
e$x$opts$baseOption$xAxis <- list( |
| 2405 | ! |
list( |
| 2406 | ! |
type = "value", |
| 2407 | ! |
scale = TRUE |
| 2408 |
) |
|
| 2409 |
) |
|
| 2410 |
} |
|
| 2411 | ||
| 2412 | 1x |
if (isTRUE(legend)) {
|
| 2413 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 2414 |
} |
|
| 2415 | ||
| 2416 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(serie_opts)) |
| 2417 |
} |
|
| 2418 | ||
| 2419 | 5x |
e |
| 2420 |
} |
|
| 2421 | ||
| 2422 |
#' @rdname band |
|
| 2423 |
#' @export |
|
| 2424 |
e_band_ <- function( |
|
| 2425 |
e, |
|
| 2426 |
min, |
|
| 2427 |
max, |
|
| 2428 |
stack = "confidence-band", |
|
| 2429 |
symbol = c("none", "none"),
|
|
| 2430 |
areaStyle = list(list(color = "rgba(0,0,0,0)"), list()), |
|
| 2431 |
legend = list(FALSE, FALSE), |
|
| 2432 |
... |
|
| 2433 |
) {
|
|
| 2434 | 6x |
if (missing(e)) {
|
| 2435 | 1x |
stop("must pass e", call. = FALSE)
|
| 2436 |
} |
|
| 2437 | 1x |
if(e$x$tl)( |
| 2438 | 1x |
stop("timeline not supported")
|
| 2439 |
) |
|
| 2440 | ||
| 2441 | 4x |
if (missing(min) || missing(max)) {
|
| 2442 | 1x |
stop("must pass min and max", call. = FALSE)
|
| 2443 |
} |
|
| 2444 | 3x |
args <- list(...) |
| 2445 | ||
| 2446 | 3x |
args$lineStyle <- list( |
| 2447 | 3x |
list(normal = list(opacity = 0)), |
| 2448 | 3x |
list(normal = list(opacity = 0)) |
| 2449 |
) |
|
| 2450 | 3x |
args$symbol <- symbol |
| 2451 | 3x |
args$areaStyle <- areaStyle |
| 2452 | 3x |
args$legend <- legend |
| 2453 | ||
| 2454 | 3x |
spl <- function(x, index) {
|
| 2455 | 24x |
x[[index]] |
| 2456 |
} |
|
| 2457 | ||
| 2458 | 3x |
min_opts <- purrr::map(args, spl, 1) |
| 2459 | 3x |
max_opts <- purrr::map(args, spl, 2) |
| 2460 | ||
| 2461 | 3x |
for (i in seq_along(e$x$data)) {
|
| 2462 | ||
| 2463 |
# min |
|
| 2464 | 3x |
min_opts_index <- min_opts |
| 2465 | ||
| 2466 | 3x |
min_opts_index$e <- e |
| 2467 | 3x |
min_opts_index$stack <- stack |
| 2468 | 3x |
min_opts_index$serie <- min |
| 2469 | ||
| 2470 | 3x |
e <- do.call(e_line_, min_opts_index) |
| 2471 | ||
| 2472 |
# max |
|
| 2473 | 3x |
e$x$data[[i]][, max] <- e$x$data[[i]][, max] - e$x$data[[i]][, min] |
| 2474 | ||
| 2475 | 3x |
max_opts_index <- max_opts |
| 2476 | 3x |
max_opts_index$e <- e |
| 2477 | 3x |
max_opts_index$stack <- stack |
| 2478 | 3x |
max_opts_index$serie <- max |
| 2479 | ||
| 2480 | 3x |
e <- do.call(e_line_, max_opts_index) |
| 2481 |
} |
|
| 2482 | ||
| 2483 | 3x |
e |> |
| 2484 | 3x |
e_x_axis(type = "category") |
| 2485 |
} |
|
| 2486 | ||
| 2487 |
#' @rdname band2 |
|
| 2488 |
#' @export |
|
| 2489 |
e_band2_ <- function( |
|
| 2490 |
e, |
|
| 2491 |
lower, |
|
| 2492 |
upper, |
|
| 2493 |
name = NULL, |
|
| 2494 |
legend = TRUE, |
|
| 2495 |
y_index = 0, |
|
| 2496 |
x_index = 0, |
|
| 2497 |
coord_system = "cartesian2d", |
|
| 2498 |
itemStyle = list(borderWidth = 0.5), |
|
| 2499 |
... |
|
| 2500 |
) {
|
|
| 2501 | 8x |
if (missing(e)) {
|
| 2502 | 1x |
stop("must pass e", call. = FALSE)
|
| 2503 |
} |
|
| 2504 | 7x |
if (missing(lower) || missing(upper)) {
|
| 2505 | 1x |
stop("must pass lower and upper", call. = FALSE)
|
| 2506 |
} |
|
| 2507 | 6x |
if (coord_system != "cartesian2d") {
|
| 2508 | 1x |
stop("only cartesian2d supported", call. = FALSE)
|
| 2509 |
} |
|
| 2510 | 5x |
if(is.null(e$x$mapping$x)){
|
| 2511 | ! |
stop("e$x$mapping$x is NULL")
|
| 2512 |
} |
|
| 2513 | ||
| 2514 | 5x |
args <- list(...) |
| 2515 | ||
| 2516 | 5x |
for (i in seq_along(e$x$data)) {
|
| 2517 | 6x |
vector <- .build_data2( |
| 2518 | 6x |
e$x$data[[i]], |
| 2519 | 6x |
e$x$mapping$x, |
| 2520 | 6x |
lower, |
| 2521 | 6x |
upper |
| 2522 |
) |
|
| 2523 | 6x |
e_serie <- list(data = vector) |
| 2524 | 6x |
if (y_index != 0) {
|
| 2525 | 1x |
e <- .set_y_axis(e, upper, y_index, i) |
| 2526 |
} |
|
| 2527 | 6x |
if (x_index != 0) {
|
| 2528 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 2529 |
} |
|
| 2530 | ||
| 2531 | 6x |
nm <- .name_it(e, paste0(lower, ".", upper), name, i) |
| 2532 | ||
| 2533 | 6x |
if (!e$x$tl) {
|
| 2534 | 4x |
opts <- list( |
| 2535 | 4x |
name = nm, |
| 2536 | 4x |
type = "custom", |
| 2537 | 4x |
yAxisIndex = y_index, |
| 2538 | 4x |
xAxisIndex = x_index, |
| 2539 | 4x |
coordinateSystem = coord_system, |
| 2540 | 4x |
itemStyle = itemStyle, |
| 2541 | 4x |
renderItem = htmlwidgets::JS("renderBand"),
|
| 2542 | 4x |
encode = list(x = 0, y = list(1, 2)), |
| 2543 |
... |
|
| 2544 |
) |
|
| 2545 | ||
| 2546 | 4x |
e_serie <- append(opts, e_serie) # data after renderItem, data used for Y-sizing only |
| 2547 | ||
| 2548 | 4x |
if (isTRUE(legend)) {
|
| 2549 | 4x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 2550 |
} |
|
| 2551 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(e_serie)) |
| 2552 |
} |
|
| 2553 |
else {
|
|
| 2554 | 2x |
if (isTRUE(legend)) {
|
| 2555 | 2x |
e$x$opts$legend$data <- append( |
| 2556 | 2x |
e$x$opts$legend$data, |
| 2557 | 2x |
list(nm) |
| 2558 |
) |
|
| 2559 |
} |
|
| 2560 | 2x |
e$x$opts$options[[i]]$series <- append( |
| 2561 | 2x |
e$x$opts$options[[i]]$series, |
| 2562 | 2x |
list(e_serie) |
| 2563 |
) |
|
| 2564 |
} |
|
| 2565 |
} |
|
| 2566 | 5x |
if (isTRUE(e$x$tl)) {
|
| 2567 | 1x |
series_opts <- list( |
| 2568 | 1x |
name = nm, |
| 2569 | 1x |
type = "custom", |
| 2570 | 1x |
yAxisIndex = y_index, |
| 2571 | 1x |
xAxisIndex = x_index, |
| 2572 | 1x |
coordinateSystem = coord_system, |
| 2573 | 1x |
itemStyle = itemStyle, |
| 2574 | 1x |
renderItem = htmlwidgets::JS("renderBand"),
|
| 2575 | 1x |
encode = list(x = 0, y = list(1, 2)), |
| 2576 |
... |
|
| 2577 |
) |
|
| 2578 | ||
| 2579 | 1x |
if (isTRUE(legend)) {
|
| 2580 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(nm)) |
| 2581 |
} |
|
| 2582 | 1x |
e$x$opts$baseOption$series <- append( |
| 2583 | 1x |
e$x$opts$baseOption$series, |
| 2584 | 1x |
list(series_opts) |
| 2585 |
) |
|
| 2586 |
} |
|
| 2587 | 5x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/custom", package = "echarts4r")
|
| 2588 | 5x |
dep <- htmltools::htmlDependency(name = "echarts-renderers", version = "1.0.2", src = c(file = path), script = "renderers.js") |
| 2589 | ||
| 2590 | 5x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 2591 | 5x |
e |
| 2592 |
} |
|
| 2593 | ||
| 2594 |
#' @rdname errorbar |
|
| 2595 |
#' @export |
|
| 2596 |
e_error_bar_ <- function( |
|
| 2597 |
e, |
|
| 2598 |
lower, |
|
| 2599 |
upper, |
|
| 2600 |
name = NULL, |
|
| 2601 |
legend = FALSE, |
|
| 2602 |
y_index = 0, |
|
| 2603 |
x_index = 0, |
|
| 2604 |
coord_system = "cartesian2d", |
|
| 2605 |
itemStyle = list(borderWidth = 1.5), |
|
| 2606 |
renderer = "renderErrorBar2", |
|
| 2607 |
... |
|
| 2608 |
) {
|
|
| 2609 | 7x |
if (missing(e)) {
|
| 2610 | 1x |
stop("must pass e", call. = FALSE)
|
| 2611 |
} |
|
| 2612 | 6x |
if (missing(lower) || missing(upper)) {
|
| 2613 | 1x |
stop("must pass lower and upper", call. = FALSE)
|
| 2614 |
} |
|
| 2615 | ||
| 2616 | 5x |
args <- list(...) |
| 2617 | ||
| 2618 | 5x |
ser <- if (e$x$tl) e$x$opts$baseOption$series else e$x$opts$series |
| 2619 |
# look for a name for timeline only |
|
| 2620 | 5x |
if (is.null(name) & e$x$tl) {
|
| 2621 | 1x |
name <- unlist(lapply(ser, function(x) {
|
| 2622 | 1x |
return(x$name) |
| 2623 | 1x |
}))[1] |
| 2624 |
} |
|
| 2625 | ||
| 2626 |
# look for barGap(s), barCategoryGap(s) |
|
| 2627 | 5x |
allBarGaps <- lapply(ser, function(x) {
|
| 2628 | 5x |
x$barGap |
| 2629 |
}) |
|
| 2630 | 5x |
allBarCgGaps <- lapply(ser, function(x) {
|
| 2631 | 5x |
x$barCategoryGap |
| 2632 |
}) |
|
| 2633 | 5x |
lbg <- utils::tail(unlist(allBarGaps), 1) |
| 2634 | 5x |
lbg <- if (is.null(lbg)) "" else lbg |
| 2635 | 5x |
lcg <- utils::tail(unlist(allBarCgGaps), 1) |
| 2636 | 5x |
lcg <- if (is.null(lcg)) "" else lcg |
| 2637 | 5x |
tmp <- NULL |
| 2638 | 5x |
if (!is.null(name)) {
|
| 2639 | 1x |
tmp <- unlist(lapply(ser, function(x) {
|
| 2640 | ! |
if (length(grep(name, x)) > 0) x$type else NULL |
| 2641 | 1x |
}))[1] |
| 2642 |
} |
|
| 2643 | 5x |
if (!is.null(tmp)) { # attached by name, count same types
|
| 2644 | 1x |
info <- length(unlist(lapply(ser, function(x) grep(tmp, x)))) |
| 2645 |
} else { # no name - choose bar or line but not both
|
|
| 2646 | 4x |
info <- length(unlist(lapply(ser, function(x) grep("bar", x))))
|
| 2647 | ! |
if (info == 0) info <- length(unlist(lapply(ser, function(x) grep("line", x))))
|
| 2648 | ! |
if (info == 0) info <- length(unlist(lapply(ser, function(x) grep("scatter", x))))
|
| 2649 |
} |
|
| 2650 | ||
| 2651 | 5x |
if (info == 0) {
|
| 2652 | ! |
return(e) |
| 2653 |
} # no bars/lines/scatter, nothing to attach to |
|
| 2654 | ||
| 2655 |
# save minimal info to be read by renderErrorBar2 |
|
| 2656 |
# renderers.js works in a very isolated environment, so we send data thru sessionStorage |
|
| 2657 |
# info is last barGap, last barCategoryGap, number of bars |
|
| 2658 | 5x |
info <- c(lbg, lcg, as.character(info)) |
| 2659 | ||
| 2660 | 5x |
info <- paste0( |
| 2661 | 5x |
"sessionStorage.setItem('ErrorBar.oss','",
|
| 2662 | 5x |
jsonlite::toJSON(info), |
| 2663 |
"'); ", |
|
| 2664 | 5x |
renderer |
| 2665 |
) |
|
| 2666 | 5x |
renderJS <- htmlwidgets::JS(info) |
| 2667 | ||
| 2668 | 5x |
for (i in seq_along(e$x$data)) {
|
| 2669 | 6x |
vector <- .build_data2( |
| 2670 | 6x |
e$x$data[[i]], |
| 2671 | 6x |
e$x$mapping$x, |
| 2672 | 6x |
lower, |
| 2673 | 6x |
upper |
| 2674 |
) |
|
| 2675 | 6x |
e_serie <- list(data = vector) |
| 2676 | 6x |
if (y_index != 0) {
|
| 2677 | 1x |
e <- .set_y_axis(e, upper, y_index, i) |
| 2678 |
} |
|
| 2679 | 6x |
if (x_index != 0) {
|
| 2680 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 2681 |
} |
|
| 2682 |
# I dont think this should be supported |
|
| 2683 |
# if (coord_system == "polar") {
|
|
| 2684 |
# e_serie$data <- e$x$data[[i]] |> |
|
| 2685 |
# dplyr::select(dplyr::all_of(c( |
|
| 2686 |
# lower, |
|
| 2687 |
# upper |
|
| 2688 |
# ))) |> |
|
| 2689 |
# unlist() |> |
|
| 2690 |
# unname() |> |
|
| 2691 |
# as.list() |
|
| 2692 |
# } |
|
| 2693 | 6x |
nm <- .name_it(e, ser[[i]]$name, name, i) |
| 2694 | ||
| 2695 | 6x |
if (!e$x$tl) {
|
| 2696 | 4x |
opts <- list( |
| 2697 | 4x |
name = nm, |
| 2698 | 4x |
type = "custom", |
| 2699 | 4x |
yAxisIndex = y_index, |
| 2700 | 4x |
xAxisIndex = x_index, |
| 2701 | 4x |
coordinateSystem = coord_system, |
| 2702 | 4x |
itemStyle = itemStyle, |
| 2703 | 4x |
renderItem = renderJS, |
| 2704 | 4x |
encode = list(x = 0, y = list(1, 2)), |
| 2705 |
... |
|
| 2706 |
) |
|
| 2707 | 4x |
if (!("z" %in% names(args))) opts$z <- 3
|
| 2708 | 4x |
if (!("color" %in% names(args))) opts$color <- "black" # set, or it will blend with main bar
|
| 2709 | ||
| 2710 | 4x |
e_serie <- append(e_serie, opts) |
| 2711 | 4x |
if (isTRUE(legend)) {
|
| 2712 | ! |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 2713 |
} |
|
| 2714 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(e_serie)) |
| 2715 |
} |
|
| 2716 |
else {
|
|
| 2717 | 2x |
if (isTRUE(legend)) {
|
| 2718 | ! |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 2719 |
} |
|
| 2720 | 2x |
e$x$opts$options[[i]]$series <- append( |
| 2721 | 2x |
e$x$opts$options[[i]]$series, |
| 2722 | 2x |
list(e_serie) |
| 2723 |
) |
|
| 2724 |
} |
|
| 2725 |
} |
|
| 2726 | 5x |
if (isTRUE(e$x$tl)) {
|
| 2727 | 1x |
series_opts <- list( |
| 2728 | 1x |
type = "custom", |
| 2729 | 1x |
yAxisIndex = y_index, |
| 2730 | 1x |
xAxisIndex = x_index, |
| 2731 | 1x |
coordinateSystem = coord_system, |
| 2732 | 1x |
itemStyle = itemStyle, |
| 2733 | 1x |
renderItem = renderJS, |
| 2734 | 1x |
encode = list(x = 0, y = list(1, 2)), |
| 2735 |
... |
|
| 2736 |
) |
|
| 2737 | 1x |
if (!is.null(name)) series_opts$name <- name |
| 2738 | 1x |
if (!("z" %in% names(args))) series_opts$z <- 3
|
| 2739 | 1x |
if (!("color" %in% names(args))) series_opts$color <- "black" # set, or it will blend with main bar
|
| 2740 | ||
| 2741 | 1x |
if (isTRUE(legend) && !is.null(name)) {
|
| 2742 | ! |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 2743 |
} |
|
| 2744 | 1x |
e$x$opts$baseOption$series <- append( |
| 2745 | 1x |
e$x$opts$baseOption$series, |
| 2746 | 1x |
list(series_opts) |
| 2747 |
) |
|
| 2748 |
} |
|
| 2749 | 5x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/custom", package = "echarts4r")
|
| 2750 | 5x |
dep <- htmltools::htmlDependency(name = "echarts-renderers", version = "1.0.2", src = c(file = path), script = "renderers.js") |
| 2751 | ||
| 2752 | 5x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 2753 | 5x |
e |> e_x_axis(type = "category") # wont work with type 'value' |
| 2754 |
} |
|
| 2755 | ||
| 2756 |
#' @rdname e_chord |
|
| 2757 |
#' @export |
|
| 2758 |
e_chord_ <- function(e, source, target, value, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2759 | 6x |
if (missing(e)) {
|
| 2760 | 1x |
stop("must pass e", call. = FALSE)
|
| 2761 |
} |
|
| 2762 | 5x |
if (missing(source) || missing(target) || missing(value)) {
|
| 2763 | 1x |
stop("missing source, target or values", call. = FALSE)
|
| 2764 |
} |
|
| 2765 | 1x |
if(e$x$tl)( |
| 2766 | 1x |
stop("timeline not supported")
|
| 2767 |
) |
|
| 2768 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 2769 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 2770 | ||
| 2771 |
# build JSON data |
|
| 2772 | 3x |
nodes <- .build_sankey_nodes(e$x$data[[1]], source, target) |
| 2773 | ||
| 2774 |
# build JSON data |
|
| 2775 | 3x |
links <- .build_sankey_edges(e$x$data[[1]], source, target, value) |
| 2776 | ||
| 2777 | 3x |
serie <- list( |
| 2778 | 3x |
type = "chord", |
| 2779 | 3x |
data = nodes, |
| 2780 | 3x |
links = links, |
| 2781 |
... |
|
| 2782 |
) |
|
| 2783 | ||
| 2784 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 2785 | 3x |
e |
| 2786 |
} |
| 1 |
#' Bar and Line chart |
|
| 2 |
#' |
|
| 3 |
#' Add bar serie. |
|
| 4 |
#' |
|
| 5 |
#' @param e An \code{echarts4r} object as returned by \code{\link{e_charts}} or
|
|
| 6 |
#' a proxy as returned by \code{\link{echarts4rProxy}}.
|
|
| 7 |
#' @param serie Column name of serie to plot. |
|
| 8 |
#' @param bind Binding between datasets, namely for use of \code{\link{e_brush}}.
|
|
| 9 |
#' @param name name of the serie. |
|
| 10 |
#' @param legend Whether to add serie to legend. |
|
| 11 |
#' @param ... Any other option to pass, check See Also section. |
|
| 12 |
#' @param x_index,y_index Indexes of x and y axis. |
|
| 13 |
#' @param coord_system Coordinate system to plot against. |
|
| 14 |
#' |
|
| 15 |
#' @note The bar serie expects the data on the x axis to be |
|
| 16 |
#' \code{categorical} in R this means a \code{factor} or
|
|
| 17 |
#' \code{character}. If the data on the x axis is numeric
|
|
| 18 |
#' everything should work well in most cases but strange |
|
| 19 |
#' behaviour may be observed. |
|
| 20 |
#' |
|
| 21 |
#' @examples |
|
| 22 |
#' library(dplyr) |
|
| 23 |
#' |
|
| 24 |
#' mtcars |> |
|
| 25 |
#' tibble::rownames_to_column("model") |>
|
|
| 26 |
#' mutate(total = mpg + qsec) |> |
|
| 27 |
#' arrange(desc(total)) |> |
|
| 28 |
#' e_charts(model) |> |
|
| 29 |
#' e_bar(mpg, stack = "grp") |> |
|
| 30 |
#' e_bar(qsec, stack = "grp") |
|
| 31 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-bar}{Additional arguments}
|
|
| 32 |
#' |
|
| 33 |
#' @rdname e_bar |
|
| 34 |
#' @export |
|
| 35 | 39x |
e_bar <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d", ...) UseMethod("e_bar")
|
| 36 | ||
| 37 | ||
| 38 |
#' @method e_bar echarts4r |
|
| 39 |
#' @export |
|
| 40 |
e_bar.echarts4r <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d", ...) {
|
|
| 41 | ||
| 42 | 38x |
if (missing(e)) {
|
| 43 | 1x |
stop("must pass e", call. = FALSE)
|
| 44 |
} |
|
| 45 | ||
| 46 | 37x |
if (missing(serie)) {
|
| 47 | 1x |
stop("must pass serie", call. = FALSE)
|
| 48 |
} |
|
| 49 | ||
| 50 | 36x |
bd <- .get_bind(deparse(substitute(bind))) |
| 51 | ||
| 52 | 36x |
sr <- deparse(substitute(serie)) |
| 53 | ||
| 54 | 36x |
e_bar_(e, sr, bd, name, legend, y_index, x_index, coord_system, ...) |
| 55 |
} |
|
| 56 | ||
| 57 |
#' @method e_bar echarts4rProxy |
|
| 58 |
#' @export |
|
| 59 |
e_bar.echarts4rProxy <- function(e, serie, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, coord_system = "cartesian2d", ...) {
|
|
| 60 | ||
| 61 | 3x |
if (missing(serie)) {
|
| 62 | 1x |
stop("must pass serie", call. = FALSE)
|
| 63 |
} |
|
| 64 | ||
| 65 | 2x |
bd <- .get_bind(deparse(substitute(bind))) |
| 66 | ||
| 67 | 2x |
sr <- deparse(substitute(serie)) |
| 68 | ||
| 69 | 2x |
e$chart <- e_bar_(e$chart, sr, bd, name, legend, y_index, x_index, coord_system, ...) |
| 70 | 2x |
return(e) |
| 71 |
} |
|
| 72 | ||
| 73 |
#' Line |
|
| 74 |
#' |
|
| 75 |
#' Add line serie. |
|
| 76 |
#' |
|
| 77 |
#' @inheritParams e_bar |
|
| 78 |
#' @param coord_system Coordinate system to plot against. |
|
| 79 |
#' |
|
| 80 |
#' @examples |
|
| 81 |
#' iris |> |
|
| 82 |
#' group_by(Species) |> |
|
| 83 |
#' e_charts(Sepal.Length) |> |
|
| 84 |
#' e_line(Sepal.Width) |> |
|
| 85 |
#' e_tooltip(trigger = "axis") |
|
| 86 |
#' |
|
| 87 |
#' # timeline |
|
| 88 |
#' iris |> |
|
| 89 |
#' group_by(Species) |> |
|
| 90 |
#' e_charts(Sepal.Length, timeline = TRUE) |> |
|
| 91 |
#' e_line(Sepal.Width) |> |
|
| 92 |
#' e_tooltip(trigger = "axis") |
|
| 93 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-line}{Additional arguments}
|
|
| 94 |
#' |
|
| 95 |
#' @rdname e_line |
|
| 96 |
#' @export |
|
| 97 | 79x |
e_line <- function( |
| 98 | 79x |
e, |
| 99 | 79x |
serie, |
| 100 | 79x |
bind, |
| 101 | 79x |
name = NULL, |
| 102 | 79x |
legend = TRUE, |
| 103 | 79x |
y_index = 0, |
| 104 | 79x |
x_index = 0, |
| 105 | 79x |
coord_system = "cartesian2d", |
| 106 | 79x |
...) UseMethod("e_line")
|
| 107 | ||
| 108 |
#' @export |
|
| 109 |
#' @method e_line echarts4r |
|
| 110 |
e_line.echarts4r <- function( |
|
| 111 |
e, |
|
| 112 |
serie, |
|
| 113 |
bind, |
|
| 114 |
name = NULL, |
|
| 115 |
legend = TRUE, |
|
| 116 |
y_index = 0, |
|
| 117 |
x_index = 0, |
|
| 118 |
coord_system = "cartesian2d", |
|
| 119 |
...) {
|
|
| 120 | 77x |
if (missing(e)) {
|
| 121 | 1x |
stop("must pass e", call. = FALSE)
|
| 122 |
} |
|
| 123 | 76x |
if (missing(serie)) {
|
| 124 | 1x |
stop("must pass serie", call. = FALSE)
|
| 125 |
} |
|
| 126 | ||
| 127 | 75x |
serie <- deparse(substitute(serie)) |
| 128 | ||
| 129 | 75x |
bd <- .get_bind(deparse(substitute(bind))) |
| 130 | ||
| 131 | 75x |
e_line_(e, serie, bd, name, legend, y_index, x_index, coord_system, ...) |
| 132 |
} |
|
| 133 | ||
| 134 |
#' @export |
|
| 135 |
#' @method e_line echarts4rProxy |
|
| 136 |
e_line.echarts4rProxy <- function( |
|
| 137 |
e, |
|
| 138 |
serie, |
|
| 139 |
bind, |
|
| 140 |
name = NULL, |
|
| 141 |
legend = TRUE, |
|
| 142 |
y_index = 0, |
|
| 143 |
x_index = 0, |
|
| 144 |
coord_system = "cartesian2d", |
|
| 145 |
...) {
|
|
| 146 | ||
| 147 | 4x |
if (missing(serie)) {
|
| 148 | 1x |
stop("must pass serie", call. = FALSE)
|
| 149 |
} |
|
| 150 | ||
| 151 | 3x |
serie <- deparse(substitute(serie)) |
| 152 | ||
| 153 | 3x |
bd <- .get_bind(deparse(substitute(bind))) |
| 154 | 3x |
e$chart <- e_line_(e$chart, serie, bd, name, legend, y_index, x_index, coord_system, ...) |
| 155 | 3x |
return(e) |
| 156 |
} |
|
| 157 | ||
| 158 |
#' Area |
|
| 159 |
#' |
|
| 160 |
#' Add area serie. Note that this is NOT an unique series type. Rather, this |
|
| 161 |
#' function is a shorthand for using `e_bar()` with `areaStyle = list()` |
|
| 162 |
#' enabled. |
|
| 163 |
#' |
|
| 164 |
#' @inheritParams e_bar |
|
| 165 |
#' |
|
| 166 |
#' @examples |
|
| 167 |
#' CO2 |> |
|
| 168 |
#' group_by(Plant) |> |
|
| 169 |
#' e_charts(conc) |> |
|
| 170 |
#' e_area(uptake) |> |
|
| 171 |
#' e_tooltip(trigger = "axis") |
|
| 172 |
#' |
|
| 173 |
#' # timeline |
|
| 174 |
#' iris |> |
|
| 175 |
#' group_by(Species) |> |
|
| 176 |
#' e_charts(Sepal.Length, timeline = TRUE) |> |
|
| 177 |
#' e_area(Sepal.Width) |> |
|
| 178 |
#' e_tooltip(trigger = "axis") |
|
| 179 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-line}{Additional arguments}
|
|
| 180 |
#' |
|
| 181 |
#' @rdname e_area |
|
| 182 |
#' @export |
|
| 183 | 10x |
e_area <- function( |
| 184 | 10x |
e, |
| 185 | 10x |
serie, |
| 186 | 10x |
bind, |
| 187 | 10x |
name = NULL, |
| 188 | 10x |
legend = TRUE, |
| 189 | 10x |
y_index = 0, |
| 190 | 10x |
x_index = 0, |
| 191 | 10x |
coord_system = "cartesian2d", |
| 192 | 10x |
...) UseMethod("e_area")
|
| 193 | ||
| 194 |
#' @export |
|
| 195 |
#' @method e_area echarts4r |
|
| 196 |
e_area.echarts4r <- function( |
|
| 197 |
e, |
|
| 198 |
serie, |
|
| 199 |
bind, |
|
| 200 |
name = NULL, |
|
| 201 |
legend = TRUE, |
|
| 202 |
y_index = 0, |
|
| 203 |
x_index = 0, |
|
| 204 |
coord_system = "cartesian2d", |
|
| 205 |
...) {
|
|
| 206 | 10x |
if (missing(e)) {
|
| 207 | 1x |
stop("must pass e", call. = FALSE)
|
| 208 |
} |
|
| 209 | 9x |
if (missing(serie)) {
|
| 210 | 1x |
stop("must pass serie", call. = FALSE)
|
| 211 |
} |
|
| 212 | ||
| 213 | 8x |
serie <- deparse(substitute(serie)) |
| 214 | ||
| 215 | 8x |
bd <- .get_bind(deparse(substitute(bind))) |
| 216 | ||
| 217 | 8x |
e_area_(e, serie, bd, name, legend, y_index, x_index, coord_system, ...) |
| 218 |
} |
|
| 219 | ||
| 220 |
#' @export |
|
| 221 |
#' @method e_area echarts4rProxy |
|
| 222 |
e_area.echarts4rProxy <- function( |
|
| 223 |
e, |
|
| 224 |
serie, |
|
| 225 |
bind, |
|
| 226 |
name = NULL, |
|
| 227 |
legend = TRUE, |
|
| 228 |
y_index = 0, |
|
| 229 |
x_index = 0, |
|
| 230 |
coord_system = "cartesian2d", |
|
| 231 |
...) {
|
|
| 232 | 2x |
if (missing(serie)) {
|
| 233 | 1x |
stop("must pass serie", call. = FALSE)
|
| 234 |
} |
|
| 235 | ||
| 236 | 1x |
serie <- deparse(substitute(serie)) |
| 237 | ||
| 238 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 239 | ||
| 240 | 1x |
e$chart <- e_area_(e$chart, serie, bd, name, legend, y_index, x_index, coord_system, ...) |
| 241 | 1x |
return(e) |
| 242 |
} |
|
| 243 | ||
| 244 |
#' Step |
|
| 245 |
#' |
|
| 246 |
#' Add step serie. |
|
| 247 |
#' |
|
| 248 |
#' @inheritParams e_bar |
|
| 249 |
#' @param step Step type, one of \code{start}, \code{middle} or \code{end}.
|
|
| 250 |
#' @param fill Set to fill as area. |
|
| 251 |
#' |
|
| 252 |
#' @examples |
|
| 253 |
#' USArrests |> |
|
| 254 |
#' tibble::rownames_to_column("State") |>
|
|
| 255 |
#' e_charts(State) |> |
|
| 256 |
#' e_step(Murder, name = "Start", step = "start", fill = TRUE) |> |
|
| 257 |
#' e_step(Rape, name = "Middle", step = "middle") |> |
|
| 258 |
#' e_step(Assault, name = "End", step = "end") |> |
|
| 259 |
#' e_tooltip(trigger = "axis") |
|
| 260 |
#' |
|
| 261 |
#' # timeline |
|
| 262 |
#' iris |> |
|
| 263 |
#' group_by(Species) |> |
|
| 264 |
#' e_charts(Sepal.Length, timeline = TRUE) |> |
|
| 265 |
#' e_step(Sepal.Width) |> |
|
| 266 |
#' e_tooltip(trigger = "axis") |
|
| 267 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-line}{Additional arguments}
|
|
| 268 |
#' |
|
| 269 |
#' @rdname e_step |
|
| 270 |
#' @export |
|
| 271 |
e_step <- function( |
|
| 272 |
e, |
|
| 273 |
serie, |
|
| 274 |
bind, |
|
| 275 |
step = c("start", "middle", "end"),
|
|
| 276 |
fill = FALSE, |
|
| 277 |
name = NULL, |
|
| 278 |
legend = TRUE, |
|
| 279 |
y_index = 0, |
|
| 280 |
x_index = 0, |
|
| 281 |
coord_system = "cartesian2d", |
|
| 282 |
...) {
|
|
| 283 | 7x |
UseMethod("e_step")
|
| 284 |
} |
|
| 285 | ||
| 286 |
#' @export |
|
| 287 |
#' @method e_step echarts4r |
|
| 288 |
e_step.echarts4r <- function( |
|
| 289 |
e, |
|
| 290 |
serie, |
|
| 291 |
bind, |
|
| 292 |
step = c("start", "middle", "end"),
|
|
| 293 |
fill = FALSE, |
|
| 294 |
name = NULL, |
|
| 295 |
legend = TRUE, |
|
| 296 |
y_index = 0, |
|
| 297 |
x_index = 0, |
|
| 298 |
coord_system = "cartesian2d", |
|
| 299 |
...) {
|
|
| 300 | ||
| 301 | 8x |
if (missing(e)) {
|
| 302 | 1x |
stop("must pass e", call. = FALSE)
|
| 303 |
} |
|
| 304 | 7x |
if (missing(serie)) {
|
| 305 | 1x |
stop("must pass serie", call. = FALSE)
|
| 306 |
} |
|
| 307 | ||
| 308 | 6x |
serie <- deparse(substitute(serie)) |
| 309 | ||
| 310 | 6x |
bd <- .get_bind(deparse(substitute(bind))) |
| 311 | ||
| 312 | 6x |
e_step_(e, serie, bd, step, fill, name, legend, y_index, x_index, coord_system = coord_system, ...) |
| 313 |
} |
|
| 314 | ||
| 315 |
#' @export |
|
| 316 |
#' @method e_step echarts4rProxy |
|
| 317 |
e_step.echarts4rProxy <- function( |
|
| 318 |
e, |
|
| 319 |
serie, |
|
| 320 |
bind, |
|
| 321 |
step = c("start", "middle", "end"),
|
|
| 322 |
fill = FALSE, |
|
| 323 |
name = NULL, |
|
| 324 |
legend = TRUE, |
|
| 325 |
y_index = 0, |
|
| 326 |
x_index = 0, |
|
| 327 |
coord_system = "cartesian2d", |
|
| 328 |
...) {
|
|
| 329 | 2x |
if (missing(serie)) {
|
| 330 | 1x |
stop("must pass serie", call. = FALSE)
|
| 331 |
} |
|
| 332 | ||
| 333 | 1x |
serie <- deparse(substitute(serie)) |
| 334 | ||
| 335 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 336 | ||
| 337 | 1x |
e$chart <- e_step_(e$chart, serie, bd, step, fill, name, legend, y_index, x_index, coord_system = coord_system, ...) |
| 338 | 1x |
return(e) |
| 339 |
} |
|
| 340 | ||
| 341 |
#' Scatter |
|
| 342 |
#' |
|
| 343 |
#' Add scatter serie. |
|
| 344 |
#' |
|
| 345 |
#' @inheritParams e_bar |
|
| 346 |
#' @param size Column name containing size of points. |
|
| 347 |
#' @param symbol The symbol to use, default to \code{NULL}, can also be \code{circle}, \code{rect},
|
|
| 348 |
#' \code{roundRect}, \code{triangle}, \code{diamond}, \code{pin}, \code{arrow}, or \code{none}.
|
|
| 349 |
#' @param symbol_size Size of points, either an integer or a vector of length 2, |
|
| 350 |
#' if \code{size} is \emph{not} \code{NULL} or missing it is applied as a multiplier to \code{scale}.
|
|
| 351 |
#' @param scale A function that takes a vector of \code{numeric} and returns a vector of \code{numeric}
|
|
| 352 |
#' of the same length. You can disable the scaling by setting it to \code{NULL}.
|
|
| 353 |
#' @param coord_system Coordinate system to plot against, see examples. |
|
| 354 |
#' @param rm_x,rm_y Whether to remove x and y axis, only applies if \code{coord_system} is not
|
|
| 355 |
#' set to \code{cartesian2d}.
|
|
| 356 |
#' @param x A vector of integers or numeric. |
|
| 357 |
#' @param jitter_factor,jitter_amount Jitter points, passed to \code{jitter}. This is now deprecated since the upgrade to echarts v6.
|
|
| 358 |
#' Recommend using e_jitter(). |
|
| 359 |
#' @param scale_js the JavaScript scaling function. |
|
| 360 |
#' |
|
| 361 |
#' @section Scaling function: defaults to \code{e_scale} which is a basic function that rescales \code{size}
|
|
| 362 |
#' between 1 and 20 for that makes for decent sized points on the chart. |
|
| 363 |
#' |
|
| 364 |
#' @examples |
|
| 365 |
#' # scaling |
|
| 366 |
#' e_scale(c(1, 1000)) |
|
| 367 |
#' |
|
| 368 |
#' mtcars |> |
|
| 369 |
#' e_charts(mpg) |> |
|
| 370 |
#' e_scatter(wt, qsec) |
|
| 371 |
#' |
|
| 372 |
#' # custom function |
|
| 373 |
#' my_scale <- function(x) scales::rescale(x, to = c(2, 50)) |
|
| 374 |
#' |
|
| 375 |
#' echart <- mtcars |> |
|
| 376 |
#' e_charts(mpg) |> |
|
| 377 |
#' e_scatter(wt, qsec, scale = my_scale) |
|
| 378 |
#' |
|
| 379 |
#' echart |
|
| 380 |
#' |
|
| 381 |
#' # rescale color too |
|
| 382 |
#' echart |> |
|
| 383 |
#' e_visual_map(wt, scale = my_scale) |
|
| 384 |
#' |
|
| 385 |
#' # or |
|
| 386 |
#' echart |> |
|
| 387 |
#' e_visual_map(min = 2, max = 50) |
|
| 388 |
#' |
|
| 389 |
#' # disable scaling |
|
| 390 |
#' mtcars |> |
|
| 391 |
#' e_charts(qsec) |> |
|
| 392 |
#' e_scatter(wt, mpg, scale = NULL) |
|
| 393 |
#' |
|
| 394 |
#' # jitter point |
|
| 395 |
#' mtcars |> |
|
| 396 |
#' e_charts(cyl) |> |
|
| 397 |
#' e_scatter(wt, symbol_size = 5) |> |
|
| 398 |
#' e_scatter(wt, jitter_factor = 2, legend = FALSE) |
|
| 399 |
#' |
|
| 400 |
#' # examples |
|
| 401 |
#' USArrests |> |
|
| 402 |
#' e_charts(Assault) |> |
|
| 403 |
#' e_scatter(Murder, Rape) |> |
|
| 404 |
#' e_effect_scatter(Rape, Murder, y_index = 1) |> |
|
| 405 |
#' e_grid(index = c(0, 1)) |> |
|
| 406 |
#' e_tooltip() |
|
| 407 |
#' |
|
| 408 |
#' iris |> |
|
| 409 |
#' e_charts_("Sepal.Length") |>
|
|
| 410 |
#' e_scatter_( |
|
| 411 |
#' "Sepal.Width", |
|
| 412 |
#' symbol_size = c(8, 2), |
|
| 413 |
#' symbol = "rect" |
|
| 414 |
#' ) |> |
|
| 415 |
#' e_x_axis(min = 4) |
|
| 416 |
#' |
|
| 417 |
#' quakes |> |
|
| 418 |
#' e_charts(long) |> |
|
| 419 |
#' e_geo( |
|
| 420 |
#' roam = TRUE, |
|
| 421 |
#' boundingCoords = list( |
|
| 422 |
#' c(185, -10), |
|
| 423 |
#' c(165, -40) |
|
| 424 |
#' ) |
|
| 425 |
#' ) |> |
|
| 426 |
#' e_scatter(lat, mag, coord_system = "geo") |> |
|
| 427 |
#' e_visual_map(min = 4, max = 6.5) |
|
| 428 |
#' |
|
| 429 |
#' # timeline |
|
| 430 |
#' iris |> |
|
| 431 |
#' group_by(Species) |> |
|
| 432 |
#' e_charts(Petal.Width, timeline = TRUE) |> |
|
| 433 |
#' e_scatter(Sepal.Width, Sepal.Length) |> |
|
| 434 |
#' e_tooltip(trigger = "axis") |
|
| 435 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-scatter}{Additional arguments scatter},
|
|
| 436 |
#' \href{https://echarts.apache.org/en/option.html#series-effectScatter}{Additional arguments for effect scatter}
|
|
| 437 |
#' |
|
| 438 |
#' @rdname scatter |
|
| 439 |
#' @export |
|
| 440 |
e_scatter <- function( |
|
| 441 |
e, |
|
| 442 |
serie, |
|
| 443 |
size, |
|
| 444 |
bind, |
|
| 445 |
symbol = NULL, |
|
| 446 |
symbol_size = 1, |
|
| 447 |
scale = e_scale, |
|
| 448 |
scale_js = "function(data){ return data[3];}",
|
|
| 449 |
name = NULL, |
|
| 450 |
coord_system = "cartesian2d", |
|
| 451 |
jitter_factor = 0, |
|
| 452 |
jitter_amount = NULL, |
|
| 453 |
legend = TRUE, |
|
| 454 |
y_index = 0, |
|
| 455 |
x_index = 0, |
|
| 456 |
rm_x = TRUE, |
|
| 457 |
rm_y = TRUE, |
|
| 458 |
...) {
|
|
| 459 | 129x |
UseMethod("e_scatter")
|
| 460 |
} |
|
| 461 | ||
| 462 |
#' @export |
|
| 463 |
#' @method e_scatter echarts4r |
|
| 464 |
e_scatter.echarts4r <- function( |
|
| 465 |
e, |
|
| 466 |
serie, |
|
| 467 |
size, |
|
| 468 |
bind, |
|
| 469 |
symbol = NULL, |
|
| 470 |
symbol_size = 1, |
|
| 471 |
scale = e_scale, |
|
| 472 |
scale_js = "function(data){ return data[3];}",
|
|
| 473 |
name = NULL, |
|
| 474 |
coord_system = "cartesian2d", |
|
| 475 |
jitter_factor = 0, |
|
| 476 |
jitter_amount = NULL, |
|
| 477 |
legend = TRUE, |
|
| 478 |
y_index = 0, |
|
| 479 |
x_index = 0, |
|
| 480 |
rm_x = TRUE, |
|
| 481 |
rm_y = TRUE, |
|
| 482 |
...) {
|
|
| 483 | 129x |
if (missing(e)) {
|
| 484 | 1x |
stop("must pass e", call. = FALSE)
|
| 485 |
} |
|
| 486 | 128x |
if (missing(serie)) {
|
| 487 | 1x |
stop("must pass serie", call. = FALSE)
|
| 488 |
} |
|
| 489 | ||
| 490 | 127x |
serie <- deparse(substitute(serie)) |
| 491 | 127x |
sz <- .get_size(deparse(substitute(size))) |
| 492 | 127x |
bd <- .get_bind(deparse(substitute(bind))) |
| 493 | ||
| 494 | 127x |
e_scatter_( |
| 495 | 127x |
e = e, |
| 496 | 127x |
serie = serie, |
| 497 | 127x |
size = sz, |
| 498 | 127x |
bind = bd, |
| 499 | 127x |
symbol = symbol, |
| 500 | 127x |
symbol_size = symbol_size, |
| 501 | 127x |
scale = scale, |
| 502 | 127x |
scale_js = scale_js, |
| 503 | 127x |
name = name, |
| 504 | 127x |
coord_system = coord_system, |
| 505 | 127x |
jitter_factor = jitter_factor, |
| 506 | 127x |
jitter_amount = jitter_amount, |
| 507 | 127x |
legend = legend, |
| 508 | 127x |
y_index = y_index, |
| 509 | 127x |
x_index = x_index, |
| 510 | 127x |
rm_x = rm_x, |
| 511 | 127x |
rm_y = rm_y, |
| 512 |
... |
|
| 513 |
) |
|
| 514 |
} |
|
| 515 | ||
| 516 |
#' @export |
|
| 517 |
#' @method e_scatter echarts4rProxy |
|
| 518 |
e_scatter.echarts4rProxy <- function( |
|
| 519 |
e, |
|
| 520 |
serie, |
|
| 521 |
size, |
|
| 522 |
bind, |
|
| 523 |
symbol = NULL, |
|
| 524 |
symbol_size = 1, |
|
| 525 |
scale = e_scale, |
|
| 526 |
scale_js = "function(data){ return data[3];}",
|
|
| 527 |
name = NULL, |
|
| 528 |
coord_system = "cartesian2d", |
|
| 529 |
jitter_factor = 0, |
|
| 530 |
jitter_amount = NULL, |
|
| 531 |
legend = TRUE, |
|
| 532 |
y_index = 0, |
|
| 533 |
x_index = 0, |
|
| 534 |
rm_x = TRUE, |
|
| 535 |
rm_y = TRUE, |
|
| 536 |
...) {
|
|
| 537 | ||
| 538 | 2x |
if (missing(serie)) {
|
| 539 | 1x |
stop("must pass serie", call. = FALSE)
|
| 540 |
} |
|
| 541 | ||
| 542 | 1x |
serie <- deparse(substitute(serie)) |
| 543 | ||
| 544 | 1x |
sz <- .get_size(deparse(substitute(size))) |
| 545 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 546 | ||
| 547 | 1x |
e$chart <- e_scatter_( |
| 548 | 1x |
e = e$chart, |
| 549 | 1x |
serie = serie, |
| 550 | 1x |
size = sz, |
| 551 | 1x |
bind = bd, |
| 552 | 1x |
symbol = symbol, |
| 553 | 1x |
symbol_size = symbol_size, |
| 554 | 1x |
scale = scale, |
| 555 | 1x |
scale_js = scale_js, |
| 556 | 1x |
name = name, |
| 557 | 1x |
coord_system = coord_system, |
| 558 | 1x |
jitter_factor = jitter_factor, |
| 559 | 1x |
jitter_amount = jitter_amount, |
| 560 | 1x |
legend = legend, |
| 561 | 1x |
y_index = y_index, |
| 562 | 1x |
x_index = x_index, |
| 563 | 1x |
rm_x = rm_x, |
| 564 | 1x |
rm_y = rm_y, |
| 565 |
... |
|
| 566 |
) |
|
| 567 | ||
| 568 | 1x |
return(e) |
| 569 |
} |
|
| 570 | ||
| 571 |
#' @rdname scatter |
|
| 572 |
#' @export |
|
| 573 |
e_effect_scatter <- function( |
|
| 574 |
e, |
|
| 575 |
serie, |
|
| 576 |
size, |
|
| 577 |
bind, |
|
| 578 |
symbol = NULL, |
|
| 579 |
symbol_size = 1, |
|
| 580 |
scale = e_scale, |
|
| 581 |
scale_js = "function(data){ return data[3];}",
|
|
| 582 |
name = NULL, |
|
| 583 |
coord_system = "cartesian2d", |
|
| 584 |
legend = TRUE, |
|
| 585 |
y_index = 0, |
|
| 586 |
x_index = 0, |
|
| 587 |
rm_x = TRUE, |
|
| 588 |
rm_y = TRUE, |
|
| 589 |
...) {
|
|
| 590 | 9x |
UseMethod("e_effect_scatter")
|
| 591 |
} |
|
| 592 | ||
| 593 |
#' @export |
|
| 594 |
#' @method e_effect_scatter echarts4r |
|
| 595 |
e_effect_scatter.echarts4r <- function( |
|
| 596 |
e, |
|
| 597 |
serie, |
|
| 598 |
size, |
|
| 599 |
bind, |
|
| 600 |
symbol = NULL, |
|
| 601 |
symbol_size = 1, |
|
| 602 |
scale = e_scale, |
|
| 603 |
scale_js = "function(data){ return data[3];}",
|
|
| 604 |
name = NULL, |
|
| 605 |
coord_system = "cartesian2d", |
|
| 606 |
legend = TRUE, |
|
| 607 |
y_index = 0, |
|
| 608 |
x_index = 0, |
|
| 609 |
rm_x = TRUE, |
|
| 610 |
rm_y = TRUE, |
|
| 611 |
...) {
|
|
| 612 | 9x |
if (missing(e)) {
|
| 613 | 1x |
stop("must pass e", call. = FALSE)
|
| 614 |
} |
|
| 615 | 8x |
if (missing(serie)) {
|
| 616 | 1x |
stop("must pass serie", call. = FALSE)
|
| 617 |
} |
|
| 618 | ||
| 619 | 7x |
serie <- deparse(substitute(serie)) |
| 620 | ||
| 621 | 7x |
sz <- .get_size(deparse(substitute(size))) |
| 622 | 7x |
bd <- .get_bind(deparse(substitute(bind))) |
| 623 | ||
| 624 | 7x |
e_effect_scatter_( |
| 625 | 7x |
e, |
| 626 | 7x |
serie = serie, |
| 627 | 7x |
size = sz, |
| 628 | 7x |
bind = bd, |
| 629 | 7x |
symbol = symbol, |
| 630 | 7x |
symbol_size = symbol_size, |
| 631 | 7x |
scale = scale, |
| 632 | 7x |
scale_js = scale_js, |
| 633 | 7x |
name = name, |
| 634 | 7x |
coord_system, |
| 635 | 7x |
legend, |
| 636 | 7x |
y_index, |
| 637 | 7x |
x_index, |
| 638 | 7x |
rm_x, |
| 639 | 7x |
rm_y, |
| 640 |
... |
|
| 641 |
) |
|
| 642 |
} |
|
| 643 | ||
| 644 |
#' @export |
|
| 645 |
#' @method e_effect_scatter echarts4rProxy |
|
| 646 |
e_effect_scatter.echarts4rProxy <- function( |
|
| 647 |
e, |
|
| 648 |
serie, |
|
| 649 |
size, |
|
| 650 |
bind, |
|
| 651 |
symbol = NULL, |
|
| 652 |
symbol_size = 1, |
|
| 653 |
scale = e_scale, |
|
| 654 |
scale_js = "function(data){ return data[3];}",
|
|
| 655 |
name = NULL, |
|
| 656 |
coord_system = "cartesian2d", |
|
| 657 |
legend = TRUE, |
|
| 658 |
y_index = 0, |
|
| 659 |
x_index = 0, |
|
| 660 |
rm_x = TRUE, |
|
| 661 |
rm_y = TRUE, |
|
| 662 |
...) {
|
|
| 663 | 2x |
if (missing(serie)) {
|
| 664 | 1x |
stop("must pass serie", call. = FALSE)
|
| 665 |
} |
|
| 666 | ||
| 667 | 1x |
serie <- deparse(substitute(serie)) |
| 668 | ||
| 669 | 1x |
sz <- .get_size(deparse(substitute(size))) |
| 670 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 671 | ||
| 672 | 1x |
e$chart <- e_effect_scatter_( |
| 673 | 1x |
e$chart, |
| 674 | 1x |
serie = serie, |
| 675 | 1x |
size = sz, |
| 676 | 1x |
bind = bd, |
| 677 | 1x |
symbol = symbol, |
| 678 | 1x |
symbol_size = symbol_size, |
| 679 | 1x |
scale = scale, |
| 680 | 1x |
scale_js = scale_js, |
| 681 | 1x |
name = name, |
| 682 | 1x |
coord_system, |
| 683 | 1x |
legend, |
| 684 | 1x |
y_index, |
| 685 | 1x |
x_index, |
| 686 | 1x |
rm_x, |
| 687 | 1x |
rm_y, |
| 688 |
... |
|
| 689 |
) |
|
| 690 | ||
| 691 | 1x |
return(e) |
| 692 |
} |
|
| 693 | ||
| 694 |
#' Candlestick |
|
| 695 |
#' |
|
| 696 |
#' Add a candlestick chart. |
|
| 697 |
#' |
|
| 698 |
#' @inheritParams e_bar |
|
| 699 |
#' @param opening,closing,low,high Stock prices. |
|
| 700 |
#' |
|
| 701 |
#' @examples |
|
| 702 |
#' date <- c( |
|
| 703 |
#' "2017-01-01", |
|
| 704 |
#' "2017-01-02", |
|
| 705 |
#' "2017-01-03", |
|
| 706 |
#' "2017-01-04", |
|
| 707 |
#' "2017-03-05", |
|
| 708 |
#' "2017-01-06", |
|
| 709 |
#' "2017-01-07" |
|
| 710 |
#' ) |
|
| 711 |
#' |
|
| 712 |
#' stock <- data.frame( |
|
| 713 |
#' date = date, |
|
| 714 |
#' opening = c(200.60, 200.22, 198.43, 199.05, 203.54, 203.40, 208.34), |
|
| 715 |
#' closing = c(200.72, 198.85, 199.05, 203.73, 204.08, 208.11, 211.88), |
|
| 716 |
#' low = c(197.82, 198.07, 197.90, 198.10, 202.00, 201.50, 207.60), |
|
| 717 |
#' high = c(203.32, 200.67, 200.00, 203.95, 204.90, 208.44, 213.17) |
|
| 718 |
#' ) |
|
| 719 |
#' |
|
| 720 |
#' stock |> |
|
| 721 |
#' e_charts(date) |> |
|
| 722 |
#' e_candle(opening, closing, low, high) |> |
|
| 723 |
#' e_y_axis(min = 190, max = 220) |
|
| 724 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-candlestick}{Additional arguments}
|
|
| 725 |
#' |
|
| 726 |
#' @rdname e_candle |
|
| 727 |
#' @export |
|
| 728 |
e_candle <- function(e, opening, closing, low, high, bind, name = "candle", legend = TRUE, ...) {
|
|
| 729 | 4x |
UseMethod("e_candle")
|
| 730 |
} |
|
| 731 | ||
| 732 |
#' @export |
|
| 733 |
#' @method e_candle echarts4r |
|
| 734 |
e_candle.echarts4r <- function(e, opening, closing, low, high, bind, name = "candle", legend = TRUE, ...) {
|
|
| 735 | ||
| 736 | 6x |
if (missing(e)) {
|
| 737 | 2x |
stop("must pass e", call. = FALSE)
|
| 738 |
} |
|
| 739 | ||
| 740 | 4x |
if (missing(opening) || missing(closing) || missing(low) || missing(high)) {
|
| 741 | 2x |
stop("missing inputs", call. = FALSE)
|
| 742 |
} |
|
| 743 | ||
| 744 | 2x |
bd <- .get_bind(deparse(substitute(bind))) |
| 745 | ||
| 746 | 2x |
e_candle_( |
| 747 | 2x |
e, |
| 748 | 2x |
deparse(substitute(opening)), |
| 749 | 2x |
deparse(substitute(closing)), |
| 750 | 2x |
deparse(substitute(low)), |
| 751 | 2x |
deparse(substitute(high)), |
| 752 | 2x |
bd, |
| 753 | 2x |
name, |
| 754 | 2x |
legend, |
| 755 |
... |
|
| 756 |
) |
|
| 757 |
} |
|
| 758 | ||
| 759 |
#' @export |
|
| 760 |
#' @method e_candle echarts4rProxy |
|
| 761 |
e_candle.echarts4rProxy <- function(e, opening, closing, low, high, bind, name = "candle", legend = TRUE, ...) {
|
|
| 762 | ||
| 763 | 2x |
if (missing(opening) || missing(closing) || missing(low) || missing(high)) {
|
| 764 | 1x |
stop("missing inputs", call. = FALSE)
|
| 765 |
} |
|
| 766 | ||
| 767 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 768 | ||
| 769 | 1x |
e$chart <- e_candle_( |
| 770 | 1x |
e$chart, |
| 771 | 1x |
deparse(substitute(opening)), |
| 772 | 1x |
deparse(substitute(closing)), |
| 773 | 1x |
deparse(substitute(low)), |
| 774 | 1x |
deparse(substitute(high)), |
| 775 | 1x |
bd, |
| 776 | 1x |
name, |
| 777 | 1x |
legend, |
| 778 |
... |
|
| 779 |
) |
|
| 780 | ||
| 781 | 1x |
return(e) |
| 782 |
} |
|
| 783 | ||
| 784 |
#' Radar |
|
| 785 |
#' |
|
| 786 |
#' Add a radar chart |
|
| 787 |
#' |
|
| 788 |
#' @inheritParams e_bar |
|
| 789 |
#' @param max Maximum value. |
|
| 790 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 791 |
#' @param radar A \code{list} of options to pass to the \code{radar}
|
|
| 792 |
#' rather than the serie, see \href{https://echarts.apache.org/en/option.html#radar}{official documentation}
|
|
| 793 |
#' alternatively, use the \code{\link{e_radar_opts}}.
|
|
| 794 |
#' |
|
| 795 |
#' @examples |
|
| 796 |
#' df <- data.frame( |
|
| 797 |
#' x = LETTERS[1:5], |
|
| 798 |
#' y = runif(5, 1, 5), |
|
| 799 |
#' z = runif(5, 3, 7) |
|
| 800 |
#' ) |
|
| 801 |
#' |
|
| 802 |
#' df |> |
|
| 803 |
#' e_charts(x) |> |
|
| 804 |
#' e_radar(y, max = 7) |> |
|
| 805 |
#' e_radar(z) |> |
|
| 806 |
#' e_tooltip(trigger = "item") |
|
| 807 |
#' @rdname e_radar |
|
| 808 |
#' @export |
|
| 809 |
e_radar <- function( |
|
| 810 |
e, |
|
| 811 |
serie, |
|
| 812 |
max = 100, |
|
| 813 |
name = NULL, |
|
| 814 |
legend = TRUE, |
|
| 815 |
rm_x = TRUE, |
|
| 816 |
rm_y = TRUE, |
|
| 817 |
..., |
|
| 818 |
radar = list()) {
|
|
| 819 | 13x |
UseMethod("e_radar")
|
| 820 |
} |
|
| 821 | ||
| 822 |
#' @export |
|
| 823 |
#' @method e_radar echarts4r |
|
| 824 |
e_radar.echarts4r <- function( |
|
| 825 |
e, |
|
| 826 |
serie, |
|
| 827 |
max = 100, |
|
| 828 |
name = NULL, |
|
| 829 |
legend = TRUE, |
|
| 830 |
rm_x = TRUE, |
|
| 831 |
rm_y = TRUE, |
|
| 832 |
..., |
|
| 833 |
radar = list()) {
|
|
| 834 | ||
| 835 | 13x |
if (missing(e)) {
|
| 836 | 1x |
stop("must pass e", call. = FALSE)
|
| 837 |
} |
|
| 838 | ||
| 839 | 12x |
if (missing(serie)) {
|
| 840 | 1x |
stop("must pass serie", call. = FALSE)
|
| 841 |
} |
|
| 842 | ||
| 843 | 11x |
serie <- deparse(substitute(serie)) |
| 844 | ||
| 845 | 11x |
e_radar_(e, serie, max, name, legend, rm_x, rm_y, ..., radar = radar) |
| 846 |
} |
|
| 847 | ||
| 848 |
#' @export |
|
| 849 |
#' @method e_radar echarts4rProxy |
|
| 850 |
e_radar.echarts4rProxy <- function( |
|
| 851 |
e, |
|
| 852 |
serie, |
|
| 853 |
max = 100, |
|
| 854 |
name = NULL, |
|
| 855 |
legend = TRUE, |
|
| 856 |
rm_x = TRUE, |
|
| 857 |
rm_y = TRUE, |
|
| 858 |
..., |
|
| 859 |
radar = list()) {
|
|
| 860 | 2x |
if (missing(serie)) {
|
| 861 | 1x |
stop("must pass serie", call. = FALSE)
|
| 862 |
} |
|
| 863 | ||
| 864 | 1x |
if(is.null(e$chart$x$mapping$x)){
|
| 865 | ! |
stop("provide x in echarts4rProxy")
|
| 866 |
} |
|
| 867 | ||
| 868 | 1x |
serie <- deparse(substitute(serie)) |
| 869 | ||
| 870 | 1x |
e$chart <- e_radar_(e$chart, serie, max, name, legend, rm_x, rm_y, ..., radar = radar) |
| 871 | 1x |
return(e) |
| 872 |
} |
|
| 873 | ||
| 874 |
#' Funnel |
|
| 875 |
#' |
|
| 876 |
#' Add a funnel. |
|
| 877 |
#' |
|
| 878 |
#' @param e An \code{echarts4r} object as returned by \code{\link{e_charts}}.
|
|
| 879 |
#' @param name name of the serie. |
|
| 880 |
#' @param legend Whether to add serie to legend. |
|
| 881 |
#' @param ... Any other option to pass to \code{bar} or \code{line} char types.
|
|
| 882 |
#' @param values,labels Values and labels of funnel. |
|
| 883 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 884 |
#' |
|
| 885 |
#' @details No \code{bind} argument here, with a funnel \code{bind} = \code{labels}.
|
|
| 886 |
#' |
|
| 887 |
#' @examples |
|
| 888 |
#' funnel <- data.frame( |
|
| 889 |
#' stage = c("View", "Click", "Purchase"),
|
|
| 890 |
#' value = c(80, 30, 20) |
|
| 891 |
#' ) |
|
| 892 |
#' |
|
| 893 |
#' funnel |> |
|
| 894 |
#' e_charts() |> |
|
| 895 |
#' e_funnel(value, stage) |
|
| 896 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-funnel}{Additional arguments}
|
|
| 897 |
#' |
|
| 898 |
#' @rdname e_funnel |
|
| 899 |
#' @export |
|
| 900 |
e_funnel <- function(e, values, labels, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...){
|
|
| 901 | 11x |
UseMethod("e_funnel")
|
| 902 |
} |
|
| 903 |
#' @export |
|
| 904 |
#' @method e_funnel echarts4r |
|
| 905 |
e_funnel.echarts4r <- function(e, values, labels, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 906 | 11x |
if (missing(e)) {
|
| 907 | 1x |
stop("must pass e", call. = FALSE)
|
| 908 |
} |
|
| 909 | 10x |
if (missing(values) || missing(labels)) {
|
| 910 | 1x |
stop("missing values or labels", call. = FALSE)
|
| 911 |
} |
|
| 912 | ||
| 913 | 9x |
e_funnel_( |
| 914 | 9x |
e = e, |
| 915 | 9x |
values = deparse(substitute(values)), |
| 916 | 9x |
labels = deparse(substitute(labels)), |
| 917 | 9x |
name = name, |
| 918 | 9x |
legend = legend, |
| 919 | 9x |
rm_x = rm_x, |
| 920 | 9x |
rm_y = rm_y, |
| 921 |
... |
|
| 922 |
) |
|
| 923 |
} |
|
| 924 | ||
| 925 |
#' @export |
|
| 926 |
#' @method e_funnel echarts4rProxy |
|
| 927 |
e_funnel.echarts4rProxy <- function(e, values, labels, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 928 | 2x |
if (missing(values) || missing(labels)) {
|
| 929 | 1x |
stop("missing values or labels", call. = FALSE)
|
| 930 |
} |
|
| 931 | ||
| 932 | 1x |
e$chart <- e_funnel_( |
| 933 | 1x |
e = e$chart, |
| 934 | 1x |
values = deparse(substitute(values)), |
| 935 | 1x |
labels = deparse(substitute(labels)), |
| 936 | 1x |
name = name, |
| 937 | 1x |
legend = legend, |
| 938 | 1x |
rm_x = rm_x, |
| 939 | 1x |
rm_y = rm_y, |
| 940 |
... |
|
| 941 |
) |
|
| 942 | ||
| 943 | 1x |
return(e) |
| 944 |
} |
|
| 945 | ||
| 946 |
#' Sankey |
|
| 947 |
#' |
|
| 948 |
#' Draw a sankey diagram. |
|
| 949 |
#' |
|
| 950 |
#' @inheritParams e_bar |
|
| 951 |
#' @param layout Layout of sankey. |
|
| 952 |
#' @param source,target Source and target columns. |
|
| 953 |
#' @param value Value change from \code{source} to \code{target}.
|
|
| 954 |
#' @param rm_x,rm_y Whether to remove the x and y axis, defaults to \code{TRUE}.
|
|
| 955 |
#' |
|
| 956 |
#' @examples |
|
| 957 |
#' sankey <- data.frame( |
|
| 958 |
#' source = c("a", "b", "c", "d", "c"),
|
|
| 959 |
#' target = c("b", "c", "d", "e", "e"),
|
|
| 960 |
#' value = ceiling(rnorm(5, 10, 1)), |
|
| 961 |
#' stringsAsFactors = FALSE |
|
| 962 |
#' ) |
|
| 963 |
#' |
|
| 964 |
#' sankey |> |
|
| 965 |
#' e_charts() |> |
|
| 966 |
#' e_sankey(source, target, value) |
|
| 967 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-sankey}{Additional arguments}
|
|
| 968 |
#' |
|
| 969 |
#' @rdname e_sankey |
|
| 970 |
#' @export |
|
| 971 |
e_sankey <- function(e, source, target, value, layout = "none", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 972 | 5x |
UseMethod("e_sankey")
|
| 973 |
} |
|
| 974 | ||
| 975 |
#' @export |
|
| 976 |
#' @method e_sankey echarts4r |
|
| 977 |
e_sankey.echarts4r <- function(e, source, target, value, layout = "none", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 978 | 5x |
if (missing(e)) {
|
| 979 | 1x |
stop("must pass e", call. = FALSE)
|
| 980 |
} |
|
| 981 | 4x |
if (missing(source) || missing(target) || missing(value)) {
|
| 982 | 1x |
stop("missing source, target or values", call. = FALSE)
|
| 983 |
} |
|
| 984 | 1x |
if(e$x$tl)( |
| 985 | 1x |
stop("timeline not supported")
|
| 986 |
) |
|
| 987 | ||
| 988 | 2x |
e <- .rm_axis(e, rm_x, "x") |
| 989 | 2x |
e <- .rm_axis(e, rm_y, "y") |
| 990 | ||
| 991 | 2x |
e_sankey_( |
| 992 | 2x |
e, |
| 993 | 2x |
deparse(substitute(source)), |
| 994 | 2x |
deparse(substitute(target)), |
| 995 | 2x |
deparse(substitute(value)), |
| 996 | 2x |
layout, |
| 997 | 2x |
rm_x, |
| 998 | 2x |
rm_y, |
| 999 |
... |
|
| 1000 |
) |
|
| 1001 |
} |
|
| 1002 | ||
| 1003 |
#' @export |
|
| 1004 |
#' @method e_sankey echarts4rProxy |
|
| 1005 |
e_sankey.echarts4rProxy <- function(e, source, target, value, layout = "none", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1006 | 2x |
if (missing(source) || missing(target) || missing(value)) {
|
| 1007 | 1x |
stop("missing source, target or values", call. = FALSE)
|
| 1008 |
} |
|
| 1009 | ||
| 1010 |
# Remove bc this breaks |
|
| 1011 |
# e <- .rm_axis(e, rm_x, "x") |
|
| 1012 |
# e <- .rm_axis(e, rm_y, "y") |
|
| 1013 | ||
| 1014 | 1x |
e$chart <- e_sankey_( |
| 1015 | 1x |
e$chart, |
| 1016 | 1x |
deparse(substitute(source)), |
| 1017 | 1x |
deparse(substitute(target)), |
| 1018 | 1x |
deparse(substitute(value)), |
| 1019 | 1x |
layout, |
| 1020 | 1x |
rm_x, |
| 1021 | 1x |
rm_y, |
| 1022 |
... |
|
| 1023 |
) |
|
| 1024 | ||
| 1025 | 1x |
return(e) |
| 1026 |
} |
|
| 1027 | ||
| 1028 | ||
| 1029 | ||
| 1030 |
#' Graph |
|
| 1031 |
#' |
|
| 1032 |
#' Create a graph. |
|
| 1033 |
#' |
|
| 1034 |
#' @param e An \code{echarts4} object as returned by \code{e_charts}.
|
|
| 1035 |
#' @param name Name of graph. |
|
| 1036 |
#' @param nodes Data.frame of nodes. |
|
| 1037 |
#' @param names Names of nodes, unique. |
|
| 1038 |
#' @param value Values of nodes or edges. |
|
| 1039 |
#' @param size Sizes of nodes or edges. |
|
| 1040 |
#' @param symbol Symbols of nodes. |
|
| 1041 |
#' @param legend Whether to add serie to legend. |
|
| 1042 |
#' @param category Group of nodes (i.e.: group membership). |
|
| 1043 |
#' @param xpos,ypos X and Y coordinates for nodes. Valid when \code{layout = "none"}
|
|
| 1044 |
#' @param edges Data.frame of edges. |
|
| 1045 |
#' @param source,target Column names of source and target. |
|
| 1046 |
#' @param color Variable to map to the color of the edges. |
|
| 1047 |
#' @param layout Layout, one of \code{force}, \code{none} or \code{circular}.
|
|
| 1048 |
#' @param rm_x,rm_y Whether to remove the x and y axis, defaults to \code{TRUE}.
|
|
| 1049 |
#' @param itemStyle This option is available for for GL and canvas |
|
| 1050 |
#' graph but is only necessary for GL. |
|
| 1051 |
#' @param ... Any other parameter. |
|
| 1052 |
#' |
|
| 1053 |
#' @examples |
|
| 1054 |
#' value <- rnorm(10, 10, 2) |
|
| 1055 |
#' |
|
| 1056 |
#' nodes <- data.frame( |
|
| 1057 |
#' name = sample(LETTERS, 10), |
|
| 1058 |
#' value = value, |
|
| 1059 |
#' size = value, |
|
| 1060 |
#' symbol = sample(c("circle", "rect", "triangle"), 10, replace = TRUE),
|
|
| 1061 |
#' grp = rep(c("grp1", "grp2"), 5),
|
|
| 1062 |
#' stringsAsFactors = FALSE |
|
| 1063 |
#' ) |
|
| 1064 |
#' |
|
| 1065 |
#' value_edges <- sample(1:100, 20, replace = TRUE) |
|
| 1066 |
#' edges <- data.frame( |
|
| 1067 |
#' source = sample(nodes$name, 20, replace = TRUE), |
|
| 1068 |
#' target = sample(nodes$name, 20, replace = TRUE), |
|
| 1069 |
#' value = value_edges, |
|
| 1070 |
#' size = ceiling(value_edges / 20), |
|
| 1071 |
#' stringsAsFactors = FALSE |
|
| 1072 |
#' ) |
|
| 1073 |
#' |
|
| 1074 |
#' e_charts() |> |
|
| 1075 |
#' e_graph() |> |
|
| 1076 |
#' e_graph_nodes(nodes, name, value, size, grp, symbol) |> |
|
| 1077 |
#' e_graph_edges(edges, source, target, value, size) |> |
|
| 1078 |
#' e_tooltip() |
|
| 1079 |
#' |
|
| 1080 |
#' # Use graphGL for larger networks |
|
| 1081 |
#' nodes <- data.frame( |
|
| 1082 |
#' name = paste0(LETTERS, 1:1000), |
|
| 1083 |
#' value = rnorm(1000, 10, 2), |
|
| 1084 |
#' size = rnorm(1000, 10, 2), |
|
| 1085 |
#' grp = rep(c("grp1", "grp2"), 500),
|
|
| 1086 |
#' stringsAsFactors = FALSE |
|
| 1087 |
#' ) |
|
| 1088 |
#' |
|
| 1089 |
#' edges <- data.frame( |
|
| 1090 |
#' source = sample(nodes$name, 2000, replace = TRUE), |
|
| 1091 |
#' target = sample(nodes$name, 2000, replace = TRUE), |
|
| 1092 |
#' stringsAsFactors = FALSE |
|
| 1093 |
#' ) |
|
| 1094 |
#' |
|
| 1095 |
#' e_charts() |> |
|
| 1096 |
#' e_graph_gl() |> |
|
| 1097 |
#' e_graph_nodes(nodes, name, value, size, grp) |> |
|
| 1098 |
#' e_graph_edges(edges, source, target) |
|
| 1099 |
#' |
|
| 1100 |
#' # Fixed node positions, and edge color by variable |
|
| 1101 |
#' nodes <- data.frame( |
|
| 1102 |
#' name = c("A", "B", "C", "D", "E"),
|
|
| 1103 |
#' value = c("A", "B", "C", "D", "E"),
|
|
| 1104 |
#' group = c("gr1", "gr1", "gr2", "gr2", "gr3"),
|
|
| 1105 |
#' size = 3:7 * 10, |
|
| 1106 |
#' x = c(0, 200, 400, 600, 800), |
|
| 1107 |
#' y = c(100, 100, 200, 200, 0) |
|
| 1108 |
#' ) |
|
| 1109 |
#' |
|
| 1110 |
#' edges <- data.frame( |
|
| 1111 |
#' source = c("A", "B", "C", "D", "E"),
|
|
| 1112 |
#' target = c("B", "C", "D", "E", "D"),
|
|
| 1113 |
#' size = rep(3, 5), |
|
| 1114 |
#' color = c("red", "green", "blue", "yellow", "black")
|
|
| 1115 |
#' ) |
|
| 1116 |
#' |
|
| 1117 |
#' e_charts() |> |
|
| 1118 |
#' e_graph(layout = "none", autoCurveness = TRUE) |> |
|
| 1119 |
#' e_graph_nodes(nodes, name, value, size, category = group, xpos = x, ypos = y) |> |
|
| 1120 |
#' e_graph_edges(edges, source, target, size = size, color = color) |> |
|
| 1121 |
#' e_tooltip() |
|
| 1122 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-graph}{Additional arguments},
|
|
| 1123 |
#' \code{\link{e_modularity}}
|
|
| 1124 |
#' |
|
| 1125 |
#' @rdname graph |
|
| 1126 |
#' @export |
|
| 1127 | 15x |
e_graph <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_graph")
|
| 1128 | ||
| 1129 |
#' @export |
|
| 1130 |
#' @method e_graph echarts4r |
|
| 1131 |
e_graph.echarts4r <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1132 | 13x |
if (missing(e)) {
|
| 1133 | 2x |
stop("must pass e", call. = FALSE)
|
| 1134 |
} |
|
| 1135 | ||
| 1136 | 11x |
e <- .rm_axis(e, rm_x, "x") |
| 1137 | 11x |
e <- .rm_axis(e, rm_y, "y") |
| 1138 | ||
| 1139 | 11x |
serie <- list( |
| 1140 | 11x |
name = name, |
| 1141 | 11x |
type = "graph", |
| 1142 | 11x |
layout = layout, |
| 1143 |
... |
|
| 1144 |
) |
|
| 1145 | ||
| 1146 | 11x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 1147 | ||
| 1148 |
# dependency |
|
| 1149 | 11x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 1150 | 11x |
dep <- htmltools::htmlDependency( |
| 1151 | 11x |
name = "echarts-graph-modularity", |
| 1152 | 11x |
version = "1.1.0", |
| 1153 | 11x |
src = c(file = path), |
| 1154 | 11x |
script = "echarts-graph-modularity.min.js" |
| 1155 |
) |
|
| 1156 | ||
| 1157 | 11x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 1158 | ||
| 1159 | 11x |
e |
| 1160 |
} |
|
| 1161 | ||
| 1162 |
#' @export |
|
| 1163 |
#' @method e_graph echarts4rProxy |
|
| 1164 |
e_graph.echarts4rProxy <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1165 | 4x |
e$chart <- e_graph(e$chart, layout, name, rm_x, rm_y, ...) |
| 1166 | 4x |
return(e) |
| 1167 |
} |
|
| 1168 | ||
| 1169 |
#' @rdname graph |
|
| 1170 |
#' @export |
|
| 1171 | 12x |
e_graph_gl <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ..., itemStyle = list(opacity = 1)) UseMethod("e_graph_gl")
|
| 1172 | ||
| 1173 |
#' @export |
|
| 1174 |
#' @method e_graph_gl echarts4r |
|
| 1175 |
e_graph_gl.echarts4r <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ..., itemStyle = list(opacity = 1)) {
|
|
| 1176 | 11x |
if (missing(e)) {
|
| 1177 | 1x |
stop("must pass e", call. = FALSE)
|
| 1178 |
} |
|
| 1179 | 1x |
if(e$x$tl)( |
| 1180 | 1x |
stop("timeline not supported")
|
| 1181 |
) |
|
| 1182 | 9x |
e <- .rm_axis(e, rm_x, "x") |
| 1183 | 9x |
e <- .rm_axis(e, rm_y, "y") |
| 1184 | ||
| 1185 | 9x |
serie <- list( |
| 1186 | 9x |
name = name, |
| 1187 | 9x |
type = "graphGL", |
| 1188 | 9x |
layout = layout, |
| 1189 | 9x |
itemStyle = itemStyle, |
| 1190 |
... |
|
| 1191 |
) |
|
| 1192 | ||
| 1193 |
# add dependencies |
|
| 1194 | 9x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 1195 | 9x |
dep_gl <- htmltools::htmlDependency( |
| 1196 | 9x |
name = "echarts-gl", |
| 1197 | 9x |
version = "1.1.2", |
| 1198 | 9x |
src = c(file = path), |
| 1199 | 9x |
script = "echarts-gl.min.js" |
| 1200 |
) |
|
| 1201 | ||
| 1202 | 9x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 1203 | 9x |
dep_modularity <- htmltools::htmlDependency( |
| 1204 | 9x |
name = "echarts-graph-modularity", |
| 1205 | 9x |
version = "1.1.0", |
| 1206 | 9x |
src = c(file = path), |
| 1207 | 9x |
script = "echarts-graph-modularity.min.js" |
| 1208 |
) |
|
| 1209 | ||
| 1210 | 9x |
e$dependencies <- append(e$dependencies, list(dep_gl)) |
| 1211 | 9x |
e$dependencies <- append(e$dependencies, list(dep_modularity)) |
| 1212 | ||
| 1213 | 9x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 1214 | 9x |
e |
| 1215 |
} |
|
| 1216 | ||
| 1217 |
#' @export |
|
| 1218 |
#' @method e_graph_gl echarts4rProxy |
|
| 1219 |
e_graph_gl.echarts4rProxy <- function(e, layout = "force", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1220 | 2x |
e$chart <- e_graph_gl(e$chart, layout, name, rm_x, rm_y, ...) |
| 1221 | 2x |
return(e) |
| 1222 |
} |
|
| 1223 | ||
| 1224 |
#' @rdname graph |
|
| 1225 |
#' @export |
|
| 1226 | 13x |
e_graph_nodes <- function(e, nodes, names, value, size, category, symbol = NULL, legend = TRUE, xpos = NULL, ypos = NULL) UseMethod("e_graph_nodes")
|
| 1227 | ||
| 1228 |
#' @export |
|
| 1229 |
#' @method e_graph_nodes echarts4r |
|
| 1230 |
e_graph_nodes.echarts4r <- function(e, nodes, names, value, size, category, symbol = NULL, legend = TRUE, xpos = NULL, ypos = NULL) {
|
|
| 1231 | 13x |
if (missing(e)) {
|
| 1232 | 2x |
stop("must pass e", call. = FALSE)
|
| 1233 |
} |
|
| 1234 | 11x |
if (missing(nodes) || missing(names) || missing(value)) {
|
| 1235 | 2x |
stop("missing arguments", call. = FALSE)
|
| 1236 |
} |
|
| 1237 | ||
| 1238 | 9x |
value <- dplyr::enquo(value) |
| 1239 | 9x |
symbolSize <- dplyr::enquo(size) |
| 1240 | 9x |
symbol <- dplyr::enquo(symbol) |
| 1241 | 9x |
names <- dplyr::enquo(names) |
| 1242 | 9x |
xpos <- dplyr::enquo(xpos) |
| 1243 | 9x |
ypos <- dplyr::enquo(ypos) |
| 1244 | ||
| 1245 | 9x |
if (!missing(category) && !missing(size)) {
|
| 1246 | 6x |
e$x$opts$series[[length(e$x$opts$series)]]$categories <- .build_graph_category(nodes, dplyr::enquo(category)) |
| 1247 | ||
| 1248 | 6x |
if (isTRUE(legend)) {
|
| 1249 | 6x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, unique(nodes[[deparse(substitute(category))]])) |
| 1250 |
} |
|
| 1251 | ||
| 1252 | 6x |
nodes <- .build_graph_nodes( |
| 1253 | 6x |
nodes, |
| 1254 | 6x |
names, |
| 1255 | 6x |
value, |
| 1256 | 6x |
symbolSize, |
| 1257 | 6x |
dplyr::enquo(category), |
| 1258 | 6x |
symbol, |
| 1259 | 6x |
xpos, |
| 1260 | 6x |
ypos |
| 1261 |
) |
|
| 1262 | 3x |
} else if (missing(category) && !missing(size)) {
|
| 1263 | 2x |
nodes <- .build_graph_nodes_no_cat( |
| 1264 | 2x |
nodes, |
| 1265 | 2x |
names, |
| 1266 | 2x |
value, |
| 1267 | 2x |
symbolSize, |
| 1268 | 2x |
symbol, |
| 1269 | 2x |
xpos, |
| 1270 | 2x |
ypos |
| 1271 |
) |
|
| 1272 | 1x |
} else if (missing(category) && missing(size)) {
|
| 1273 | 1x |
nodes <- .build_graph_nodes_no_size( |
| 1274 | 1x |
nodes, |
| 1275 | 1x |
names, |
| 1276 | 1x |
value, |
| 1277 | 1x |
symbol, |
| 1278 | 1x |
xpos, |
| 1279 | 1x |
ypos |
| 1280 |
) |
|
| 1281 |
} |
|
| 1282 | ||
| 1283 |
# build JSON data |
|
| 1284 | 9x |
e$x$opts$series[[length(e$x$opts$series)]]$data <- nodes |
| 1285 | 9x |
e |
| 1286 |
} |
|
| 1287 | ||
| 1288 |
#' @export |
|
| 1289 |
#' @method e_graph_nodes echarts4rProxy |
|
| 1290 |
e_graph_nodes.echarts4rProxy <- function(e, nodes, names, value, size, category, symbol = NULL, legend = TRUE, xpos = NULL, ypos = NULL) {
|
|
| 1291 | ||
| 1292 | 4x |
if (missing(nodes) || missing(names) || missing(value)) {
|
| 1293 | 1x |
stop("missing arguments", call. = FALSE)
|
| 1294 |
} |
|
| 1295 | ||
| 1296 | 3x |
value <- dplyr::enquo(value) |
| 1297 | 3x |
symbolSize <- dplyr::enquo(size) |
| 1298 | 3x |
symbol <- dplyr::enquo(symbol) |
| 1299 | 3x |
names <- dplyr::enquo(names) |
| 1300 | 3x |
xpos <- dplyr::enquo(xpos) |
| 1301 | 3x |
ypos <- dplyr::enquo(ypos) |
| 1302 | ||
| 1303 | 3x |
if (!missing(category) && !missing(size)) {
|
| 1304 | 1x |
e$chart$x$opts$series[[length(e$chart$x$opts$series)]]$categories <- .build_graph_category(nodes, dplyr::enquo(category)) |
| 1305 | ||
| 1306 | 1x |
if (isTRUE(legend)) {
|
| 1307 | 1x |
e$chart$x$opts$legend$data <- append(e$chart$x$opts$legend$data, unique(nodes[[deparse(substitute(category))]])) |
| 1308 |
} |
|
| 1309 | ||
| 1310 | 1x |
nodes <- .build_graph_nodes( |
| 1311 | 1x |
nodes, |
| 1312 | 1x |
names, |
| 1313 | 1x |
value, |
| 1314 | 1x |
symbolSize, |
| 1315 | 1x |
symbol, |
| 1316 | 1x |
dplyr::enquo(category), |
| 1317 | 1x |
xpos, |
| 1318 | 1x |
ypos |
| 1319 |
) |
|
| 1320 | 2x |
} else if (missing(category) && !missing(size)) {
|
| 1321 | 1x |
nodes <- .build_graph_nodes_no_cat( |
| 1322 | 1x |
nodes, |
| 1323 | 1x |
names, |
| 1324 | 1x |
value, |
| 1325 | 1x |
symbolSize, |
| 1326 | 1x |
symbol, |
| 1327 | 1x |
xpos, |
| 1328 | 1x |
ypos |
| 1329 |
) |
|
| 1330 | 1x |
} else if (missing(category) && missing(size)) {
|
| 1331 | 1x |
nodes <- .build_graph_nodes_no_size( |
| 1332 | 1x |
nodes, |
| 1333 | 1x |
names, |
| 1334 | 1x |
value, |
| 1335 | 1x |
symbol, |
| 1336 | 1x |
xpos, |
| 1337 | 1x |
ypos |
| 1338 |
) |
|
| 1339 |
} |
|
| 1340 | ||
| 1341 |
# build JSON data |
|
| 1342 | 3x |
e$chart$x$opts$series[[length(e$chart$x$opts$series)]]$data <- nodes |
| 1343 | 3x |
e |
| 1344 |
} |
|
| 1345 | ||
| 1346 |
#' @rdname graph |
|
| 1347 |
#' @export |
|
| 1348 | 8x |
e_graph_edges <- function(e, edges, source, target, value, size, color) UseMethod("e_graph_edges")
|
| 1349 | ||
| 1350 |
#' @method e_graph_edges echarts4r |
|
| 1351 |
#' @export |
|
| 1352 |
e_graph_edges.echarts4r <- function(e, edges, source, target, value, size, color) {
|
|
| 1353 | 10x |
if (missing(e)) {
|
| 1354 | 2x |
stop("must pass e", call. = FALSE)
|
| 1355 |
} |
|
| 1356 | 8x |
if (missing(edges) || missing(source) || missing(target)) {
|
| 1357 | 2x |
stop("must pass edges, source and target", call. = FALSE)
|
| 1358 |
} |
|
| 1359 | ||
| 1360 | 6x |
if (missing(size)) {
|
| 1361 | 4x |
size <- NULL |
| 1362 |
} |
|
| 1363 | ||
| 1364 | 6x |
if (missing(value)) {
|
| 1365 | 6x |
value <- NULL |
| 1366 |
} |
|
| 1367 | ||
| 1368 | 6x |
if (missing(color)) {
|
| 1369 | 4x |
color <- NULL |
| 1370 |
} |
|
| 1371 | ||
| 1372 | 6x |
source <- dplyr::enquo(source) |
| 1373 | 6x |
target <- dplyr::enquo(target) |
| 1374 | 6x |
value <- dplyr::enquo(value) |
| 1375 | 6x |
size <- dplyr::enquo(size) |
| 1376 | 6x |
color <- dplyr::enquo(color) |
| 1377 | ||
| 1378 | 6x |
data <- .build_graph_edges( |
| 1379 | 6x |
edges, |
| 1380 | 6x |
source, |
| 1381 | 6x |
target, |
| 1382 | 6x |
value, |
| 1383 | 6x |
size, |
| 1384 | 6x |
color |
| 1385 |
) |
|
| 1386 | ||
| 1387 |
# build JSON data |
|
| 1388 | 6x |
e$x$opts$series[[length(e$x$opts$series)]]$links <- data |
| 1389 | 6x |
e |
| 1390 |
} |
|
| 1391 | ||
| 1392 | ||
| 1393 |
#' @method e_graph_edges echarts4rProxy |
|
| 1394 |
#' @export |
|
| 1395 |
e_graph_edges.echarts4rProxy <- function(e, edges, source, target, value, size, color) {
|
|
| 1396 | ||
| 1397 | 2x |
if (missing(edges) || missing(source) || missing(target)) {
|
| 1398 | 1x |
stop("must pass edges, source and target", call. = FALSE)
|
| 1399 |
} |
|
| 1400 | ||
| 1401 | 1x |
if (missing(size)) {
|
| 1402 | ! |
size <- NULL |
| 1403 |
} |
|
| 1404 | ||
| 1405 | 1x |
if (missing(color)) {
|
| 1406 | 1x |
color <- NULL |
| 1407 |
} |
|
| 1408 | ||
| 1409 | 1x |
source <- dplyr::enquo(source) |
| 1410 | 1x |
target <- dplyr::enquo(target) |
| 1411 | 1x |
value <- dplyr::enquo(value) |
| 1412 | 1x |
size <- dplyr::enquo(size) |
| 1413 | 1x |
color <- dplyr::enquo(color) |
| 1414 | ||
| 1415 | 1x |
data <- .build_graph_edges( |
| 1416 | 1x |
edges, |
| 1417 | 1x |
source, |
| 1418 | 1x |
target, |
| 1419 | 1x |
value, |
| 1420 | 1x |
size, |
| 1421 | 1x |
color |
| 1422 |
) |
|
| 1423 | ||
| 1424 |
# build JSON data |
|
| 1425 | 1x |
e$chart$x$opts$series[[length(e$chart$x$opts$series)]]$links <- data |
| 1426 | 1x |
e |
| 1427 |
} |
|
| 1428 | ||
| 1429 | ||
| 1430 |
#' Heatmap |
|
| 1431 |
#' |
|
| 1432 |
#' Draw heatmap by coordinates. |
|
| 1433 |
#' |
|
| 1434 |
#' @inheritParams e_bar |
|
| 1435 |
#' @param y,z Coordinates and values. |
|
| 1436 |
#' @param coord_system Coordinate system to plot against, takes |
|
| 1437 |
#' \code{cartesian2d}, \code{geo} or \code{calendar}.
|
|
| 1438 |
#' @param rm_x,rm_y Whether to remove x and y axis, only applies if \code{coord_system} is not
|
|
| 1439 |
#' set to \code{cartesian2d}.
|
|
| 1440 |
#' @param calendar The index of the calendar to plot against. |
|
| 1441 |
#' |
|
| 1442 |
#' @examples |
|
| 1443 |
#' v <- LETTERS[1:10] |
|
| 1444 |
#' matrix <- data.frame( |
|
| 1445 |
#' x = sample(v, 300, replace = TRUE), |
|
| 1446 |
#' y = sample(v, 300, replace = TRUE), |
|
| 1447 |
#' z = rnorm(300, 10, 1), |
|
| 1448 |
#' stringsAsFactors = FALSE |
|
| 1449 |
#' ) |> |
|
| 1450 |
#' dplyr::group_by(x, y) |> |
|
| 1451 |
#' dplyr::summarise(z = sum(z)) |> |
|
| 1452 |
#' dplyr::ungroup() |
|
| 1453 |
#' |
|
| 1454 |
#' matrix |> |
|
| 1455 |
#' e_charts(x) |> |
|
| 1456 |
#' e_heatmap(y, z, itemStyle = list(emphasis = list(shadowBlur = 10))) |> |
|
| 1457 |
#' e_visual_map(z) |
|
| 1458 |
#' |
|
| 1459 |
#' # calendar |
|
| 1460 |
#' dates <- seq.Date(as.Date("2017-01-01"), as.Date("2018-12-31"), by = "day")
|
|
| 1461 |
#' values <- rnorm(length(dates), 20, 6) |
|
| 1462 |
#' |
|
| 1463 |
#' year <- data.frame(date = dates, values = values) |
|
| 1464 |
#' |
|
| 1465 |
#' year |> |
|
| 1466 |
#' e_charts(date) |> |
|
| 1467 |
#' e_calendar(range = "2018") |> |
|
| 1468 |
#' e_heatmap(values, coord_system = "calendar") |> |
|
| 1469 |
#' e_visual_map(max = 30) |
|
| 1470 |
#' |
|
| 1471 |
#' # calendar multiple years |
|
| 1472 |
#' year |> |
|
| 1473 |
#' dplyr::mutate(year = format(date, "%Y")) |> |
|
| 1474 |
#' group_by(year) |> |
|
| 1475 |
#' e_charts(date) |> |
|
| 1476 |
#' e_calendar(range = "2017", top = 40) |> |
|
| 1477 |
#' e_calendar(range = "2018", top = 260) |> |
|
| 1478 |
#' e_heatmap(values, coord_system = "calendar") |> |
|
| 1479 |
#' e_visual_map(max = 30) |
|
| 1480 |
#' |
|
| 1481 |
#' # map |
|
| 1482 |
#' quakes |> |
|
| 1483 |
#' e_charts(long) |> |
|
| 1484 |
#' e_geo( |
|
| 1485 |
#' boundingCoords = list( |
|
| 1486 |
#' c(190, -10), |
|
| 1487 |
#' c(180, -40) |
|
| 1488 |
#' ) |
|
| 1489 |
#' ) |> |
|
| 1490 |
#' e_heatmap( |
|
| 1491 |
#' lat, |
|
| 1492 |
#' mag, |
|
| 1493 |
#' coord_system = "geo", |
|
| 1494 |
#' blurSize = 5, |
|
| 1495 |
#' pointSize = 3 |
|
| 1496 |
#' ) |> |
|
| 1497 |
#' e_visual_map(mag) |
|
| 1498 |
#' |
|
| 1499 |
#' # timeline |
|
| 1500 |
#' library(dplyr) |
|
| 1501 |
#' |
|
| 1502 |
#' axis <- LETTERS[1:10] |
|
| 1503 |
#' df <- expand.grid(axis, axis) |
|
| 1504 |
#' |
|
| 1505 |
#' bind_rows(df, df) |> |
|
| 1506 |
#' mutate( |
|
| 1507 |
#' values = runif(n(), 1, 10), |
|
| 1508 |
#' grp = c( |
|
| 1509 |
#' rep("A", 100),
|
|
| 1510 |
#' rep("B", 100)
|
|
| 1511 |
#' ) |
|
| 1512 |
#' ) |> |
|
| 1513 |
#' group_by(grp) |> |
|
| 1514 |
#' e_charts(Var1, timeline = TRUE) |> |
|
| 1515 |
#' e_heatmap(Var2, values) |> |
|
| 1516 |
#' e_visual_map(values) |
|
| 1517 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-heatmap}{Additional arguments}
|
|
| 1518 |
#' |
|
| 1519 |
#' @rdname e_heatmap |
|
| 1520 |
#' @export |
|
| 1521 |
e_heatmap <- function( |
|
| 1522 |
e, |
|
| 1523 |
y, |
|
| 1524 |
z, |
|
| 1525 |
bind, |
|
| 1526 |
name = NULL, |
|
| 1527 |
coord_system = "cartesian2d", |
|
| 1528 |
rm_x = TRUE, |
|
| 1529 |
rm_y = TRUE, |
|
| 1530 |
calendar = NULL, |
|
| 1531 |
...) {
|
|
| 1532 | 8x |
UseMethod("e_heatmap")
|
| 1533 |
} |
|
| 1534 | ||
| 1535 |
#' @export |
|
| 1536 |
#' @method e_heatmap echarts4r |
|
| 1537 |
e_heatmap.echarts4r <- function( |
|
| 1538 |
e, |
|
| 1539 |
y, |
|
| 1540 |
z, |
|
| 1541 |
bind, |
|
| 1542 |
name = NULL, |
|
| 1543 |
coord_system = "cartesian2d", |
|
| 1544 |
rm_x = TRUE, |
|
| 1545 |
rm_y = TRUE, |
|
| 1546 |
calendar = NULL, |
|
| 1547 |
...) {
|
|
| 1548 | 10x |
if (missing(e)) {
|
| 1549 | 2x |
stop("must pass e", call. = FALSE)
|
| 1550 |
} |
|
| 1551 | ||
| 1552 | 8x |
if (missing(y)) {
|
| 1553 | 2x |
stop("must pass y", call. = FALSE)
|
| 1554 |
} |
|
| 1555 | ||
| 1556 | 6x |
if (!missing(z)) {
|
| 1557 | 4x |
z <- deparse(substitute(z)) |
| 1558 |
} else {
|
|
| 1559 | 2x |
z <- NULL |
| 1560 |
} |
|
| 1561 | ||
| 1562 | 6x |
bd <- .get_bind(deparse(substitute(bind))) |
| 1563 | ||
| 1564 | 6x |
e_heatmap_(e, deparse(substitute(y)), z, bd, name, coord_system, rm_x, rm_y, calendar, ...) |
| 1565 |
} |
|
| 1566 | ||
| 1567 |
#' @export |
|
| 1568 |
#' @method e_heatmap echarts4rProxy |
|
| 1569 |
e_heatmap.echarts4rProxy <- function( |
|
| 1570 |
e, |
|
| 1571 |
y, |
|
| 1572 |
z, |
|
| 1573 |
bind, |
|
| 1574 |
name = NULL, |
|
| 1575 |
coord_system = "cartesian2d", |
|
| 1576 |
rm_x = TRUE, |
|
| 1577 |
rm_y = TRUE, |
|
| 1578 |
calendar = NULL, |
|
| 1579 |
...) {
|
|
| 1580 | ||
| 1581 | 2x |
if (missing(y)) {
|
| 1582 | 1x |
stop("must pass y", call. = FALSE)
|
| 1583 |
} |
|
| 1584 | ||
| 1585 | 1x |
if(is.null(e$chart$x$mapping$x)){
|
| 1586 | ! |
stop("provide x in echarts4rProxy")
|
| 1587 |
} |
|
| 1588 | ||
| 1589 | 1x |
if (!missing(z)) {
|
| 1590 | 1x |
z <- deparse(substitute(z)) |
| 1591 |
} else {
|
|
| 1592 | ! |
z <- NULL |
| 1593 |
} |
|
| 1594 | ||
| 1595 | 1x |
if (!missing(bind)) {
|
| 1596 | ! |
bind <- deparse(substitute(bind)) |
| 1597 |
} else {
|
|
| 1598 | 1x |
bind <- NULL |
| 1599 |
} |
|
| 1600 | ||
| 1601 | 1x |
e$chart <- e_heatmap_(e$chart, deparse(substitute(y)), z, bind, name, coord_system, rm_x, rm_y, calendar, ...) |
| 1602 | 1x |
return(e) |
| 1603 |
} |
|
| 1604 | ||
| 1605 |
#' Parallel |
|
| 1606 |
#' |
|
| 1607 |
#' Draw parallel coordinates. |
|
| 1608 |
#' |
|
| 1609 |
#' @inheritParams e_bar |
|
| 1610 |
#' @param ... Columns to select from the data passed to \code{\link{e_charts}}.
|
|
| 1611 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 1612 |
#' @param opts A list of additional options to pass to the serie. |
|
| 1613 |
#' |
|
| 1614 |
#' @examples |
|
| 1615 |
#' df <- data.frame( |
|
| 1616 |
#' price = rnorm(5, 10), |
|
| 1617 |
#' amount = rnorm(5, 15), |
|
| 1618 |
#' letter = LETTERS[1:5] |
|
| 1619 |
#' ) |
|
| 1620 |
#' |
|
| 1621 |
#' df |> |
|
| 1622 |
#' e_charts() |> |
|
| 1623 |
#' e_parallel(price, amount, letter, opts = list(smooth = TRUE)) |
|
| 1624 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-parallel}{Additional arguments}
|
|
| 1625 |
#' |
|
| 1626 |
#' @rdname e_parallel |
|
| 1627 |
#' @export |
|
| 1628 |
e_parallel <- function(e, ..., name = NULL, rm_x = TRUE, rm_y = TRUE, opts = list()) {
|
|
| 1629 | 5x |
UseMethod("e_parallel")}
|
| 1630 | ||
| 1631 |
#' @export |
|
| 1632 |
#' @method e_parallel echarts4r |
|
| 1633 |
e_parallel.echarts4r <- function(e, ..., name = NULL, rm_x = TRUE, rm_y = TRUE, opts = list()) {
|
|
| 1634 | 5x |
if (missing(e)) {
|
| 1635 | 1x |
stop("must pass e", call. = FALSE)
|
| 1636 |
} |
|
| 1637 | 1x |
if(e$x$tl)( |
| 1638 | 1x |
stop("timeline not supported")
|
| 1639 |
) |
|
| 1640 | ||
| 1641 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 1642 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 1643 | ||
| 1644 | 3x |
data <- e$x$data[[1]] |> |
| 1645 | 3x |
dplyr::select(...) |
| 1646 | ||
| 1647 |
# remove names |
|
| 1648 | 3x |
df <- data |
| 1649 | 3x |
row.names(data) <- NULL |
| 1650 | 3x |
data <- unname(data) |
| 1651 | ||
| 1652 | 3x |
data <- apply(data, 1, as.list) |
| 1653 | ||
| 1654 |
# remove white spaces (ex: in " 1") |
|
| 1655 | 3x |
data <- purrr::map(data, function(x) {
|
| 1656 | 13x |
gsub(" ", "", x) |>
|
| 1657 | 13x |
as.list() |
| 1658 |
}) |
|
| 1659 | ||
| 1660 | 3x |
serie <- list( |
| 1661 | 3x |
name = name, |
| 1662 | 3x |
type = "parallel", |
| 1663 | 3x |
data = data |
| 1664 |
) |
|
| 1665 | ||
| 1666 | 3x |
serie <- append(serie, opts) |
| 1667 | ||
| 1668 | 3x |
para <- list() |
| 1669 | 3x |
for (i in seq_len(ncol(df))) {
|
| 1670 | 8x |
line <- list() |
| 1671 | 8x |
line$dim <- i - 1 |
| 1672 | 8x |
line$name <- names(df)[i] |
| 1673 | 8x |
if (inherits(df[, i], "character") || inherits(df[, i], "factor")) {
|
| 1674 | 3x |
line$type <- "category" |
| 1675 | 3x |
line$data <- unique(df[, i]) |
| 1676 |
} |
|
| 1677 | ||
| 1678 | 8x |
para[[i]] <- line |
| 1679 |
} |
|
| 1680 | ||
| 1681 | 3x |
e$x$opts$series <- append(e$x$opts$series, serie) |
| 1682 | 3x |
e$x$opts$parallelAxis <- para |
| 1683 | 3x |
e |
| 1684 |
} |
|
| 1685 | ||
| 1686 |
#' @export |
|
| 1687 |
#' @method e_parallel echarts4rProxy |
|
| 1688 |
e_parallel.echarts4rProxy <- function(e, ..., name = NULL, rm_x = TRUE, rm_y = TRUE) {
|
|
| 1689 | 1x |
e$chart <- e_parallel(e$chart, ..., name = name, rm_x = rm_x, rm_y = rm_y) |
| 1690 | 1x |
return(e) |
| 1691 |
} |
|
| 1692 | ||
| 1693 |
#' Pie |
|
| 1694 |
#' |
|
| 1695 |
#' Draw pie and donut charts. |
|
| 1696 |
#' |
|
| 1697 |
#' @inheritParams e_bar |
|
| 1698 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 1699 |
#' |
|
| 1700 |
#' @examples |
|
| 1701 |
#' mtcars |> |
|
| 1702 |
#' head() |> |
|
| 1703 |
#' tibble::rownames_to_column("model") |>
|
|
| 1704 |
#' e_charts(model) |> |
|
| 1705 |
#' e_pie(carb) |
|
| 1706 |
#' |
|
| 1707 |
#' # timeline |
|
| 1708 |
#' df <- data.frame( |
|
| 1709 |
#' grp = c("A", "A", "A", "B", "B", "B"),
|
|
| 1710 |
#' labels = rep(LETTERS[1:3], 2), |
|
| 1711 |
#' values = runif(6, 1, 5) |
|
| 1712 |
#' ) |
|
| 1713 |
#' |
|
| 1714 |
#' df |> |
|
| 1715 |
#' group_by(grp) |> |
|
| 1716 |
#' e_charts(labels, timeline = TRUE) |> |
|
| 1717 |
#' e_pie(values) |
|
| 1718 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-pie}{Additional arguments}
|
|
| 1719 |
#' |
|
| 1720 |
#' @rdname e_pie |
|
| 1721 |
#' @export |
|
| 1722 | 9x |
e_pie <- function(e, serie, name = NULL, legend = TRUE, coord_system = "", rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_pie")
|
| 1723 | ||
| 1724 |
#' @export |
|
| 1725 |
#' @method e_pie echarts4r |
|
| 1726 |
e_pie.echarts4r <- function(e, serie, name = NULL, legend = TRUE, coord_system = "", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1727 | 9x |
if (missing(e)) {
|
| 1728 | 1x |
stop("must pass e", call. = FALSE)
|
| 1729 |
} |
|
| 1730 | 8x |
if (missing(serie)) {
|
| 1731 | 1x |
stop("must pass serie", call. = FALSE)
|
| 1732 |
} |
|
| 1733 | ||
| 1734 | 7x |
e_pie_(e, deparse(substitute(serie)), name, legend, coord_system, rm_x, rm_y, ...) |
| 1735 |
} |
|
| 1736 | ||
| 1737 |
#' @export |
|
| 1738 |
#' @method e_pie echarts4rProxy |
|
| 1739 |
e_pie.echarts4rProxy <- function(e, serie, name = NULL, legend = TRUE, coord_system = "", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1740 | ||
| 1741 | 2x |
if (missing(serie)) {
|
| 1742 | 1x |
stop("must pass serie", call. = FALSE)
|
| 1743 |
} |
|
| 1744 | ||
| 1745 | 1x |
if(is.null(e$chart$x$mapping$x)){
|
| 1746 | ! |
stop("provide x in echarts4rProxy")
|
| 1747 |
} |
|
| 1748 | ||
| 1749 | 1x |
e$chart <- e_pie_(e$chart, deparse(substitute(serie)), name, legend, coord_system, rm_x, rm_y, ...) |
| 1750 | 1x |
return(e) |
| 1751 |
} |
|
| 1752 | ||
| 1753 |
#' Sunburst |
|
| 1754 |
#' |
|
| 1755 |
#' Build a sunburst. |
|
| 1756 |
#' |
|
| 1757 |
#' @inheritParams e_bar |
|
| 1758 |
#' @param styles Vector of style lists, defaults to \code{NULL}.
|
|
| 1759 |
#' @param names Names of items to style, expects a \code{list}, defaults to \code{NULL}.
|
|
| 1760 |
#' @param levels Hierarchical levels to style, expects a \code{list}, defaults to \code{NULL}.
|
|
| 1761 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 1762 |
#' |
|
| 1763 |
#' @details |
|
| 1764 |
#' Charts e_sunburst, e_treemap and e_tree require hierarchical input data. |
|
| 1765 |
#' Such structure could be represented thru json lists or nested tibbles (data.frame). |
|
| 1766 |
#' Input data may contain styles, see \code{itemStyle} in examples \code{jsonl} and \code{df} below.
|
|
| 1767 |
#' The number of lists in the \code{styles} parameter should match the number of elements
|
|
| 1768 |
#' in \code{names} and/or \code{levels}. If both \code{names} and \code{levels} are present,
|
|
| 1769 |
#' name styles will take precedence over level styles. |
|
| 1770 |
#' Multiple names may have the same style, see \code{c('land','river')} below.
|
|
| 1771 |
#' Multiple levels may have the same style, see \code{c(3,4)} below.
|
|
| 1772 |
#' \code{styles} lists contain items such as \code{color}, or \code{borderColor} as specified in the
|
|
| 1773 |
#' \href{https://echarts.apache.org/en/option.html#series-sunburst.data.itemStyle}{official documentation}.
|
|
| 1774 |
#' |
|
| 1775 |
#' @examples |
|
| 1776 |
#' |
|
| 1777 |
#' # json list hierarchical data representation |
|
| 1778 |
#' jsonl <- jsonlite::fromJSON('[
|
|
| 1779 |
#' {"name": "earth", "value": 30,
|
|
| 1780 |
#' "children": [ |
|
| 1781 |
#' {"name": "land", "value":10,
|
|
| 1782 |
#' "children": [ |
|
| 1783 |
#' {"name": "forest", "value": 3},
|
|
| 1784 |
#' {"name": "river", "value": 7}
|
|
| 1785 |
#' ]}, |
|
| 1786 |
#' {"name": "ocean", "value":20,
|
|
| 1787 |
#' "children": [ |
|
| 1788 |
#' {"name": "fish", "value": 10,
|
|
| 1789 |
#' "children": [ |
|
| 1790 |
#' {"name": "shark", "value":2},
|
|
| 1791 |
#' {"name": "tuna", "value":6}
|
|
| 1792 |
#' ]}, |
|
| 1793 |
#' {"name": "kelp", "value": 5}
|
|
| 1794 |
#' ]} |
|
| 1795 |
#' ] |
|
| 1796 |
#' }, |
|
| 1797 |
#' {"name": "mars", "value": 30,
|
|
| 1798 |
#' "children": [ |
|
| 1799 |
#' {"name": "crater", "value": 20},
|
|
| 1800 |
#' {"name": "valley", "value": 20}
|
|
| 1801 |
#' ]}, |
|
| 1802 |
#' {"name": "venus", "value": 40, "itemStyle": {"color": "blue"} }
|
|
| 1803 |
#' ]', simplifyDataFrame = FALSE) |
|
| 1804 |
#' |
|
| 1805 |
#' jsonl |> |
|
| 1806 |
#' e_charts() |> |
|
| 1807 |
#' e_sunburst() # demo |
|
| 1808 |
#' |
|
| 1809 |
#' |
|
| 1810 |
#' # tibble hierarchical data representation |
|
| 1811 |
#' library(dplyr) |
|
| 1812 |
#' df <- tibble( |
|
| 1813 |
#' name = c("earth", "mars", "venus"),
|
|
| 1814 |
#' value = c(30, 40, 30), |
|
| 1815 |
#' # 1st level |
|
| 1816 |
#' itemStyle = tibble(color = c(NA, "red", "blue")), |
|
| 1817 |
#' # embedded styles, optional |
|
| 1818 |
#' children = list( |
|
| 1819 |
#' tibble( |
|
| 1820 |
#' name = c("land", "ocean"),
|
|
| 1821 |
#' value = c(10, 20), |
|
| 1822 |
#' # 2nd level |
|
| 1823 |
#' children = list( |
|
| 1824 |
#' tibble(name = c("forest", "river"), value = c(3, 7)),
|
|
| 1825 |
#' # 3rd level |
|
| 1826 |
#' tibble( |
|
| 1827 |
#' name = c("fish", "kelp"),
|
|
| 1828 |
#' value = c(10, 5), |
|
| 1829 |
#' children = list( |
|
| 1830 |
#' tibble(name = c("shark", "tuna"), value = c(2, 6)),
|
|
| 1831 |
#' # 4th level |
|
| 1832 |
#' NULL # kelp |
|
| 1833 |
#' ) |
|
| 1834 |
#' ) |
|
| 1835 |
#' ) |
|
| 1836 |
#' ), |
|
| 1837 |
#' tibble(name = c("crater", "valley"), value = c(20, 20)),
|
|
| 1838 |
#' NULL # venus |
|
| 1839 |
#' ) |
|
| 1840 |
#' ) |
|
| 1841 |
#' |
|
| 1842 |
#' df |> |
|
| 1843 |
#' e_charts() |> |
|
| 1844 |
#' e_sunburst() |> |
|
| 1845 |
#' e_theme("westeros")
|
|
| 1846 |
#' |
|
| 1847 |
#' # with styles |
|
| 1848 |
#' myStyles <- c(list(color = "green"), list(color = "magenta")) # custom styles defined |
|
| 1849 |
#' myNames <- list(c("land", "river"), "crater") # names to style
|
|
| 1850 |
#' myLevels <- list(2, c(3, 4)) # hierarchical levels to style |
|
| 1851 |
#' |
|
| 1852 |
#' df |> |
|
| 1853 |
#' e_charts() |> |
|
| 1854 |
#' e_sunburst(myStyles, myNames, myLevels) |
|
| 1855 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-sunburst}{Additional arguments}
|
|
| 1856 |
#' |
|
| 1857 |
#' @rdname e_sunburst |
|
| 1858 |
#' @export |
|
| 1859 |
e_sunburst <- function(e, styles = NULL, names = NULL, levels = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1860 | 5x |
UseMethod("e_sunburst")}
|
| 1861 | ||
| 1862 |
#' @export |
|
| 1863 |
#' @method e_sunburst echarts4r |
|
| 1864 |
e_sunburst.echarts4r <- function(e, styles = NULL, names = NULL, levels = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1865 | 5x |
if (missing(e)) {
|
| 1866 | 1x |
stop("must pass e", call. = FALSE)
|
| 1867 |
} |
|
| 1868 | 4x |
e_sunburst_(e, styles, names, levels, rm_x, rm_y, ...) |
| 1869 |
} |
|
| 1870 | ||
| 1871 |
#' @export |
|
| 1872 |
#' @method e_sunburst echarts4rProxy |
|
| 1873 |
e_sunburst.echarts4rProxy <- function(e, styles = NULL, names = NULL, levels = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1874 | 1x |
e$chart <- e_sunburst_(e$chart, styles, names, levels, rm_x, rm_y, ...) |
| 1875 | ||
| 1876 | 1x |
return(e) |
| 1877 |
} |
|
| 1878 | ||
| 1879 |
#' Treemap |
|
| 1880 |
#' |
|
| 1881 |
#' Build a treemap. |
|
| 1882 |
#' |
|
| 1883 |
#' @inheritParams e_bar |
|
| 1884 |
#' @param styles Vector of style lists, defaults to \code{NULL}.
|
|
| 1885 |
#' @param names Names of items to style, expects a \code{list}, defaults to \code{NULL}.
|
|
| 1886 |
#' @param levels Hierarchical levels to style, expects a \code{list}, defaults to \code{NULL}.
|
|
| 1887 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 1888 |
#' |
|
| 1889 |
#' @examples |
|
| 1890 |
#' library(dplyr) |
|
| 1891 |
#' df <- tibble( |
|
| 1892 |
#' name = c("earth", "mars", "venus"),
|
|
| 1893 |
#' value = c(30, 40, 30), |
|
| 1894 |
#' # 1st level |
|
| 1895 |
#' itemStyle = tibble(color = c(NA, "red", "blue")), |
|
| 1896 |
#' # embedded styles, optional |
|
| 1897 |
#' children = list( |
|
| 1898 |
#' tibble( |
|
| 1899 |
#' name = c("land", "ocean"),
|
|
| 1900 |
#' value = c(10, 20), |
|
| 1901 |
#' # 2nd level |
|
| 1902 |
#' children = list( |
|
| 1903 |
#' tibble(name = c("forest", "river"), value = c(3, 7)),
|
|
| 1904 |
#' # 3rd level |
|
| 1905 |
#' tibble( |
|
| 1906 |
#' name = c("fish", "kelp"),
|
|
| 1907 |
#' value = c(10, 5), |
|
| 1908 |
#' children = list( |
|
| 1909 |
#' tibble(name = c("shark", "tuna"), value = c(2, 6)),
|
|
| 1910 |
#' # 4th level |
|
| 1911 |
#' NULL # kelp |
|
| 1912 |
#' ) |
|
| 1913 |
#' ) |
|
| 1914 |
#' ) |
|
| 1915 |
#' ), |
|
| 1916 |
#' tibble(name = c("crater", "valley"), value = c(20, 20)),
|
|
| 1917 |
#' NULL # venus |
|
| 1918 |
#' ) |
|
| 1919 |
#' ) |
|
| 1920 |
#' |
|
| 1921 |
#' df |> |
|
| 1922 |
#' e_charts() |> |
|
| 1923 |
#' e_treemap() |
|
| 1924 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-treemap}{Additional arguments}
|
|
| 1925 |
#' |
|
| 1926 |
#' @rdname e_treemap |
|
| 1927 |
#' @export |
|
| 1928 |
e_treemap <- function(e, styles = NULL, names = NULL, levels = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1929 | 4x |
UseMethod("e_treemap")}
|
| 1930 | ||
| 1931 |
#' @export |
|
| 1932 |
#' @method e_treemap echarts4r |
|
| 1933 |
e_treemap.echarts4r <- function(e, styles = NULL, names = NULL, levels = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1934 | 4x |
if (missing(e)) {
|
| 1935 | 1x |
stop("must pass e", call. = FALSE)
|
| 1936 |
} |
|
| 1937 | 3x |
e_treemap_(e, styles, names, levels, rm_x, rm_y, ...) |
| 1938 |
} |
|
| 1939 | ||
| 1940 |
#' @export |
|
| 1941 |
#' @method e_treemap echarts4rProxy |
|
| 1942 |
e_treemap.echarts4rProxy <- function(e, styles = NULL, names = NULL, levels = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1943 | 1x |
e$chart <- e_treemap_(e$chart, styles, names, levels, rm_x, rm_y, ...) |
| 1944 | ||
| 1945 | 1x |
return(e) |
| 1946 |
} |
|
| 1947 | ||
| 1948 |
#' River |
|
| 1949 |
#' |
|
| 1950 |
#' Build a theme river. |
|
| 1951 |
#' |
|
| 1952 |
#' @inheritParams e_bar |
|
| 1953 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 1954 |
#' |
|
| 1955 |
#' @examples |
|
| 1956 |
#' dates <- seq.Date(Sys.Date() - 30, Sys.Date(), by = "day") |
|
| 1957 |
#' grps <- lapply(LETTERS[1:3], rep, 31) |> unlist() |
|
| 1958 |
#' |
|
| 1959 |
#' df <- data.frame( |
|
| 1960 |
#' dates = rep(dates, 3), |
|
| 1961 |
#' groups = grps, |
|
| 1962 |
#' values = runif(length(grps), 1, 50) |
|
| 1963 |
#' ) |
|
| 1964 |
#' |
|
| 1965 |
#' df |> |
|
| 1966 |
#' group_by(groups) |> |
|
| 1967 |
#' e_charts(dates) |> |
|
| 1968 |
#' e_river(values) |> |
|
| 1969 |
#' e_tooltip(trigger = "axis") |
|
| 1970 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-themeRiver}{Additional arguments}
|
|
| 1971 |
#' |
|
| 1972 |
#' @rdname e_river |
|
| 1973 |
#' @export |
|
| 1974 |
e_river <- function(e, serie, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1975 | 7x |
UseMethod("e_river")
|
| 1976 |
} |
|
| 1977 | ||
| 1978 |
#' @export |
|
| 1979 |
#' @method e_river echarts4r |
|
| 1980 |
e_river.echarts4r <- function(e, serie, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1981 | 6x |
if (missing(e)) {
|
| 1982 | 1x |
stop("must pass e", call. = FALSE)
|
| 1983 |
} |
|
| 1984 | 5x |
if (missing(serie)) {
|
| 1985 | 1x |
stop("must pass serie", call. = FALSE)
|
| 1986 |
} |
|
| 1987 | ||
| 1988 | 4x |
e_river_(e, deparse(substitute(serie)), name, legend, rm_x, rm_y, ...) |
| 1989 |
} |
|
| 1990 | ||
| 1991 |
#' @export |
|
| 1992 |
#' @method e_river echarts4rProxy |
|
| 1993 |
e_river.echarts4rProxy <- function(e, serie, name = NULL, legend = TRUE, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 1994 | 3x |
if (missing(serie)) {
|
| 1995 | 1x |
stop("must pass serie", call. = FALSE)
|
| 1996 |
} |
|
| 1997 | ||
| 1998 | 2x |
if(is.null(e$chart$x$mapping$x)){
|
| 1999 | ! |
stop("provide x in echarts4rProxy")
|
| 2000 |
} |
|
| 2001 | ||
| 2002 | 2x |
e$chart <- e_river_(e$chart, deparse(substitute(serie)), name, legend, rm_x, rm_y, ...) |
| 2003 | 2x |
return(e) |
| 2004 |
} |
|
| 2005 | ||
| 2006 |
#' Boxplot |
|
| 2007 |
#' |
|
| 2008 |
#' Draw boxplot. |
|
| 2009 |
#' |
|
| 2010 |
#' @inheritParams e_bar |
|
| 2011 |
#' @param outliers Whether to plot outliers. |
|
| 2012 |
# |
|
| 2013 |
#' |
|
| 2014 |
#' @examples |
|
| 2015 |
#' df <- data.frame( |
|
| 2016 |
#' x = c(1:10, 25), |
|
| 2017 |
#' y = c(1:10, -6) |
|
| 2018 |
#' ) |
|
| 2019 |
#' |
|
| 2020 |
#' df |> |
|
| 2021 |
#' e_charts() |> |
|
| 2022 |
#' e_boxplot(y, outliers = TRUE) |> |
|
| 2023 |
#' e_boxplot(x, outliers = TRUE) |
|
| 2024 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-boxplot}{Additional arguments}
|
|
| 2025 |
#' |
|
| 2026 |
#' @rdname e_boxplot |
|
| 2027 |
#' @export |
|
| 2028 |
e_boxplot <- function(e, serie, name = NULL, outliers = TRUE, ...) {
|
|
| 2029 | 4x |
UseMethod("e_boxplot")
|
| 2030 |
} |
|
| 2031 | ||
| 2032 |
#' @export |
|
| 2033 |
#' @method e_boxplot echarts4r |
|
| 2034 |
e_boxplot.echarts4r <- function(e, serie, name = NULL, outliers = TRUE, ...) {
|
|
| 2035 | 4x |
if (missing(e)) {
|
| 2036 | 1x |
stop("must pass e", call. = FALSE)
|
| 2037 |
} |
|
| 2038 | ||
| 2039 | 3x |
if (missing(serie)) {
|
| 2040 | 1x |
stop("must pass serie", call. = FALSE)
|
| 2041 |
} |
|
| 2042 | ||
| 2043 | 2x |
if (is.null(name)) { # defaults to column name
|
| 2044 | 2x |
name <- deparse(substitute(serie)) |
| 2045 |
} |
|
| 2046 | ||
| 2047 | 2x |
e_boxplot_(e, deparse(substitute(serie)), name, outliers, ...) |
| 2048 |
} |
|
| 2049 | ||
| 2050 |
#' @export |
|
| 2051 |
#' @method e_boxplot echarts4rProxy |
|
| 2052 |
e_boxplot.echarts4rProxy <- function(e, serie, name = NULL, outliers = TRUE, ...) {
|
|
| 2053 | 2x |
if (missing(serie)) {
|
| 2054 | 1x |
stop("must pass serie", call. = FALSE)
|
| 2055 |
} |
|
| 2056 | ||
| 2057 | 1x |
if (is.null(name)) { # defaults to column name
|
| 2058 | 1x |
name <- deparse(substitute(serie)) |
| 2059 |
} |
|
| 2060 | ||
| 2061 | 1x |
e$chart <- e_boxplot_(e$chart, deparse(substitute(serie)), name, outliers, ...) |
| 2062 | 1x |
return(e) |
| 2063 |
} |
|
| 2064 | ||
| 2065 |
#' Tree |
|
| 2066 |
#' |
|
| 2067 |
#' Build a tree. |
|
| 2068 |
#' |
|
| 2069 |
#' @inheritParams e_bar |
|
| 2070 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 2071 |
#' |
|
| 2072 |
#' @examples |
|
| 2073 |
#' library(dplyr) |
|
| 2074 |
#' df <- tibble( |
|
| 2075 |
#' name = "earth", |
|
| 2076 |
#' # 1st level |
|
| 2077 |
#' children = list( |
|
| 2078 |
#' tibble( |
|
| 2079 |
#' name = c("land", "ocean"),
|
|
| 2080 |
#' # 2nd level |
|
| 2081 |
#' children = list( |
|
| 2082 |
#' tibble(name = c("forest", "river")),
|
|
| 2083 |
#' # 3rd level |
|
| 2084 |
#' tibble( |
|
| 2085 |
#' name = c("fish", "kelp"),
|
|
| 2086 |
#' children = list( |
|
| 2087 |
#' tibble( |
|
| 2088 |
#' name = c("shark", "tuna")
|
|
| 2089 |
#' ), |
|
| 2090 |
#' # 4th level |
|
| 2091 |
#' NULL # kelp |
|
| 2092 |
#' ) |
|
| 2093 |
#' ) |
|
| 2094 |
#' ) |
|
| 2095 |
#' ) |
|
| 2096 |
#' ) |
|
| 2097 |
#' ) |
|
| 2098 |
#' |
|
| 2099 |
#' df |> |
|
| 2100 |
#' e_charts() |> |
|
| 2101 |
#' e_tree(initialTreeDepth = 3, label = list(offset = c(0, -11))) |
|
| 2102 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-tree}{Additional arguments}
|
|
| 2103 |
#' |
|
| 2104 |
#' @rdname e_tree |
|
| 2105 |
#' @export |
|
| 2106 |
e_tree <- function(e, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2107 | 4x |
UseMethod("e_tree")}
|
| 2108 | ||
| 2109 |
#' @export |
|
| 2110 |
#' @method e_tree echarts4r |
|
| 2111 |
e_tree.echarts4r <- function(e, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2112 | 4x |
if (missing(e)) {
|
| 2113 | 1x |
stop("must pass e", call. = FALSE)
|
| 2114 |
} |
|
| 2115 | ||
| 2116 | 3x |
e_tree_(e, rm_x, rm_y, ...) |
| 2117 |
} |
|
| 2118 | ||
| 2119 |
#' @export |
|
| 2120 |
#' @method e_tree echarts4rProxy |
|
| 2121 |
e_tree.echarts4rProxy <- function(e, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2122 | 1x |
e$chart <- e_tree_(e$chart, rm_x, rm_y, ...) |
| 2123 | 1x |
return(e) |
| 2124 |
} |
|
| 2125 | ||
| 2126 |
#' Gauge |
|
| 2127 |
#' |
|
| 2128 |
#' Plot a gauge. |
|
| 2129 |
#' |
|
| 2130 |
#' @inheritParams e_bar |
|
| 2131 |
#' @param value Value to gauge. |
|
| 2132 |
#' @param name Text on gauge. |
|
| 2133 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 2134 |
#' |
|
| 2135 |
#' @examples |
|
| 2136 |
#' e_charts() |> |
|
| 2137 |
#' e_gauge(57, "PERCENT") |
|
| 2138 |
#' |
|
| 2139 |
#' # timeline |
|
| 2140 |
#' data.frame(time = 2015:2017) |> |
|
| 2141 |
#' group_by(time) |> |
|
| 2142 |
#' e_charts(timeline = TRUE) |> |
|
| 2143 |
#' e_gauge( |
|
| 2144 |
#' c(57, 23, 65), |
|
| 2145 |
#' c("percent", "percentage", "cases")
|
|
| 2146 |
#' ) |
|
| 2147 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-gauge}{Additional arguments}
|
|
| 2148 |
#' |
|
| 2149 |
#' @rdname e_gauge |
|
| 2150 |
#' @export |
|
| 2151 | 6x |
e_gauge <- function(e, value, name = NULL, rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_gauge")
|
| 2152 | ||
| 2153 |
#' @export |
|
| 2154 |
#' @method e_gauge echarts4r |
|
| 2155 |
e_gauge.echarts4r <- function(e, value, name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2156 | ||
| 2157 | 5x |
if (missing(e) || missing(value)) {
|
| 2158 | 1x |
stop("missing e or value", call. = FALSE)
|
| 2159 |
} |
|
| 2160 | ||
| 2161 | 4x |
if (!inherits(value, "numeric")) {
|
| 2162 | ! |
stop("must pass numeric or integer", call. = FALSE)
|
| 2163 |
} |
|
| 2164 | ||
| 2165 |
# remove axis |
|
| 2166 | 4x |
e <- .rm_axis(e, rm_x, "x") |
| 2167 | 4x |
e <- .rm_axis(e, rm_y, "y") |
| 2168 | ||
| 2169 | 4x |
for (i in seq_along(value)) {
|
| 2170 | 6x |
serie <- list( |
| 2171 | 6x |
data = list(list(value = value[i], name = name[i])) |
| 2172 |
) |
|
| 2173 | ||
| 2174 | 6x |
opts <- list( |
| 2175 | 6x |
type = "gauge", |
| 2176 |
... |
|
| 2177 |
) |
|
| 2178 | ||
| 2179 | 6x |
if (!e$x$tl) {
|
| 2180 | 3x |
lst <- append(serie, opts) |
| 2181 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(lst)) |
| 2182 |
} else {
|
|
| 2183 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie)) |
| 2184 |
} |
|
| 2185 |
} |
|
| 2186 | ||
| 2187 | 4x |
if (e$x$tl) {
|
| 2188 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(opts)) |
| 2189 |
} |
|
| 2190 | 4x |
e |
| 2191 |
} |
|
| 2192 | ||
| 2193 |
#' @export |
|
| 2194 |
#' @method e_gauge echarts4rProxy |
|
| 2195 |
e_gauge.echarts4rProxy <- function(e, value, name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2196 | 2x |
if (missing(value)) {
|
| 2197 | 1x |
stop("missing value", call. = FALSE)
|
| 2198 |
} |
|
| 2199 | ||
| 2200 |
# Breaks if no data, so I added this. |
|
| 2201 | 1x |
if(is.null(e$chart$x$data[[1]])){
|
| 2202 | ! |
stop("provide data in echarts4rProxy")
|
| 2203 |
} |
|
| 2204 | 1x |
e$chart <- e_gauge(e$chart, value, name, rm_x, rm_y, ...) |
| 2205 | 1x |
return(e) |
| 2206 |
} |
|
| 2207 | ||
| 2208 |
#' @inheritParams e_bar |
|
| 2209 |
#' @rdname e_gauge |
|
| 2210 |
#' @export |
|
| 2211 |
e_gauge_ <- function(e, value, name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2212 | 3x |
if (missing(e) || missing(value) || missing(name)) {
|
| 2213 | 1x |
stop("missing e or value", call. = FALSE)
|
| 2214 |
} |
|
| 2215 | ||
| 2216 | 2x |
if (!inherits(value, "numeric")) {
|
| 2217 | ! |
stop("must pass numeric or integer", call. = FALSE)
|
| 2218 |
} |
|
| 2219 | ||
| 2220 |
# remove axis |
|
| 2221 | 2x |
e <- .rm_axis(e, rm_x, "x") |
| 2222 | 2x |
e <- .rm_axis(e, rm_y, "y") |
| 2223 | ||
| 2224 | 2x |
for (i in seq_along(value)) {
|
| 2225 | 4x |
serie <- list( |
| 2226 | 4x |
data = list(list(value = value[i], name = name[i])) |
| 2227 |
) |
|
| 2228 | ||
| 2229 | 4x |
opts <- list( |
| 2230 | 4x |
type = "gauge", |
| 2231 |
... |
|
| 2232 |
) |
|
| 2233 | ||
| 2234 | 4x |
if (!e$x$tl) {
|
| 2235 | 1x |
lst <- append(serie, opts) |
| 2236 | 1x |
e$x$opts$series <- append(e$x$opts$series, list(lst)) |
| 2237 |
} else {
|
|
| 2238 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(serie)) |
| 2239 |
} |
|
| 2240 |
} |
|
| 2241 | ||
| 2242 | 2x |
if (e$x$tl) {
|
| 2243 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(opts)) |
| 2244 |
} |
|
| 2245 | 2x |
e |
| 2246 |
} |
|
| 2247 | ||
| 2248 |
#' Lines 3D |
|
| 2249 |
#' |
|
| 2250 |
#' Add 3D lines. |
|
| 2251 |
#' |
|
| 2252 |
#' @inheritParams e_bar |
|
| 2253 |
#' @param coord_system Coordinate system to use, such as \code{cartesian3D}, or \code{globe}.
|
|
| 2254 |
#' @param y,z Coordinates of lines. |
|
| 2255 |
#' @param source_lon,source_lat,target_lon,target_lat coordinates. |
|
| 2256 |
#' @param source_name,target_name Names of source and target. |
|
| 2257 |
#' @param value Value of edges. |
|
| 2258 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 2259 |
#' |
|
| 2260 |
#' @examples |
|
| 2261 |
#' # Lines 3D |
|
| 2262 |
#' # Globe |
|
| 2263 |
#' # get tetures: echarts4r-assets.john-coene.com |
|
| 2264 |
#' # get textures: echarts4r-assets.john-coene.com |
|
| 2265 |
#' flights |> |
|
| 2266 |
#' e_charts() |> |
|
| 2267 |
#' e_globe( |
|
| 2268 |
#' displacementScale = 0.05 |
|
| 2269 |
#' ) |> |
|
| 2270 |
#' e_lines_3d( |
|
| 2271 |
#' start_lon, |
|
| 2272 |
#' start_lat, |
|
| 2273 |
#' end_lon, |
|
| 2274 |
#' end_lat, |
|
| 2275 |
#' name = "flights", |
|
| 2276 |
#' effect = list(show = TRUE) |
|
| 2277 |
#' ) |> |
|
| 2278 |
#' e_legend(FALSE) |
|
| 2279 |
#' |
|
| 2280 |
#' # Geo 3D |
|
| 2281 |
#' flights |> |
|
| 2282 |
#' e_charts() |> |
|
| 2283 |
#' e_geo_3d() |> |
|
| 2284 |
#' e_lines_3d( |
|
| 2285 |
#' start_lon, |
|
| 2286 |
#' start_lat, |
|
| 2287 |
#' end_lon, |
|
| 2288 |
#' end_lat, |
|
| 2289 |
#' coord_system = "geo3D" |
|
| 2290 |
#' ) |
|
| 2291 |
#' |
|
| 2292 |
#' # groups |
|
| 2293 |
#' flights$grp <- rep(LETTERS[1:2], 89) |
|
| 2294 |
#' |
|
| 2295 |
#' flights |> |
|
| 2296 |
#' group_by(grp) |> |
|
| 2297 |
#' e_charts() |> |
|
| 2298 |
#' e_geo_3d() |> |
|
| 2299 |
#' e_lines_3d( |
|
| 2300 |
#' start_lon, |
|
| 2301 |
#' start_lat, |
|
| 2302 |
#' end_lon, |
|
| 2303 |
#' end_lat, |
|
| 2304 |
#' coord_system = "geo3D" |
|
| 2305 |
#' ) |
|
| 2306 |
#' |
|
| 2307 |
#' # line 3D |
|
| 2308 |
#' df <- data.frame( |
|
| 2309 |
#' x = 1:100, |
|
| 2310 |
#' y = runif(100, 10, 25), |
|
| 2311 |
#' z = rnorm(100, 100, 50) |
|
| 2312 |
#' ) |
|
| 2313 |
#' |
|
| 2314 |
#' df |> |
|
| 2315 |
#' e_charts(x) |> |
|
| 2316 |
#' e_line_3d(y, z) |> |
|
| 2317 |
#' e_visual_map() |> |
|
| 2318 |
#' e_title("nonsense")
|
|
| 2319 |
#' |
|
| 2320 |
#' # timeline |
|
| 2321 |
#' df$grp <- rep(LETTERS[1:5], 20) |
|
| 2322 |
#' |
|
| 2323 |
#' df |> |
|
| 2324 |
#' group_by(grp) |> |
|
| 2325 |
#' e_charts(x) |> |
|
| 2326 |
#' e_line_3d(y, z) |> |
|
| 2327 |
#' e_visual_map() |> |
|
| 2328 |
#' e_title("nonsense")
|
|
| 2329 |
#' @seealso \href{https://echarts.apache.org/en/option-gl.html#series-lines3D}{Additional arguments for lines 3D},
|
|
| 2330 |
#' \href{https://echarts.apache.org/en/option-gl.html#series-lines3D}{Additional arguments for line 3D}
|
|
| 2331 |
#' |
|
| 2332 |
#' @seealso \url{https://echarts4r-assets.john-coene.com}
|
|
| 2333 |
#' |
|
| 2334 |
#' @rdname line3D |
|
| 2335 |
#' @export |
|
| 2336 |
e_lines_3d <- function( |
|
| 2337 |
e, |
|
| 2338 |
source_lon, |
|
| 2339 |
source_lat, |
|
| 2340 |
target_lon, |
|
| 2341 |
target_lat, |
|
| 2342 |
source_name, |
|
| 2343 |
target_name, |
|
| 2344 |
value, |
|
| 2345 |
name = NULL, |
|
| 2346 |
coord_system = "globe", |
|
| 2347 |
rm_x = TRUE, |
|
| 2348 |
rm_y = TRUE, |
|
| 2349 |
...) {
|
|
| 2350 | 6x |
UseMethod("e_lines_3d")
|
| 2351 |
} |
|
| 2352 | ||
| 2353 |
#' @export |
|
| 2354 |
#' @method e_lines_3d echarts4r |
|
| 2355 |
e_lines_3d.echarts4r <- function( |
|
| 2356 |
e, |
|
| 2357 |
source_lon, |
|
| 2358 |
source_lat, |
|
| 2359 |
target_lon, |
|
| 2360 |
target_lat, |
|
| 2361 |
source_name, |
|
| 2362 |
target_name, |
|
| 2363 |
value, |
|
| 2364 |
name = NULL, |
|
| 2365 |
coord_system = "globe", |
|
| 2366 |
rm_x = TRUE, |
|
| 2367 |
rm_y = TRUE, |
|
| 2368 |
...) {
|
|
| 2369 | 6x |
if (missing(e)) {
|
| 2370 | 1x |
stop("must pass e", call. = FALSE)
|
| 2371 |
} |
|
| 2372 | 5x |
if (missing(source_lat) || missing(source_lon) || missing(target_lat) || missing(target_lon)) {
|
| 2373 | 1x |
stop("missing coordinates", call. = FALSE)
|
| 2374 |
} |
|
| 2375 | ||
| 2376 | 4x |
if (missing(source_name)) {
|
| 2377 | 3x |
source_name <- NULL |
| 2378 |
} else {
|
|
| 2379 | 1x |
source_name <- deparse(substitute(source_name)) |
| 2380 |
} |
|
| 2381 | ||
| 2382 | 4x |
if (missing(target_name)) {
|
| 2383 | 3x |
target_name <- NULL |
| 2384 |
} else {
|
|
| 2385 | 1x |
target_name <- deparse(substitute(target_name)) |
| 2386 |
} |
|
| 2387 | ||
| 2388 | 4x |
if (missing(value)) {
|
| 2389 | 3x |
value <- NULL |
| 2390 |
} else {
|
|
| 2391 | 1x |
value <- deparse(substitute(value)) |
| 2392 |
} |
|
| 2393 | ||
| 2394 | 4x |
e_lines_3d_( |
| 2395 | 4x |
e, |
| 2396 | 4x |
deparse(substitute(source_lon)), |
| 2397 | 4x |
deparse(substitute(source_lat)), |
| 2398 | 4x |
deparse(substitute(target_lon)), |
| 2399 | 4x |
deparse(substitute(target_lat)), |
| 2400 | 4x |
source_name, |
| 2401 | 4x |
target_name, |
| 2402 | 4x |
value, |
| 2403 | 4x |
name, |
| 2404 | 4x |
coord_system, |
| 2405 | 4x |
rm_x, |
| 2406 | 4x |
rm_y, |
| 2407 |
... |
|
| 2408 |
) |
|
| 2409 |
} |
|
| 2410 | ||
| 2411 |
#' @export |
|
| 2412 |
#' @method e_lines_3d echarts4rProxy |
|
| 2413 |
e_lines_3d.echarts4rProxy <- function( |
|
| 2414 |
e, |
|
| 2415 |
source_lon, |
|
| 2416 |
source_lat, |
|
| 2417 |
target_lon, |
|
| 2418 |
target_lat, |
|
| 2419 |
source_name, |
|
| 2420 |
target_name, |
|
| 2421 |
value, |
|
| 2422 |
name = NULL, |
|
| 2423 |
coord_system = "globe", |
|
| 2424 |
rm_x = TRUE, |
|
| 2425 |
rm_y = TRUE, |
|
| 2426 |
...) {
|
|
| 2427 | 2x |
if (missing(source_lat) || missing(source_lon) || missing(target_lat) || missing(target_lon)) {
|
| 2428 | 1x |
stop("missing coordinates", call. = FALSE)
|
| 2429 |
} |
|
| 2430 | 1x |
if (missing(source_name)) {
|
| 2431 | 1x |
source_name <- NULL |
| 2432 |
} |
|
| 2433 | ||
| 2434 | 1x |
if (missing(target_name)) {
|
| 2435 | 1x |
target_name <- NULL |
| 2436 |
} |
|
| 2437 | ||
| 2438 | 1x |
if (missing(value)) {
|
| 2439 | 1x |
value <- NULL |
| 2440 |
} |
|
| 2441 | ||
| 2442 | 1x |
e$chart <- e_lines_3d_( |
| 2443 | 1x |
e$chart, |
| 2444 | 1x |
deparse(substitute(source_lon)), |
| 2445 | 1x |
deparse(substitute(source_lat)), |
| 2446 | 1x |
deparse(substitute(target_lon)), |
| 2447 | 1x |
deparse(substitute(target_lat)), |
| 2448 | 1x |
deparse(substitute(source_name)), |
| 2449 | 1x |
deparse(substitute(target_name)), |
| 2450 | 1x |
deparse(substitute(value)), |
| 2451 | 1x |
name, |
| 2452 | 1x |
coord_system, |
| 2453 | 1x |
rm_x, |
| 2454 | 1x |
rm_y, |
| 2455 |
... |
|
| 2456 |
) |
|
| 2457 | 1x |
return(e) |
| 2458 |
} |
|
| 2459 | ||
| 2460 |
#' @rdname line3D |
|
| 2461 |
#' @export |
|
| 2462 | 4x |
e_line_3d <- function(e, y, z, name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_line_3d")
|
| 2463 | ||
| 2464 |
#' @export |
|
| 2465 |
#' @method e_line_3d echarts4r |
|
| 2466 |
e_line_3d.echarts4r <- function(e, y, z, name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2467 | 4x |
if (missing(e)) {
|
| 2468 | 1x |
stop("must pass e", call. = FALSE)
|
| 2469 |
} |
|
| 2470 | ||
| 2471 | 3x |
if (missing(y) || missing(z)) {
|
| 2472 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 2473 |
} |
|
| 2474 | ||
| 2475 | 2x |
e_line_3d_( |
| 2476 | 2x |
e, |
| 2477 | 2x |
deparse(substitute(y)), |
| 2478 | 2x |
deparse(substitute(z)), |
| 2479 | 2x |
name, |
| 2480 | 2x |
coord_system, |
| 2481 | 2x |
rm_x, |
| 2482 | 2x |
rm_y, |
| 2483 |
... |
|
| 2484 |
) |
|
| 2485 |
} |
|
| 2486 | ||
| 2487 |
#' @export |
|
| 2488 |
#' @method e_line_3d echarts4rProxy |
|
| 2489 |
e_line_3d.echarts4rProxy <- function(e, y, z, name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 2490 | ||
| 2491 | 2x |
if (missing(y) || missing(z)) {
|
| 2492 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 2493 |
} |
|
| 2494 | ||
| 2495 | 1x |
e$chart <- e_line_3d_( |
| 2496 | 1x |
e$chart, |
| 2497 | 1x |
deparse(substitute(y)), |
| 2498 | 1x |
deparse(substitute(z)), |
| 2499 | 1x |
name, |
| 2500 | 1x |
coord_system, |
| 2501 | 1x |
rm_x, |
| 2502 | 1x |
rm_y, |
| 2503 |
... |
|
| 2504 |
) |
|
| 2505 | 1x |
return(e) |
| 2506 |
} |
|
| 2507 | ||
| 2508 |
#' Bar 3D |
|
| 2509 |
#' |
|
| 2510 |
#' Add 3D bars |
|
| 2511 |
#' |
|
| 2512 |
#' @inheritParams e_bar |
|
| 2513 |
#' @param y,z Coordinates. |
|
| 2514 |
#' @param bind Binding. |
|
| 2515 |
#' @param coord_system Coordinate system to use, one of \code{cartesian3D}, \code{geo3D}, \code{globe}.
|
|
| 2516 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 2517 |
#' |
|
| 2518 |
#' @examples |
|
| 2519 |
#' \dontrun{
|
|
| 2520 |
#' # volcano |
|
| 2521 |
#' volcano |> |
|
| 2522 |
#' as.table() |> |
|
| 2523 |
#' as.data.frame() |> |
|
| 2524 |
#' dplyr::mutate( |
|
| 2525 |
#' Var1 = as.integer(Var1), |
|
| 2526 |
#' Var2 = as.integer(Var2) |
|
| 2527 |
#' ) |> |
|
| 2528 |
#' e_charts(Var1) |> |
|
| 2529 |
#' e_bar_3d(Var2, Freq) |> |
|
| 2530 |
#' e_visual_map(Freq) |
|
| 2531 |
#' |
|
| 2532 |
#' url <- paste0( |
|
| 2533 |
#' "https://echarts.apache.org/examples/", |
|
| 2534 |
#' "data-gl/asset/data/population.json" |
|
| 2535 |
#' ) |
|
| 2536 |
#' data <- jsonlite::fromJSON(url) |
|
| 2537 |
#' data <- as.data.frame(data) |
|
| 2538 |
#' names(data) <- c("lon", "lat", "value")
|
|
| 2539 |
#' |
|
| 2540 |
#' # globe |
|
| 2541 |
#' data |> |
|
| 2542 |
#' e_charts(lon) |> |
|
| 2543 |
#' e_globe() |> |
|
| 2544 |
#' e_bar_3d(lat, value, coord_system = "globe") |> |
|
| 2545 |
#' e_visual_map() |
|
| 2546 |
#' |
|
| 2547 |
#' # get3d |
|
| 2548 |
#' data |> |
|
| 2549 |
#' e_charts(lon) |> |
|
| 2550 |
#' e_geo_3d() |> |
|
| 2551 |
#' e_bar_3d(lat, value, coord_system = "geo3D") |> |
|
| 2552 |
#' e_visual_map() |
|
| 2553 |
#' |
|
| 2554 |
#' # stacked |
|
| 2555 |
#' v <- LETTERS[1:10] |
|
| 2556 |
#' matrix <- data.frame( |
|
| 2557 |
#' x = sample(v, 300, replace = TRUE), |
|
| 2558 |
#' y = sample(v, 300, replace = TRUE), |
|
| 2559 |
#' z1 = rnorm(300, 10, 1), |
|
| 2560 |
#' z2 = rnorm(300, 10, 1), |
|
| 2561 |
#' stringsAsFactors = FALSE |
|
| 2562 |
#' ) |> |
|
| 2563 |
#' dplyr::group_by(x, y) |> |
|
| 2564 |
#' dplyr::summarise( |
|
| 2565 |
#' z1 = sum(z1), |
|
| 2566 |
#' z2 = sum(z2) |
|
| 2567 |
#' ) |> |
|
| 2568 |
#' dplyr::ungroup() |
|
| 2569 |
#' |
|
| 2570 |
#' trans <- list(opacity = 0.4) # transparency |
|
| 2571 |
#' emphasis <- list(itemStyle = list(color = "#313695")) |
|
| 2572 |
#' |
|
| 2573 |
#' matrix |> |
|
| 2574 |
#' e_charts(x) |> |
|
| 2575 |
#' e_bar_3d(y, z1, stack = "stack", name = "Serie 1", itemStyle = trans, emphasis = emphasis) |> |
|
| 2576 |
#' e_bar_3d(y, z2, stack = "stack", name = "Serie 2", itemStyle = trans, emphasis = emphasis) |> |
|
| 2577 |
#' e_legend() |
|
| 2578 |
#' |
|
| 2579 |
#' # timeline |
|
| 2580 |
#' matrix |> |
|
| 2581 |
#' group_by(x) |> |
|
| 2582 |
#' e_charts(y, timeline = TRUE) |> |
|
| 2583 |
#' e_bar_3d(z1, z2) |> |
|
| 2584 |
#' e_visual_map(z2) |
|
| 2585 |
#' } |
|
| 2586 |
#' |
|
| 2587 |
#' @seealso \href{https://echarts.apache.org/en/option-gl.html#series-bar3D}{Additional arguments}
|
|
| 2588 |
#' |
|
| 2589 |
#' @rdname e_bar_3d |
|
| 2590 |
#' @export |
|
| 2591 |
e_bar_3d <- function( |
|
| 2592 |
e, |
|
| 2593 |
y, |
|
| 2594 |
z, |
|
| 2595 |
bind, |
|
| 2596 |
coord_system = "cartesian3D", |
|
| 2597 |
name = NULL, |
|
| 2598 |
rm_x = TRUE, |
|
| 2599 |
rm_y = TRUE, |
|
| 2600 |
...) {
|
|
| 2601 | 16x |
UseMethod("e_bar_3d")
|
| 2602 |
} |
|
| 2603 | ||
| 2604 |
#' @export |
|
| 2605 |
#' @method e_bar_3d echarts4r |
|
| 2606 |
e_bar_3d.echarts4r <- function( |
|
| 2607 |
e, |
|
| 2608 |
y, |
|
| 2609 |
z, |
|
| 2610 |
bind, |
|
| 2611 |
coord_system = "cartesian3D", |
|
| 2612 |
name = NULL, |
|
| 2613 |
rm_x = TRUE, |
|
| 2614 |
rm_y = TRUE, |
|
| 2615 |
...) {
|
|
| 2616 | 16x |
if (missing(e)) {
|
| 2617 | 1x |
stop("must pass e", call. = FALSE)
|
| 2618 |
} |
|
| 2619 | ||
| 2620 | 15x |
if (missing(y) || missing(z)) {
|
| 2621 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 2622 |
} |
|
| 2623 | ||
| 2624 | 14x |
bd <- .get_bind(deparse(substitute(bind))) |
| 2625 | ||
| 2626 | 14x |
e_bar_3d_( |
| 2627 | 14x |
e, |
| 2628 | 14x |
deparse(substitute(y)), |
| 2629 | 14x |
deparse(substitute(z)), |
| 2630 | 14x |
bd, |
| 2631 | 14x |
coord_system, |
| 2632 | 14x |
name, |
| 2633 | 14x |
rm_x, |
| 2634 | 14x |
rm_y, |
| 2635 |
... |
|
| 2636 |
) |
|
| 2637 |
} |
|
| 2638 | ||
| 2639 |
#' @export |
|
| 2640 |
#' @method e_bar_3d echarts4rProxy |
|
| 2641 |
e_bar_3d.echarts4rProxy <- function( |
|
| 2642 |
e, |
|
| 2643 |
y, |
|
| 2644 |
z, |
|
| 2645 |
bind, |
|
| 2646 |
coord_system = "cartesian3D", |
|
| 2647 |
name = NULL, |
|
| 2648 |
rm_x = TRUE, |
|
| 2649 |
rm_y = TRUE, |
|
| 2650 |
...) {
|
|
| 2651 | 2x |
if (missing(y) || missing(z)) {
|
| 2652 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 2653 |
} |
|
| 2654 | ||
| 2655 | 1x |
if(is.null(e$chart$x$mapping$x)){
|
| 2656 | ! |
stop("provide x in echarts4rProxy")
|
| 2657 |
} |
|
| 2658 | ||
| 2659 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 2660 | ||
| 2661 | 1x |
e$chart <- e_bar_3d_( |
| 2662 | 1x |
e$chart, |
| 2663 | 1x |
deparse(substitute(y)), |
| 2664 | 1x |
deparse(substitute(z)), |
| 2665 | 1x |
bd, |
| 2666 | 1x |
coord_system, |
| 2667 | 1x |
name, |
| 2668 | 1x |
rm_x, |
| 2669 | 1x |
rm_y, |
| 2670 |
... |
|
| 2671 |
) |
|
| 2672 | 1x |
return(e) |
| 2673 |
} |
|
| 2674 | ||
| 2675 |
#' Surface |
|
| 2676 |
#' |
|
| 2677 |
#' Add a surface plot. |
|
| 2678 |
#' |
|
| 2679 |
#' @inheritParams e_bar |
|
| 2680 |
#' @param y,z Coordinates. |
|
| 2681 |
#' @param bind Binding. |
|
| 2682 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 2683 |
#' |
|
| 2684 |
#' @examples |
|
| 2685 |
#' data("volcano")
|
|
| 2686 |
#' |
|
| 2687 |
#' surface <- as.data.frame(as.table(volcano)) |
|
| 2688 |
#' surface$Var1 <- as.numeric(surface$Var1) |
|
| 2689 |
#' surface$Var2 <- as.numeric(surface$Var2) |
|
| 2690 |
#' |
|
| 2691 |
#' surface |> |
|
| 2692 |
#' e_charts(Var1) |> |
|
| 2693 |
#' e_surface(Var2, Freq) |> |
|
| 2694 |
#' e_visual_map(Freq) |
|
| 2695 |
#' @rdname e_surface |
|
| 2696 |
#' @export |
|
| 2697 |
e_surface <- function( |
|
| 2698 |
e, |
|
| 2699 |
y, |
|
| 2700 |
z, |
|
| 2701 |
bind, |
|
| 2702 |
name = NULL, |
|
| 2703 |
rm_x = TRUE, |
|
| 2704 |
rm_y = TRUE, |
|
| 2705 |
...) {
|
|
| 2706 | 5x |
UseMethod("e_surface")
|
| 2707 |
} |
|
| 2708 | ||
| 2709 |
#' @export |
|
| 2710 |
#' @method e_surface echarts4r |
|
| 2711 |
e_surface.echarts4r <- function( |
|
| 2712 |
e, |
|
| 2713 |
y, |
|
| 2714 |
z, |
|
| 2715 |
bind, |
|
| 2716 |
name = NULL, |
|
| 2717 |
rm_x = TRUE, |
|
| 2718 |
rm_y = TRUE, |
|
| 2719 |
...) {
|
|
| 2720 | 5x |
if (missing(e)) {
|
| 2721 | 1x |
stop("must pass e", call. = FALSE)
|
| 2722 |
} |
|
| 2723 | ||
| 2724 | 4x |
if (missing(y) || missing(z)) {
|
| 2725 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 2726 |
} |
|
| 2727 | ||
| 2728 | 3x |
bd <- .get_bind(deparse(substitute(bind))) |
| 2729 | ||
| 2730 | 3x |
e_surface_( |
| 2731 | 3x |
e, |
| 2732 | 3x |
y = deparse(substitute(y)), |
| 2733 | 3x |
z = deparse(substitute(z)), |
| 2734 | 3x |
bind = bd, |
| 2735 | 3x |
name = name, |
| 2736 | 3x |
rm_x = rm_x, |
| 2737 | 3x |
rm_y = rm_y, |
| 2738 |
... |
|
| 2739 |
) |
|
| 2740 |
} |
|
| 2741 | ||
| 2742 |
#' @export |
|
| 2743 |
#' @method e_surface echarts4rProxy |
|
| 2744 |
e_surface.echarts4rProxy <- function( |
|
| 2745 |
e, |
|
| 2746 |
y, |
|
| 2747 |
z, |
|
| 2748 |
bind, |
|
| 2749 |
name = NULL, |
|
| 2750 |
rm_x = TRUE, |
|
| 2751 |
rm_y = TRUE, |
|
| 2752 |
...) {
|
|
| 2753 | 2x |
if (missing(y) || missing(z)) {
|
| 2754 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 2755 |
} |
|
| 2756 | ||
| 2757 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 2758 | ||
| 2759 | 1x |
e$chart <- e_surface_( |
| 2760 | 1x |
e$chart, |
| 2761 | 1x |
deparse(substitute(y)), |
| 2762 | 1x |
deparse(substitute(z)), |
| 2763 | 1x |
bd, |
| 2764 | 1x |
name, |
| 2765 | 1x |
rm_x, |
| 2766 | 1x |
rm_y, |
| 2767 |
... |
|
| 2768 |
) |
|
| 2769 | 1x |
return(e) |
| 2770 |
} |
|
| 2771 | ||
| 2772 |
#' Lines |
|
| 2773 |
#' |
|
| 2774 |
#' Add lines. |
|
| 2775 |
#' |
|
| 2776 |
#' @inheritParams e_bar |
|
| 2777 |
#' @param source_lon,source_lat,target_lon,target_lat coordinates. |
|
| 2778 |
#' @param source_name,target_name Names of source and target. |
|
| 2779 |
#' @param value Value of edges. |
|
| 2780 |
#' @param coord_system Coordinate system to use, one of \code{geo}, or \code{cartesian2d}.
|
|
| 2781 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 2782 |
#' |
|
| 2783 |
#' @examples |
|
| 2784 |
#' flights |> |
|
| 2785 |
#' e_charts() |> |
|
| 2786 |
#' e_geo() |> |
|
| 2787 |
#' e_lines( |
|
| 2788 |
#' start_lon, |
|
| 2789 |
#' start_lat, |
|
| 2790 |
#' end_lon, |
|
| 2791 |
#' end_lat, |
|
| 2792 |
#' airport1, |
|
| 2793 |
#' airport2, |
|
| 2794 |
#' cnt, |
|
| 2795 |
#' name = "flights", |
|
| 2796 |
#' lineStyle = list(normal = list(curveness = 0.3)) |
|
| 2797 |
#' ) |> |
|
| 2798 |
#' e_tooltip( |
|
| 2799 |
#' trigger = "item", |
|
| 2800 |
#' formatter = htmlwidgets::JS("
|
|
| 2801 |
#' function(params){
|
|
| 2802 |
#' return( |
|
| 2803 |
#' params.seriesName +'<br />' + |
|
| 2804 |
#' params.data.source_name + ' -> ' + |
|
| 2805 |
#' params.data.target_name + ':'+ params.value |
|
| 2806 |
#' ) |
|
| 2807 |
#' } |
|
| 2808 |
#' ") |
|
| 2809 |
#' ) |
|
| 2810 |
#' |
|
| 2811 |
#' # timeline |
|
| 2812 |
#' flights$grp <- rep(LETTERS[1:2], 89) |
|
| 2813 |
#' |
|
| 2814 |
#' flights |> |
|
| 2815 |
#' group_by(grp) |> |
|
| 2816 |
#' e_charts(timeline = TRUE) |> |
|
| 2817 |
#' e_geo() |> |
|
| 2818 |
#' e_lines( |
|
| 2819 |
#' start_lon, |
|
| 2820 |
#' start_lat, |
|
| 2821 |
#' end_lon, |
|
| 2822 |
#' end_lat, |
|
| 2823 |
#' cnt, |
|
| 2824 |
#' coord_system = "geo" |
|
| 2825 |
#' ) |
|
| 2826 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-lines}{Additional arguments}
|
|
| 2827 |
#' |
|
| 2828 |
#' @rdname e_lines |
|
| 2829 |
#' @export |
|
| 2830 |
e_lines <- function( |
|
| 2831 |
e, |
|
| 2832 |
source_lon, |
|
| 2833 |
source_lat, |
|
| 2834 |
target_lon, |
|
| 2835 |
target_lat, |
|
| 2836 |
source_name, |
|
| 2837 |
target_name, |
|
| 2838 |
value, |
|
| 2839 |
coord_system = "geo", |
|
| 2840 |
name = NULL, |
|
| 2841 |
rm_x = TRUE, |
|
| 2842 |
rm_y = TRUE, |
|
| 2843 |
...) {
|
|
| 2844 | ||
| 2845 | 5x |
UseMethod("e_lines")
|
| 2846 |
} |
|
| 2847 | ||
| 2848 |
#' @export |
|
| 2849 |
#' @method e_lines echarts4r |
|
| 2850 |
e_lines.echarts4r <- function( |
|
| 2851 |
e, |
|
| 2852 |
source_lon, |
|
| 2853 |
source_lat, |
|
| 2854 |
target_lon, |
|
| 2855 |
target_lat, |
|
| 2856 |
source_name, |
|
| 2857 |
target_name, |
|
| 2858 |
value, |
|
| 2859 |
coord_system = "geo", |
|
| 2860 |
name = NULL, |
|
| 2861 |
rm_x = TRUE, |
|
| 2862 |
rm_y = TRUE, |
|
| 2863 |
...) {
|
|
| 2864 | 5x |
if (missing(e)) {
|
| 2865 | 1x |
stop("must pass e", call. = FALSE)
|
| 2866 |
} |
|
| 2867 | ||
| 2868 | 4x |
if (missing(source_lat) || missing(source_lon) || missing(target_lat) || missing(target_lon)) {
|
| 2869 | 1x |
stop("missing coordinates", call. = FALSE)
|
| 2870 |
} |
|
| 2871 | ||
| 2872 | 3x |
if (missing(source_name)) {
|
| 2873 | 2x |
source_name <- NULL |
| 2874 |
} else {
|
|
| 2875 | 1x |
source_name <- deparse(substitute(source_name)) |
| 2876 |
} |
|
| 2877 | ||
| 2878 | 3x |
if (missing(target_name)) {
|
| 2879 | 2x |
target_name <- NULL |
| 2880 |
} else {
|
|
| 2881 | 1x |
target_name <- deparse(substitute(target_name)) |
| 2882 |
} |
|
| 2883 | ||
| 2884 | 3x |
if (missing(value)) {
|
| 2885 | 2x |
value <- NULL |
| 2886 |
} else {
|
|
| 2887 | 1x |
value <- deparse(substitute(value)) |
| 2888 |
} |
|
| 2889 | ||
| 2890 | 3x |
e_lines_( |
| 2891 | 3x |
e, |
| 2892 | 3x |
deparse(substitute(source_lon)), |
| 2893 | 3x |
deparse(substitute(source_lat)), |
| 2894 | 3x |
deparse(substitute(target_lon)), |
| 2895 | 3x |
deparse(substitute(target_lat)), |
| 2896 | 3x |
source_name, |
| 2897 | 3x |
target_name, |
| 2898 | 3x |
value, |
| 2899 | 3x |
coord_system, |
| 2900 | 3x |
name, |
| 2901 | 3x |
rm_x, |
| 2902 | 3x |
rm_y, |
| 2903 |
... |
|
| 2904 |
) |
|
| 2905 |
} |
|
| 2906 | ||
| 2907 |
#' @export |
|
| 2908 |
#' @method e_lines echarts4rProxy |
|
| 2909 |
e_lines.echarts4rProxy <- function( |
|
| 2910 |
e, |
|
| 2911 |
source_lon, |
|
| 2912 |
source_lat, |
|
| 2913 |
target_lon, |
|
| 2914 |
target_lat, |
|
| 2915 |
source_name, |
|
| 2916 |
target_name, |
|
| 2917 |
value, |
|
| 2918 |
coord_system = "geo", |
|
| 2919 |
name = NULL, |
|
| 2920 |
rm_x = TRUE, |
|
| 2921 |
rm_y = TRUE, |
|
| 2922 |
...) {
|
|
| 2923 | 2x |
if (missing(source_lat) || missing(source_lon) || missing(target_lat) || missing(target_lon)) {
|
| 2924 | 1x |
stop("missing coordinates", call. = FALSE)
|
| 2925 |
} |
|
| 2926 | ||
| 2927 | 1x |
if (missing(source_name)) {
|
| 2928 | 1x |
source_name <- NULL |
| 2929 |
} |
|
| 2930 | ||
| 2931 | 1x |
if (missing(target_name)) {
|
| 2932 | 1x |
target_name <- NULL |
| 2933 |
} |
|
| 2934 | ||
| 2935 | 1x |
if (missing(value)) {
|
| 2936 | 1x |
value <- NULL |
| 2937 |
} |
|
| 2938 | ||
| 2939 | 1x |
e$chart <- e_lines_( |
| 2940 | 1x |
e$chart, |
| 2941 | 1x |
deparse(substitute(source_lon)), |
| 2942 | 1x |
deparse(substitute(source_lat)), |
| 2943 | 1x |
deparse(substitute(target_lon)), |
| 2944 | 1x |
deparse(substitute(target_lat)), |
| 2945 | 1x |
deparse(substitute(source_name)), |
| 2946 | 1x |
deparse(substitute(target_name)), |
| 2947 | 1x |
deparse(substitute(value)), |
| 2948 | 1x |
coord_system, |
| 2949 | 1x |
name, |
| 2950 | 1x |
rm_x, |
| 2951 | 1x |
rm_y, |
| 2952 |
... |
|
| 2953 |
) |
|
| 2954 | ||
| 2955 | 1x |
return(e) |
| 2956 |
} |
|
| 2957 | ||
| 2958 |
#' Scatter 3D |
|
| 2959 |
#' |
|
| 2960 |
#' Add 3D scatter. |
|
| 2961 |
#' |
|
| 2962 |
#' @inheritParams e_bar |
|
| 2963 |
#' @param y,z Coordinates. |
|
| 2964 |
#' @param bind Binding. |
|
| 2965 |
#' @param color,size Color and Size of bubbles. |
|
| 2966 |
#' @param coord_system Coordinate system to use, one of \code{geo3D},
|
|
| 2967 |
#' \code{globe}, or \code{cartesian3D}.
|
|
| 2968 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 2969 |
#' |
|
| 2970 |
#' @examples |
|
| 2971 |
#' v <- LETTERS[1:10] |
|
| 2972 |
#' matrix <- data.frame( |
|
| 2973 |
#' x = sample(v, 300, replace = TRUE), |
|
| 2974 |
#' y = sample(v, 300, replace = TRUE), |
|
| 2975 |
#' z = rnorm(300, 10, 1), |
|
| 2976 |
#' color = rnorm(300, 10, 1), |
|
| 2977 |
#' size = rnorm(300, 10, 1), |
|
| 2978 |
#' stringsAsFactors = FALSE |
|
| 2979 |
#' ) |> |
|
| 2980 |
#' dplyr::group_by(x, y) |> |
|
| 2981 |
#' dplyr::summarise( |
|
| 2982 |
#' z = sum(z), |
|
| 2983 |
#' color = sum(color), |
|
| 2984 |
#' size = sum(size) |
|
| 2985 |
#' ) |> |
|
| 2986 |
#' dplyr::ungroup() |
|
| 2987 |
#' |
|
| 2988 |
#' matrix |> |
|
| 2989 |
#' e_charts(x) |> |
|
| 2990 |
#' e_scatter_3d(y, z, size, color) |> |
|
| 2991 |
#' e_visual_map( |
|
| 2992 |
#' min = 1, |
|
| 2993 |
#' max = 100, |
|
| 2994 |
#' inRange = list(symbolSize = c(1, 30)), |
|
| 2995 |
#' # scale size |
|
| 2996 |
#' dimension = 3 # third dimension 0 = x, y = 1, z = 2, size = 3 |
|
| 2997 |
#' ) |> |
|
| 2998 |
#' e_visual_map( |
|
| 2999 |
#' min = 1, |
|
| 3000 |
#' max = 100, |
|
| 3001 |
#' inRange = list(color = c("#bf444c", "#d88273", "#f6efa6")),
|
|
| 3002 |
#' # scale colors |
|
| 3003 |
#' dimension = 4, |
|
| 3004 |
#' # third dimension 0 = x, y = 1, z = 2, size = 3, color = 4 |
|
| 3005 |
#' bottom = 300 # padding to avoid visual maps overlap |
|
| 3006 |
#' ) |
|
| 3007 |
#' |
|
| 3008 |
#' airports |> |
|
| 3009 |
#' e_charts(long) |> |
|
| 3010 |
#' e_globe( |
|
| 3011 |
#' globeOuterRadius = 100 |
|
| 3012 |
#' ) |> |
|
| 3013 |
#' e_scatter_3d(lat, cnt, coord_system = "globe", blendMode = "lighter") |> |
|
| 3014 |
#' e_visual_map(inRange = list(symbolSize = c(1, 10))) |
|
| 3015 |
#' |
|
| 3016 |
#' # timeline |
|
| 3017 |
#' airports |> |
|
| 3018 |
#' group_by(state) |> |
|
| 3019 |
#' e_charts(long, timeline = TRUE) |> |
|
| 3020 |
#' e_globe( |
|
| 3021 |
#' globeOuterRadius = 100 |
|
| 3022 |
#' ) |> |
|
| 3023 |
#' e_scatter_3d(lat, cnt, coord_system = "globe", blendMode = "lighter") |> |
|
| 3024 |
#' e_visual_map(inRange = list(symbolSize = c(1, 10))) |
|
| 3025 |
#' @seealso |
|
| 3026 |
#' \href{https://echarts.apache.org/en/option-gl.html#series-scatter3D}{Additional
|
|
| 3027 |
#' arguments} |
|
| 3028 |
#' |
|
| 3029 |
#' @rdname e_scatter_3d |
|
| 3030 |
#' @export |
|
| 3031 |
e_scatter_3d <- function( |
|
| 3032 |
e, |
|
| 3033 |
y, |
|
| 3034 |
z, |
|
| 3035 |
color, |
|
| 3036 |
size, |
|
| 3037 |
bind, |
|
| 3038 |
coord_system = "cartesian3D", |
|
| 3039 |
name = NULL, |
|
| 3040 |
rm_x = TRUE, |
|
| 3041 |
rm_y = TRUE, |
|
| 3042 |
legend = FALSE, |
|
| 3043 |
...) {
|
|
| 3044 | 7x |
UseMethod("e_scatter_3d")
|
| 3045 |
} |
|
| 3046 | ||
| 3047 |
#' @export |
|
| 3048 |
#' @method e_scatter_3d echarts4r |
|
| 3049 |
e_scatter_3d.echarts4r <- function( |
|
| 3050 |
e, |
|
| 3051 |
y, |
|
| 3052 |
z, |
|
| 3053 |
color, |
|
| 3054 |
size, |
|
| 3055 |
bind, |
|
| 3056 |
coord_system = "cartesian3D", |
|
| 3057 |
name = NULL, |
|
| 3058 |
rm_x = TRUE, |
|
| 3059 |
rm_y = TRUE, |
|
| 3060 |
legend = FALSE, |
|
| 3061 |
...) {
|
|
| 3062 | 7x |
if (missing(e)) {
|
| 3063 | 1x |
stop("must pass e", call. = FALSE)
|
| 3064 |
} |
|
| 3065 | 6x |
if (missing(y) || missing(z)) {
|
| 3066 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 3067 |
} |
|
| 3068 | ||
| 3069 | 5x |
if (!missing(color)) {
|
| 3070 | ! |
colour <- deparse(substitute(color)) |
| 3071 |
} else {
|
|
| 3072 | 5x |
colour <- NULL |
| 3073 |
} |
|
| 3074 | ||
| 3075 | 5x |
sz <- .get_size(deparse(substitute(size))) |
| 3076 | 5x |
bd <- .get_bind(deparse(substitute(bind))) |
| 3077 | ||
| 3078 | 5x |
e_scatter_3d_( |
| 3079 | 5x |
e, |
| 3080 | 5x |
deparse(substitute(y)), |
| 3081 | 5x |
deparse(substitute(z)), |
| 3082 | 5x |
colour, |
| 3083 | 5x |
sz, |
| 3084 | 5x |
bd, |
| 3085 | 5x |
coord_system, |
| 3086 | 5x |
name, |
| 3087 | 5x |
rm_x, |
| 3088 | 5x |
rm_y, |
| 3089 | 5x |
legend, |
| 3090 |
... |
|
| 3091 |
) |
|
| 3092 |
} |
|
| 3093 | ||
| 3094 |
#' @export |
|
| 3095 |
#' @method e_scatter_3d echarts4rProxy |
|
| 3096 |
e_scatter_3d.echarts4rProxy <- function( |
|
| 3097 |
e, |
|
| 3098 |
y, |
|
| 3099 |
z, |
|
| 3100 |
color, |
|
| 3101 |
size, |
|
| 3102 |
bind, |
|
| 3103 |
coord_system = "cartesian3D", |
|
| 3104 |
name = NULL, |
|
| 3105 |
rm_x = TRUE, |
|
| 3106 |
rm_y = TRUE, |
|
| 3107 |
legend = FALSE, |
|
| 3108 |
...) {
|
|
| 3109 | 2x |
if (missing(y) || missing(z)) {
|
| 3110 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 3111 |
} |
|
| 3112 | ||
| 3113 | 1x |
if(is.null(e$chart$x$mapping$x)){
|
| 3114 | ! |
stop("provide x in echarts4rProxy")
|
| 3115 |
} |
|
| 3116 | ||
| 3117 | 1x |
if (!missing(color)) {
|
| 3118 | ! |
colour <- deparse(substitute(color)) |
| 3119 |
} else {
|
|
| 3120 | 1x |
colour <- NULL |
| 3121 |
} |
|
| 3122 | ||
| 3123 | 1x |
sz <- .get_size(deparse(substitute(size))) |
| 3124 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 3125 | ||
| 3126 | 1x |
e$chart <- e_scatter_3d_( |
| 3127 | 1x |
e$chart, |
| 3128 | 1x |
deparse(substitute(y)), |
| 3129 | 1x |
deparse(substitute(z)), |
| 3130 | 1x |
colour, |
| 3131 | 1x |
sz, |
| 3132 | 1x |
bd, |
| 3133 | 1x |
coord_system, |
| 3134 | 1x |
name, |
| 3135 | 1x |
rm_x, |
| 3136 | 1x |
rm_y, |
| 3137 | 1x |
legend, |
| 3138 |
... |
|
| 3139 |
) |
|
| 3140 | ||
| 3141 | 1x |
return(e) |
| 3142 |
} |
|
| 3143 | ||
| 3144 |
#' Flow GL |
|
| 3145 |
#' |
|
| 3146 |
#' @inheritParams e_bar |
|
| 3147 |
#' @param y Vector position on the y axis. |
|
| 3148 |
#' @param sx,sy Velocity in respective axis. |
|
| 3149 |
#' @param color Vector color. |
|
| 3150 |
#' @param coord_system Coordinate system to use. |
|
| 3151 |
#' @param rm_x,rm_y Whether to remove x and y axis, only applies if \code{coord_system} is not \code{null}.
|
|
| 3152 |
#' |
|
| 3153 |
#' @examples |
|
| 3154 |
#' # coordinates |
|
| 3155 |
#' vectors <- expand.grid(0:9, 0:9) |
|
| 3156 |
#' names(vectors) <- c("x", "y")
|
|
| 3157 |
#' vectors$sx <- rnorm(100) |
|
| 3158 |
#' vectors$sy <- rnorm(100) |
|
| 3159 |
#' vectors$color <- log10(runif(100, 1, 10)) |
|
| 3160 |
#' |
|
| 3161 |
#' vectors |> |
|
| 3162 |
#' e_charts(x) |> |
|
| 3163 |
#' e_flow_gl(y, sx, sy, color) |> |
|
| 3164 |
#' e_visual_map( |
|
| 3165 |
#' min = 0, |
|
| 3166 |
#' max = 1, |
|
| 3167 |
#' # log 10 |
|
| 3168 |
#' dimension = 4, |
|
| 3169 |
#' # x = 0, y = 1, sx = 3, sy = 4 |
|
| 3170 |
#' show = FALSE, |
|
| 3171 |
#' # hide |
|
| 3172 |
#' inRange = list( |
|
| 3173 |
#' color = c( |
|
| 3174 |
#' "#313695", |
|
| 3175 |
#' "#4575b4", |
|
| 3176 |
#' "#74add1", |
|
| 3177 |
#' "#abd9e9", |
|
| 3178 |
#' "#e0f3f8", |
|
| 3179 |
#' "#ffffbf", |
|
| 3180 |
#' "#fee090", |
|
| 3181 |
#' "#fdae61", |
|
| 3182 |
#' "#f46d43", |
|
| 3183 |
#' "#d73027", |
|
| 3184 |
#' "#a50026" |
|
| 3185 |
#' ) |
|
| 3186 |
#' ) |
|
| 3187 |
#' ) |> |
|
| 3188 |
#' e_x_axis( |
|
| 3189 |
#' splitLine = list(show = FALSE) |
|
| 3190 |
#' ) |> |
|
| 3191 |
#' e_y_axis( |
|
| 3192 |
#' splitLine = list(show = FALSE) |
|
| 3193 |
#' ) |
|
| 3194 |
#' |
|
| 3195 |
#' # map |
|
| 3196 |
#' latlong <- seq(-180, 180, by = 5) |
|
| 3197 |
#' wind <- expand.grid(lng = latlong, lat = latlong) |
|
| 3198 |
#' wind$slng <- rnorm(nrow(wind), 0, 200) |
|
| 3199 |
#' wind$slat <- rnorm(nrow(wind), 0, 200) |
|
| 3200 |
#' wind$color <- abs(wind$slat) - abs(wind$slng) |
|
| 3201 |
#' rng <- range(wind$color) |
|
| 3202 |
#' |
|
| 3203 |
#' trans <- list(opacity = 0.5) # transparency |
|
| 3204 |
#' |
|
| 3205 |
#' wind |> |
|
| 3206 |
#' e_charts(lng, backgroundColor = "#333") |> |
|
| 3207 |
#' e_geo() |> |
|
| 3208 |
#' e_flow_gl( |
|
| 3209 |
#' lat, |
|
| 3210 |
#' slng, |
|
| 3211 |
#' slat, |
|
| 3212 |
#' color, |
|
| 3213 |
#' itemStyle = trans, |
|
| 3214 |
#' particleSize = 2 |
|
| 3215 |
#' ) |> |
|
| 3216 |
#' e_visual_map( |
|
| 3217 |
#' color, |
|
| 3218 |
#' # range |
|
| 3219 |
#' dimension = 4, |
|
| 3220 |
#' # lng = 0, lat = 1, slng = 2, slat = 3, color = 4 |
|
| 3221 |
#' show = FALSE, |
|
| 3222 |
#' # hide |
|
| 3223 |
#' inRange = list( |
|
| 3224 |
#' color = c( |
|
| 3225 |
#' "#313695", |
|
| 3226 |
#' "#4575b4", |
|
| 3227 |
#' "#74add1", |
|
| 3228 |
#' "#abd9e9", |
|
| 3229 |
#' "#e0f3f8", |
|
| 3230 |
#' "#ffffbf", |
|
| 3231 |
#' "#fee090", |
|
| 3232 |
#' "#fdae61", |
|
| 3233 |
#' "#f46d43", |
|
| 3234 |
#' "#d73027", |
|
| 3235 |
#' "#a50026" |
|
| 3236 |
#' ) |
|
| 3237 |
#' ) |
|
| 3238 |
#' ) |> |
|
| 3239 |
#' e_x_axis(show = FALSE) |> |
|
| 3240 |
#' e_y_axis(show = FALSE) |
|
| 3241 |
#' @seealso \href{https://echarts.apache.org/en/option-gl.html#series-flowGL}{Additional arguments}
|
|
| 3242 |
#' |
|
| 3243 |
#' @rdname e_flow_gl |
|
| 3244 |
#' @export |
|
| 3245 |
e_flow_gl <- function(e, y, sx, sy, color, name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 3246 | 5x |
UseMethod("e_flow_gl")}
|
| 3247 | ||
| 3248 |
#' @export |
|
| 3249 |
#' @method e_flow_gl echarts4r |
|
| 3250 |
e_flow_gl.echarts4r <- function(e, y, sx, sy, color, name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 3251 | 5x |
if (missing(e)) {
|
| 3252 | 1x |
stop("must pass e", call. = FALSE)
|
| 3253 |
} |
|
| 3254 | ||
| 3255 | 4x |
if (missing(y) || missing(sx) || missing(sy)) {
|
| 3256 | 1x |
stop("must pass y, sx, and sy", call. = FALSE)
|
| 3257 |
} |
|
| 3258 | ||
| 3259 | 3x |
if (!missing(color)) {
|
| 3260 | 2x |
colour <- deparse(substitute(color)) |
| 3261 |
} else {
|
|
| 3262 | 1x |
colour <- NULL |
| 3263 |
} |
|
| 3264 | ||
| 3265 | 3x |
e_flow_gl_( |
| 3266 | 3x |
e = e, |
| 3267 | 3x |
y = deparse(substitute(y)), |
| 3268 | 3x |
sx = deparse(substitute(sx)), |
| 3269 | 3x |
sy = deparse(substitute(sy)), |
| 3270 | 3x |
color = colour, |
| 3271 | 3x |
name, |
| 3272 | 3x |
coord_system, |
| 3273 | 3x |
rm_x, |
| 3274 | 3x |
rm_y, |
| 3275 |
... |
|
| 3276 |
) |
|
| 3277 |
} |
|
| 3278 | ||
| 3279 |
#' @export |
|
| 3280 |
#' @method e_flow_gl echarts4rProxy |
|
| 3281 |
e_flow_gl.echarts4rProxy <- function(e, y, sx, sy, color, name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 3282 | 2x |
if (missing(y) || missing(sx) || missing(sy)) {
|
| 3283 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 3284 |
} |
|
| 3285 | ||
| 3286 | 1x |
if (!missing(color)) {
|
| 3287 | ! |
colour <- deparse(substitute(color)) |
| 3288 |
} else {
|
|
| 3289 | 1x |
colour <- NULL |
| 3290 |
} |
|
| 3291 | ||
| 3292 | 1x |
e$chart <- e_flow_gl_( |
| 3293 | 1x |
e = e$chart, |
| 3294 | 1x |
y = deparse(substitute(y)), |
| 3295 | 1x |
sx = deparse(substitute(sx)), |
| 3296 | 1x |
sy = deparse(substitute(sy)), |
| 3297 | 1x |
color = colour, |
| 3298 | 1x |
name, |
| 3299 | 1x |
coord_system, |
| 3300 | 1x |
rm_x, |
| 3301 | 1x |
rm_y, |
| 3302 |
... |
|
| 3303 |
) |
|
| 3304 | 1x |
return(e) |
| 3305 |
} |
|
| 3306 | ||
| 3307 |
#' Scatter GL |
|
| 3308 |
#' |
|
| 3309 |
#' Draw scatter GL. |
|
| 3310 |
#' |
|
| 3311 |
#' @inheritParams e_bar |
|
| 3312 |
#' @param y,z Column names containing y and z data. |
|
| 3313 |
#' @param coord_system Coordinate system to plot against. |
|
| 3314 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 3315 |
#' |
|
| 3316 |
#' @examples |
|
| 3317 |
#' quakes |> |
|
| 3318 |
#' e_charts(long) |> |
|
| 3319 |
#' e_geo( |
|
| 3320 |
#' roam = TRUE, |
|
| 3321 |
#' boundingCoords = list( |
|
| 3322 |
#' c(185, -10), |
|
| 3323 |
#' c(165, -40) |
|
| 3324 |
#' ) |
|
| 3325 |
#' ) |> |
|
| 3326 |
#' e_scatter_gl(lat, depth) |
|
| 3327 |
#' |
|
| 3328 |
#' # timeline |
|
| 3329 |
#' quakes$year <- rep(c("2017", "2018"), 500)
|
|
| 3330 |
#' |
|
| 3331 |
#' quakes |> |
|
| 3332 |
#' group_by(year) |> |
|
| 3333 |
#' e_charts(long, timeline = TRUE) |> |
|
| 3334 |
#' e_geo( |
|
| 3335 |
#' roam = TRUE, |
|
| 3336 |
#' boundingCoords = list( |
|
| 3337 |
#' c(185, -10), |
|
| 3338 |
#' c(165, -40) |
|
| 3339 |
#' ) |
|
| 3340 |
#' ) |> |
|
| 3341 |
#' e_scatter_gl(lat, depth) |
|
| 3342 |
#' @seealso \href{https://echarts.apache.org/en/option-gl.html#series-scatterGL}{Additional arguments}
|
|
| 3343 |
#' |
|
| 3344 |
#' @rdname e_scatter_gl |
|
| 3345 |
#' @export |
|
| 3346 |
e_scatter_gl <- function(e, y, z, name = NULL, coord_system = "geo", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 3347 | 6x |
UseMethod("e_scatter_gl")}
|
| 3348 | ||
| 3349 |
#' @export |
|
| 3350 |
#' @method e_scatter_gl echarts4r |
|
| 3351 |
e_scatter_gl.echarts4r <- function(e, y, z, name = NULL, coord_system = "geo", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 3352 | 6x |
if (missing(e)) {
|
| 3353 | 1x |
stop("must pass e", call. = FALSE)
|
| 3354 |
} |
|
| 3355 | ||
| 3356 | 5x |
if (missing(y) || missing(z)) {
|
| 3357 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 3358 |
} |
|
| 3359 | ||
| 3360 | 4x |
z_serie <- NULL |
| 3361 | 4x |
if (!missing(z)) {
|
| 3362 | 4x |
z_serie <- deparse(substitute(z)) |
| 3363 |
} |
|
| 3364 | ||
| 3365 | 4x |
e_scatter_gl_( |
| 3366 | 4x |
e, |
| 3367 | 4x |
deparse(substitute(y)), |
| 3368 | 4x |
z_serie, |
| 3369 | 4x |
name, |
| 3370 | 4x |
coord_system, |
| 3371 | 4x |
rm_x, |
| 3372 | 4x |
rm_y, |
| 3373 |
... |
|
| 3374 |
) |
|
| 3375 |
} |
|
| 3376 | ||
| 3377 |
#' @export |
|
| 3378 |
#' @method e_scatter_gl echarts4rProxy |
|
| 3379 |
e_scatter_gl.echarts4rProxy <- function(e, y, z, name = NULL, coord_system = "geo", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 3380 | 2x |
if (missing(y) || missing(z)) {
|
| 3381 | 1x |
stop("must pass y and z", call. = FALSE)
|
| 3382 |
} |
|
| 3383 | ||
| 3384 | 1x |
e$chart <- e_scatter_gl_( |
| 3385 | 1x |
e$chart, |
| 3386 | 1x |
deparse(substitute(y)), |
| 3387 | 1x |
deparse(substitute(z)), |
| 3388 | 1x |
name, |
| 3389 | 1x |
coord_system, |
| 3390 | 1x |
rm_x, |
| 3391 | 1x |
rm_y, |
| 3392 |
... |
|
| 3393 |
) |
|
| 3394 | 1x |
return(e) |
| 3395 |
} |
|
| 3396 | ||
| 3397 |
#' Pictorial |
|
| 3398 |
#' |
|
| 3399 |
#' Pictorial bar chart is a type of bar chart that customized glyph |
|
| 3400 |
#' (like images, SVG PathData) can be used instead of rectangular bar. |
|
| 3401 |
#' |
|
| 3402 |
#' @inheritParams e_bar |
|
| 3403 |
#' @param symbol Symbol to plot. |
|
| 3404 |
#' |
|
| 3405 |
#' @section Symbols: |
|
| 3406 |
#' \describe{
|
|
| 3407 |
#' \item{Built-in}{ \code{circle}, \code{rect}, \code{roundRect}, \code{triangle}, \code{diamond},
|
|
| 3408 |
#' \code{pin}, \code{arrow}.}
|
|
| 3409 |
#' \item{SVG Path}{ Path data for SVG graphics.}
|
|
| 3410 |
#' \item{Images}{ Path to image, don't forget to precede it with \code{image://}, see examples.}
|
|
| 3411 |
#' } |
|
| 3412 |
#' |
|
| 3413 |
#' @examples |
|
| 3414 |
#' # built-in symbols |
|
| 3415 |
#' y <- rnorm(10, 10, 2) |
|
| 3416 |
#' df <- data.frame( |
|
| 3417 |
#' x = 1:10, |
|
| 3418 |
#' y = y, |
|
| 3419 |
#' z = y - rnorm(10, 5, 1) |
|
| 3420 |
#' ) |
|
| 3421 |
#' |
|
| 3422 |
#' df |> |
|
| 3423 |
#' e_charts(x) |> |
|
| 3424 |
#' e_bar(z, barWidth = 10) |> |
|
| 3425 |
#' e_pictorial( |
|
| 3426 |
#' y, |
|
| 3427 |
#' symbol = "rect", |
|
| 3428 |
#' symbolRepeat = TRUE, |
|
| 3429 |
#' z = -1, |
|
| 3430 |
#' symbolSize = c(10, 4) |
|
| 3431 |
#' ) |> |
|
| 3432 |
#' e_theme("westeros")
|
|
| 3433 |
#' |
|
| 3434 |
#' # svg path |
|
| 3435 |
#' path <- "path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z" |
|
| 3436 |
#' |
|
| 3437 |
#' style <- list( |
|
| 3438 |
#' normal = list(opacity = 0.5), |
|
| 3439 |
#' # normal |
|
| 3440 |
#' emphasis = list(opacity = 1) # on hover |
|
| 3441 |
#' ) |
|
| 3442 |
#' |
|
| 3443 |
#' df |> |
|
| 3444 |
#' e_charts(x) |> |
|
| 3445 |
#' e_pictorial( |
|
| 3446 |
#' y, |
|
| 3447 |
#' symbol = path, |
|
| 3448 |
#' barCategoryGap = "-130%", |
|
| 3449 |
#' itemStyle = style |
|
| 3450 |
#' ) |
|
| 3451 |
#' |
|
| 3452 |
#' # image |
|
| 3453 |
#' # might not work in RStudio viewer |
|
| 3454 |
#' # open in browser |
|
| 3455 |
#' qomo <- paste0( |
|
| 3456 |
#' "https://ecomfe.github.io/echarts-examples/public/", |
|
| 3457 |
#' "data/asset/img/hill-Qomolangma.png" |
|
| 3458 |
#' ) |
|
| 3459 |
#' |
|
| 3460 |
#' kili <- paste0( |
|
| 3461 |
#' "https://ecomfe.github.io/echarts-examples/public/", |
|
| 3462 |
#' "data/asset/img/hill-Kilimanjaro.png" |
|
| 3463 |
#' ) |
|
| 3464 |
#' |
|
| 3465 |
#' data <- data.frame( |
|
| 3466 |
#' x = c("Qomolangma", "Kilimanjaro"),
|
|
| 3467 |
#' value = c(8844, 5895), |
|
| 3468 |
#' symbol = c( |
|
| 3469 |
#' paste0("image://", qomo),
|
|
| 3470 |
#' paste0("image://", kili)
|
|
| 3471 |
#' ) |
|
| 3472 |
#' ) |
|
| 3473 |
#' |
|
| 3474 |
#' data |> |
|
| 3475 |
#' e_charts(x) |> |
|
| 3476 |
#' e_pictorial(value, symbol) |> |
|
| 3477 |
#' e_legend(FALSE) |
|
| 3478 |
#' |
|
| 3479 |
#' # timeline |
|
| 3480 |
#' df <- data.frame( |
|
| 3481 |
#' x = rep(1:5, 2), |
|
| 3482 |
#' y = runif(10, 1, 10), |
|
| 3483 |
#' year = c( |
|
| 3484 |
#' rep(2017, 5), |
|
| 3485 |
#' rep(2018, 5) |
|
| 3486 |
#' ) |
|
| 3487 |
#' ) |
|
| 3488 |
#' |
|
| 3489 |
#' df |> |
|
| 3490 |
#' group_by(year) |> |
|
| 3491 |
#' e_charts(x, timeline = TRUE) |> |
|
| 3492 |
#' e_pictorial( |
|
| 3493 |
#' y, |
|
| 3494 |
#' symbol = "rect", |
|
| 3495 |
#' symbolRepeat = TRUE, |
|
| 3496 |
#' z = -1, |
|
| 3497 |
#' symbolSize = c(10, 4) |
|
| 3498 |
#' ) |
|
| 3499 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-pictorialBar}{Additional arguments}
|
|
| 3500 |
#' |
|
| 3501 |
#' @rdname e_pictorial |
|
| 3502 |
#' @export |
|
| 3503 | 6x |
e_pictorial <- function(e, serie, symbol, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, ...) UseMethod("e_pictorial")
|
| 3504 | ||
| 3505 |
#' @export |
|
| 3506 |
#' @method e_pictorial echarts4r |
|
| 3507 |
e_pictorial.echarts4r <- function(e, serie, symbol, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, ...) {
|
|
| 3508 | 6x |
if (missing(e)) {
|
| 3509 | 1x |
stop("must pass e", call. = FALSE)
|
| 3510 |
} |
|
| 3511 | 5x |
if (missing(serie) || missing(symbol)) {
|
| 3512 | 1x |
stop("must pass serie and symbol", call. = FALSE)
|
| 3513 |
} |
|
| 3514 | ||
| 3515 | 4x |
bd <- .get_bind(deparse(substitute(bind))) |
| 3516 | ||
| 3517 |
# only deparse if it is a column name |
|
| 3518 | 4x |
if (deparse(substitute(symbol)) %in% colnames(e$x$data[[1]])) {
|
| 3519 | ! |
symbol <- deparse(substitute(symbol)) |
| 3520 |
} |
|
| 3521 | ||
| 3522 | 4x |
e_pictorial_( |
| 3523 | 4x |
e, |
| 3524 | 4x |
deparse(substitute(serie)), |
| 3525 | 4x |
symbol, |
| 3526 | 4x |
bd, |
| 3527 | 4x |
name, |
| 3528 | 4x |
legend, |
| 3529 | 4x |
y_index, |
| 3530 | 4x |
x_index, |
| 3531 |
... |
|
| 3532 |
) |
|
| 3533 |
} |
|
| 3534 | ||
| 3535 |
#' @export |
|
| 3536 |
#' @method e_pictorial echarts4rProxy |
|
| 3537 |
e_pictorial.echarts4rProxy <- function(e, serie, symbol, bind, name = NULL, legend = TRUE, y_index = 0, x_index = 0, ...) {
|
|
| 3538 | 2x |
if (missing(serie) || missing(symbol)) {
|
| 3539 | 1x |
stop("must pass serie and symbol", call. = FALSE)
|
| 3540 |
} |
|
| 3541 | ||
| 3542 | 1x |
bd <- .get_bind(deparse(substitute(bind))) |
| 3543 | ||
| 3544 |
# only deparse if it is a column name |
|
| 3545 | 1x |
if (deparse(substitute(symbol)) %in% colnames(e$chart$x$data[[1]])) {
|
| 3546 | 1x |
symbol <- deparse(substitute(symbol)) |
| 3547 |
} |
|
| 3548 | ||
| 3549 | 1x |
e$chart <- e_pictorial_( |
| 3550 | 1x |
e$chart, |
| 3551 | 1x |
deparse(substitute(serie)), |
| 3552 | 1x |
symbol, |
| 3553 | 1x |
bd, |
| 3554 | 1x |
name, |
| 3555 | 1x |
legend, |
| 3556 | 1x |
y_index, |
| 3557 | 1x |
x_index, |
| 3558 |
... |
|
| 3559 |
) |
|
| 3560 | 1x |
return(e) |
| 3561 |
} |
|
| 3562 | ||
| 3563 |
#' Smooth |
|
| 3564 |
#' |
|
| 3565 |
#' Plot formulas. |
|
| 3566 |
#' |
|
| 3567 |
#' @inheritParams e_bar |
|
| 3568 |
#' @param formula formula to pass to \code{\link{lm}}.
|
|
| 3569 |
#' @param symbol Symbol to use in \code{\link{e_line}}.
|
|
| 3570 |
#' @param smooth Whether to smooth the line. |
|
| 3571 |
#' @param model_args Arguments to pass to the underlying model. |
|
| 3572 |
#' @param ... Additional arguments to pass to \code{\link{e_line}}.
|
|
| 3573 |
#' |
|
| 3574 |
#' @examples |
|
| 3575 |
#' iris |> |
|
| 3576 |
#' group_by(Species) |> |
|
| 3577 |
#' e_charts(Sepal.Length) |> |
|
| 3578 |
#' e_scatter(Sepal.Width) |> |
|
| 3579 |
#' e_lm(Sepal.Width ~ Sepal.Length) |> |
|
| 3580 |
#' e_x_axis(min = 4) |
|
| 3581 |
#' |
|
| 3582 |
#' mtcars |> |
|
| 3583 |
#' e_charts(disp) |> |
|
| 3584 |
#' e_scatter(mpg, qsec) |> |
|
| 3585 |
#' e_loess(mpg ~ disp, smooth = TRUE, showSymbol = FALSE) |
|
| 3586 |
#' |
|
| 3587 |
#' # timeline |
|
| 3588 |
#' iris |> |
|
| 3589 |
#' group_by(Species) |> |
|
| 3590 |
#' e_charts(Sepal.Length, timeline = TRUE) |> |
|
| 3591 |
#' e_scatter(Sepal.Width) |> |
|
| 3592 |
#' e_lm(Sepal.Width ~ Sepal.Length) |> |
|
| 3593 |
#' e_x_axis(min = 4, max = 8) |> |
|
| 3594 |
#' e_y_axis(max = 5) |
|
| 3595 |
#' @rdname smooth |
|
| 3596 |
#' @export |
|
| 3597 | 4x |
e_lm <- function(e, formula, name = NULL, legend = TRUE, symbol = "none", smooth = TRUE, model_args = list(), ...) UseMethod("e_lm")
|
| 3598 | ||
| 3599 |
#' @export |
|
| 3600 |
#' @method e_lm echarts4r |
|
| 3601 |
#' @importFrom stats complete.cases |
|
| 3602 |
e_lm.echarts4r <- function(e, formula, name = NULL, legend = TRUE, symbol = "none", smooth = TRUE, model_args = list(), ...) {
|
|
| 3603 | 5x |
if (missing(e)) {
|
| 3604 | 1x |
stop("must pass e", call. = FALSE)
|
| 3605 |
} |
|
| 3606 | ||
| 3607 | 4x |
form <- as.formula(formula) |
| 3608 | ||
| 3609 | 4x |
if (!is.null(name) && length(name) != length(e$x$data)) {
|
| 3610 | 1x |
stop("More groups than names", call. = FALSE)
|
| 3611 |
} |
|
| 3612 | ||
| 3613 | 3x |
for (i in seq_along(e$x$data)) {
|
| 3614 | 8x |
e$x$data[[i]] <- e$x$data[[i]][stats::complete.cases(e$x$data[[i]]), ] |
| 3615 | ||
| 3616 | 8x |
model_args$formula <- form |
| 3617 | 8x |
model_args$data <- e$x$data[[i]] |
| 3618 | ||
| 3619 | 8x |
model <- tryCatch( |
| 3620 | 8x |
do.call(lm, model_args), |
| 3621 | 8x |
error = function(e) e |
| 3622 |
) |
|
| 3623 | ||
| 3624 | 8x |
if (!inherits(model, "error")) {
|
| 3625 | 8x |
data <- broom::augment(model) |
| 3626 | 8x |
data <- data |> dplyr::select(-dplyr::one_of(names(model$model))) |
| 3627 | ||
| 3628 | 8x |
e$x$data[[i]] <- dplyr::bind_cols(e$x$data[[i]], data) |
| 3629 | ||
| 3630 | 8x |
vector <- .build_data2(e$x$data[[i]], e$x$mapping$x, ".fitted") |
| 3631 | ||
| 3632 | 8x |
l_data <- list(data = vector) |
| 3633 | ||
| 3634 | 8x |
l <- list( |
| 3635 | 8x |
name = name, |
| 3636 | 8x |
type = "line", |
| 3637 | 8x |
symbol = symbol, |
| 3638 | 8x |
smooth = smooth, |
| 3639 |
... |
|
| 3640 |
) |
|
| 3641 | ||
| 3642 | 8x |
if (!e$x$tl) {
|
| 3643 | 5x |
if (is.null(name)) {
|
| 3644 | 5x |
nm <- paste0(names(e$x$data[[i]])[i], "-lm") |
| 3645 |
} else {
|
|
| 3646 | ! |
nm <- name[i] |
| 3647 |
} |
|
| 3648 | ||
| 3649 | 5x |
l$name <- nm |
| 3650 | ||
| 3651 | 5x |
l <- append(l, l_data) |
| 3652 | ||
| 3653 | 5x |
e$x$opts$series <- append(e$x$opts$series, list(l)) |
| 3654 | ||
| 3655 | 5x |
if (isTRUE(legend)) {
|
| 3656 | 5x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 3657 |
} |
|
| 3658 |
} else {
|
|
| 3659 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(l_data)) |
| 3660 |
} |
|
| 3661 |
} |
|
| 3662 |
} |
|
| 3663 | ||
| 3664 | 3x |
if (isTRUE(e$x$tl) && !inherits(model, "error")) {
|
| 3665 | 1x |
if (isTRUE(legend) && !is.null(name)) {
|
| 3666 | ! |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 3667 |
} |
|
| 3668 | ||
| 3669 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(l)) |
| 3670 |
} |
|
| 3671 | ||
| 3672 | 3x |
e |
| 3673 |
} |
|
| 3674 | ||
| 3675 |
#' @export |
|
| 3676 |
#' @method e_lm echarts4rProxy |
|
| 3677 |
#' @importFrom stats complete.cases |
|
| 3678 |
e_lm.echarts4rProxy <- function(e, formula, name = NULL, legend = TRUE, symbol = "none", smooth = TRUE, model_args = list(), ...) {
|
|
| 3679 | 1x |
form <- as.formula(formula) |
| 3680 | ||
| 3681 | 1x |
for (i in seq_along(e$chart$x$data)) {
|
| 3682 | 1x |
e$chart$x$data[[i]] <- e$chart$x$data[[i]][stats::complete.cases(e$chart$x$data[[i]]), ] |
| 3683 | ||
| 3684 | 1x |
model_args$formula <- form |
| 3685 | 1x |
model_args$data <- e$chart$x$data[[i]] |
| 3686 | ||
| 3687 | 1x |
model <- tryCatch( |
| 3688 | 1x |
do.call(lm, model_args), |
| 3689 | 1x |
error = function(e) e |
| 3690 |
) |
|
| 3691 | ||
| 3692 | 1x |
if (!inherits(model, "error")) {
|
| 3693 | 1x |
data <- broom::augment(model) |
| 3694 | 1x |
data <- data |> dplyr::select(-dplyr::one_of(names(model$model))) |
| 3695 | ||
| 3696 | 1x |
e$chart$x$data[[i]] <- dplyr::bind_cols(e$chart$x$data[[i]], data) |
| 3697 | ||
| 3698 | 1x |
vector <- .build_data2(e$chart$x$data[[i]], e$chart$x$mapping$x, ".fitted") |
| 3699 | ||
| 3700 | 1x |
l_data <- list(data = vector) |
| 3701 | ||
| 3702 | 1x |
l <- list( |
| 3703 | 1x |
name = name, |
| 3704 | 1x |
type = "line", |
| 3705 | 1x |
symbol = symbol, |
| 3706 | 1x |
smooth = smooth, |
| 3707 |
... |
|
| 3708 |
) |
|
| 3709 | ||
| 3710 | 1x |
if (!e$chart$x$tl) {
|
| 3711 | 1x |
if (is.null(name)) {
|
| 3712 | 1x |
nm <- paste0(names(e$chart$x$data[[i]])[i], "-lm") |
| 3713 |
} else {
|
|
| 3714 | ! |
nm <- name |
| 3715 |
} |
|
| 3716 | ||
| 3717 | 1x |
l$name <- nm |
| 3718 | ||
| 3719 | 1x |
l <- append(l, l_data) |
| 3720 | ||
| 3721 | 1x |
e$chart$x$opts$series <- append(e$chart$x$opts$series, list(l)) |
| 3722 | ||
| 3723 | 1x |
if (isTRUE(legend)) {
|
| 3724 | 1x |
e$chart$x$opts$legend$data <- append(e$chart$x$opts$legend$data, list(nm)) |
| 3725 |
} |
|
| 3726 |
} else {
|
|
| 3727 | ! |
e$chart$x$opts$options[[i]]$series <- append(e$chart$x$opts$options[[i]]$series, list(l_data)) |
| 3728 |
} |
|
| 3729 |
} |
|
| 3730 |
} |
|
| 3731 | ||
| 3732 | 1x |
if (isTRUE(e$chart$x$tl) && !inherits(model, "error")) {
|
| 3733 | ! |
if (isTRUE(legend) && !is.null(name)) {
|
| 3734 | ! |
e$chart$x$opts$baseOption$legend$data <- append(e$chart$x$opts$baseOption$legend$data, list(name)) |
| 3735 |
} |
|
| 3736 | ||
| 3737 | ! |
e$chart$x$opts$baseOption$series <- append(e$chart$x$opts$baseOption$series, list(l)) |
| 3738 |
} |
|
| 3739 | ||
| 3740 | 1x |
e |
| 3741 |
} |
|
| 3742 | ||
| 3743 |
#' @rdname smooth |
|
| 3744 |
#' @export |
|
| 3745 | 4x |
e_glm <- function(e, formula, name = NULL, legend = TRUE, symbol = "none", smooth = TRUE, model_args = list(), ...) UseMethod("e_glm")
|
| 3746 | ||
| 3747 |
#' @export |
|
| 3748 |
#' @method e_glm echarts4r |
|
| 3749 |
#' @importFrom stats complete.cases |
|
| 3750 |
e_glm.echarts4r <- function(e, formula, name = NULL, legend = TRUE, symbol = "none", smooth = TRUE, model_args = list(), ...) {
|
|
| 3751 | 4x |
if (missing(e)) {
|
| 3752 | 1x |
stop("must pass e", call. = FALSE)
|
| 3753 |
} |
|
| 3754 | ||
| 3755 | 3x |
form <- as.formula(formula) |
| 3756 | ||
| 3757 | 3x |
for (i in seq_along(e$x$data)) {
|
| 3758 | 8x |
e$x$data[[i]] <- e$x$data[[i]][stats::complete.cases(e$x$data[[i]]), ] |
| 3759 | ||
| 3760 | 8x |
model_args$formula <- form |
| 3761 | 8x |
model_args$data <- e$x$data[[i]] |
| 3762 | ||
| 3763 | 8x |
model <- tryCatch( |
| 3764 | 8x |
do.call(glm, model_args), |
| 3765 | 8x |
error = function(e) e |
| 3766 |
) |
|
| 3767 | ||
| 3768 | 8x |
if (!inherits(model, "error")) {
|
| 3769 | 8x |
data <- broom::augment(model) |
| 3770 | 8x |
data <- data |> dplyr::select(-dplyr::one_of(names(model$model))) |
| 3771 | ||
| 3772 | 8x |
e$x$data[[i]] <- dplyr::bind_cols(e$x$data[[i]], data) |
| 3773 | ||
| 3774 | 8x |
vector <- .build_data2(e$x$data[[i]], e$x$mapping$x, ".fitted") |
| 3775 | ||
| 3776 | 8x |
l_data <- list(data = vector) |
| 3777 | ||
| 3778 | 8x |
l <- list( |
| 3779 | 8x |
name = name, |
| 3780 | 8x |
type = "line", |
| 3781 | 8x |
symbol = symbol, |
| 3782 | 8x |
smooth = smooth, |
| 3783 |
... |
|
| 3784 |
) |
|
| 3785 | ||
| 3786 | 8x |
if (!e$x$tl) {
|
| 3787 | 5x |
if (is.null(name)) {
|
| 3788 | 5x |
nm <- paste0(names(e$x$data[[i]])[i], "-glm") |
| 3789 |
} else {
|
|
| 3790 | ! |
nm <- name |
| 3791 |
} |
|
| 3792 | ||
| 3793 | 5x |
l$name <- nm |
| 3794 | ||
| 3795 | 5x |
l <- append(l, l_data) |
| 3796 | ||
| 3797 | 5x |
e$x$opts$series <- append(e$x$opts$series, list(l)) |
| 3798 | ||
| 3799 | 5x |
if (isTRUE(legend)) {
|
| 3800 | 5x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 3801 |
} |
|
| 3802 |
} else {
|
|
| 3803 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(l_data)) |
| 3804 |
} |
|
| 3805 |
} |
|
| 3806 |
} |
|
| 3807 | ||
| 3808 | 3x |
if (isTRUE(e$x$tl) && !inherits(model, "error")) {
|
| 3809 | 1x |
if (isTRUE(legend) && !is.null(name)) {
|
| 3810 | ! |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 3811 |
} |
|
| 3812 | ||
| 3813 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(l)) |
| 3814 |
} |
|
| 3815 | ||
| 3816 | 3x |
e |
| 3817 |
} |
|
| 3818 | ||
| 3819 |
#' @export |
|
| 3820 |
#' @method e_glm echarts4rProxy |
|
| 3821 |
#' @importFrom stats complete.cases |
|
| 3822 |
e_glm.echarts4rProxy <- function(e, formula, name = NULL, legend = TRUE, symbol = "none", smooth = TRUE, model_args = list(), ...) {
|
|
| 3823 | 1x |
form <- as.formula(formula) |
| 3824 | ||
| 3825 | 1x |
for (i in seq_along(e$chart$x$data)) {
|
| 3826 | 1x |
e$chart$x$data[[i]] <- e$chart$x$data[[i]][stats::complete.cases(e$chart$x$data[[i]]), ] |
| 3827 | ||
| 3828 | 1x |
model_args$formula <- form |
| 3829 | 1x |
model_args$data <- e$chart$x$data[[i]] |
| 3830 | ||
| 3831 | 1x |
model <- tryCatch( |
| 3832 | 1x |
do.call(glm, model_args), |
| 3833 | 1x |
error = function(e) e |
| 3834 |
) |
|
| 3835 | ||
| 3836 | 1x |
if (!inherits(model, "error")) {
|
| 3837 | 1x |
data <- broom::augment(model) |
| 3838 | 1x |
data <- data |> dplyr::select(-dplyr::one_of(names(model$model))) |
| 3839 | ||
| 3840 | 1x |
e$chart$x$data[[i]] <- dplyr::bind_cols(e$chart$x$data[[i]], data) |
| 3841 | ||
| 3842 | 1x |
vector <- .build_data2(e$chart$x$data[[i]], e$chart$x$mapping$x, ".fitted") |
| 3843 | ||
| 3844 | 1x |
l_data <- list(data = vector) |
| 3845 | ||
| 3846 | 1x |
l <- list( |
| 3847 | 1x |
name = name, |
| 3848 | 1x |
type = "line", |
| 3849 | 1x |
symbol = symbol, |
| 3850 | 1x |
smooth = smooth, |
| 3851 |
... |
|
| 3852 |
) |
|
| 3853 | ||
| 3854 | 1x |
if (!e$chart$x$tl) {
|
| 3855 | 1x |
if (is.null(name)) {
|
| 3856 | 1x |
nm <- paste0(names(e$chart$x$data[[i]])[i], "-glm") |
| 3857 |
} else {
|
|
| 3858 | ! |
nm <- name |
| 3859 |
} |
|
| 3860 | ||
| 3861 | 1x |
l$name <- nm |
| 3862 | ||
| 3863 | 1x |
l <- append(l, l_data) |
| 3864 | ||
| 3865 | 1x |
e$chart$x$opts$series <- append(e$chart$x$opts$series, list(l)) |
| 3866 | ||
| 3867 | 1x |
if (isTRUE(legend)) {
|
| 3868 | 1x |
e$chart$x$opts$legend$data <- append(e$chart$x$opts$legend$data, list(nm)) |
| 3869 |
} |
|
| 3870 |
} else {
|
|
| 3871 | ! |
e$chart$x$opts$options[[i]]$series <- append(e$chart$x$opts$options[[i]]$series, list(l_data)) |
| 3872 |
} |
|
| 3873 |
} |
|
| 3874 |
} |
|
| 3875 | ||
| 3876 | 1x |
if (isTRUE(e$chart$x$tl) && !inherits(model, "error")) {
|
| 3877 | ! |
if (isTRUE(legend) && !is.null(name)) {
|
| 3878 | ! |
e$chart$x$opts$baseOption$legend$data <- append(e$chart$x$opts$baseOption$legend$data, list(name)) |
| 3879 |
} |
|
| 3880 | ||
| 3881 | ! |
e$chart$x$opts$baseOption$series <- append(e$chart$x$opts$baseOption$series, list(l)) |
| 3882 |
} |
|
| 3883 | ||
| 3884 | 1x |
e |
| 3885 |
} |
|
| 3886 | ||
| 3887 |
#' @rdname smooth |
|
| 3888 |
#' @export |
|
| 3889 |
e_loess <- function( |
|
| 3890 |
e, |
|
| 3891 |
formula, |
|
| 3892 |
name = NULL, |
|
| 3893 |
legend = TRUE, |
|
| 3894 |
symbol = "none", |
|
| 3895 |
smooth = TRUE, |
|
| 3896 |
x_index = 0, |
|
| 3897 |
y_index = 0, |
|
| 3898 |
model_args = list(), |
|
| 3899 |
...) {
|
|
| 3900 | 5x |
UseMethod("e_loess")
|
| 3901 |
} |
|
| 3902 | ||
| 3903 |
#' @export |
|
| 3904 |
#' @method e_loess echarts4r |
|
| 3905 |
#' @importFrom stats complete.cases |
|
| 3906 |
e_loess.echarts4r <- function( |
|
| 3907 |
e, |
|
| 3908 |
formula, |
|
| 3909 |
name = NULL, |
|
| 3910 |
legend = TRUE, |
|
| 3911 |
symbol = "none", |
|
| 3912 |
smooth = TRUE, |
|
| 3913 |
x_index = 0, |
|
| 3914 |
y_index = 0, |
|
| 3915 |
model_args = list(), |
|
| 3916 |
...) {
|
|
| 3917 | ||
| 3918 | 5x |
if (missing(e)) {
|
| 3919 | 1x |
stop("must pass e", call. = FALSE)
|
| 3920 |
} |
|
| 3921 | ||
| 3922 | 4x |
for (i in seq_along(e$x$data)) {
|
| 3923 | 8x |
e$x$data[[i]] <- e$x$data[[i]][stats::complete.cases(e$x$data[[i]]), ] |
| 3924 | ||
| 3925 | 8x |
model_args$formula <- as.formula(formula) |
| 3926 | 8x |
model_args$data <- e$x$data[[i]] |
| 3927 | ||
| 3928 | 8x |
mod <- tryCatch( |
| 3929 | 8x |
do.call(loess, model_args), |
| 3930 | 8x |
error = function(e) e |
| 3931 |
) |
|
| 3932 | ||
| 3933 | 8x |
if (!inherits(mod, "error")) {
|
| 3934 | 8x |
ser <- "ECHARTS4RLOESS" |
| 3935 | ||
| 3936 | 8x |
e$x$data[[i]][, ser] <- predict(mod) |
| 3937 | ||
| 3938 | 8x |
vector <- .build_data2( |
| 3939 | 8x |
e$x$data[[i]], |
| 3940 | 8x |
e$x$mapping$x, |
| 3941 | 8x |
ser |
| 3942 |
) |
|
| 3943 | ||
| 3944 | 8x |
l_data <- list(data = vector) |
| 3945 | ||
| 3946 | 8x |
l_opts <- list( |
| 3947 | 8x |
type = "line", |
| 3948 | 8x |
yAxisIndex = y_index, |
| 3949 | 8x |
xAxisIndex = x_index, |
| 3950 | 8x |
name = name, |
| 3951 |
... |
|
| 3952 |
) |
|
| 3953 | ||
| 3954 | 8x |
if (y_index != 0) {
|
| 3955 | 1x |
serie <- names(e$x$data[[i]])[i] |
| 3956 | 1x |
e <- .set_y_axis(e, serie, y_index) |
| 3957 |
} |
|
| 3958 | ||
| 3959 | 8x |
if (x_index != 0) {
|
| 3960 | 1x |
e <- .set_x_axis(e, x_index) |
| 3961 |
} |
|
| 3962 | ||
| 3963 | 8x |
if (!e$x$tl) {
|
| 3964 | 5x |
if (is.null(name)) {
|
| 3965 | 4x |
nm <- paste0(names(e$x$data[[i]])[i], "-loess") |
| 3966 |
} else {
|
|
| 3967 | 1x |
nm <- name |
| 3968 |
} |
|
| 3969 | ||
| 3970 | 5x |
l_opts$name <- nm |
| 3971 | ||
| 3972 | 5x |
l_data <- append(l_data, l_opts) |
| 3973 | ||
| 3974 | 5x |
if (isTRUE(legend) && !is.null(nm)) {
|
| 3975 | 5x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(nm)) |
| 3976 |
} |
|
| 3977 | ||
| 3978 | 5x |
e$x$opts$series <- append(e$x$opts$series, list(l_data)) |
| 3979 |
} else {
|
|
| 3980 | 3x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(l_data)) |
| 3981 |
} |
|
| 3982 |
} |
|
| 3983 |
} |
|
| 3984 | ||
| 3985 | 4x |
if (isTRUE(e$x$tl) && !inherits(mod, "error")) {
|
| 3986 | 1x |
if (isTRUE(legend) && !is.null(name)) {
|
| 3987 | ! |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 3988 |
} |
|
| 3989 | ||
| 3990 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(l_opts)) |
| 3991 |
} |
|
| 3992 | ||
| 3993 | 4x |
e |
| 3994 |
} |
|
| 3995 | ||
| 3996 |
#' @export |
|
| 3997 |
#' @method e_loess echarts4rProxy |
|
| 3998 |
#' @importFrom stats complete.cases |
|
| 3999 |
e_loess.echarts4rProxy <- function( |
|
| 4000 |
e, |
|
| 4001 |
formula, |
|
| 4002 |
name = NULL, |
|
| 4003 |
legend = TRUE, |
|
| 4004 |
symbol = "none", |
|
| 4005 |
smooth = TRUE, |
|
| 4006 |
x_index = 0, |
|
| 4007 |
y_index = 0, |
|
| 4008 |
model_args = list(), |
|
| 4009 |
...) {
|
|
| 4010 | 1x |
for (i in seq_along(e$chart$x$data)) {
|
| 4011 | 1x |
e$chart$x$data[[i]] <- e$chart$x$data[[i]][stats::complete.cases(e$chart$x$data[[i]]), ] |
| 4012 | ||
| 4013 | 1x |
model_args$formula <- as.formula(formula) |
| 4014 | 1x |
model_args$data <- e$chart$x$data[[i]] |
| 4015 | ||
| 4016 | 1x |
mod <- tryCatch( |
| 4017 | 1x |
do.call(loess, model_args), |
| 4018 | 1x |
error = function(e) e |
| 4019 |
) |
|
| 4020 | ||
| 4021 | 1x |
if (!inherits(mod, "error")) {
|
| 4022 | 1x |
ser <- "ECHARTS4RLOESS" |
| 4023 | ||
| 4024 | 1x |
e$chart$x$data[[i]][, ser] <- predict(mod) |
| 4025 | ||
| 4026 | 1x |
vector <- .build_data2( |
| 4027 | 1x |
e$chart$x$data[[i]], |
| 4028 | 1x |
e$chart$x$mapping$x, |
| 4029 | 1x |
ser |
| 4030 |
) |
|
| 4031 | ||
| 4032 | 1x |
l_data <- list(data = vector) |
| 4033 | ||
| 4034 | 1x |
l_opts <- list( |
| 4035 | 1x |
type = "line", |
| 4036 | 1x |
yAxisIndex = y_index, |
| 4037 | 1x |
xAxisIndex = x_index, |
| 4038 | 1x |
name = name, |
| 4039 |
... |
|
| 4040 |
) |
|
| 4041 | ||
| 4042 | 1x |
if (y_index != 0) {
|
| 4043 | ! |
serie <- names(e$x$data[[i]])[i] |
| 4044 | ! |
e <- .set_y_axis(e, serie, y_index, i) |
| 4045 |
} |
|
| 4046 | ||
| 4047 | 1x |
if (x_index != 0) {
|
| 4048 | ! |
e <- .set_x_axis(e, x_index, i) |
| 4049 |
} |
|
| 4050 | ||
| 4051 | 1x |
if (!e$chart$x$tl) {
|
| 4052 | 1x |
if (is.null(name)) {
|
| 4053 | 1x |
nm <- paste0(names(e$chart$x$data[[i]])[i], "-loess") |
| 4054 |
} else {
|
|
| 4055 | ! |
nm <- name |
| 4056 |
} |
|
| 4057 | ||
| 4058 | 1x |
l_opts$name <- nm |
| 4059 | ||
| 4060 | 1x |
l_data <- append(l_data, l_opts) |
| 4061 | ||
| 4062 | 1x |
if (isTRUE(legend) && !is.null(nm)) {
|
| 4063 | 1x |
e$chart$x$opts$legend$data <- append(e$chart$x$opts$legend$data, list(nm)) |
| 4064 |
} |
|
| 4065 | ||
| 4066 | 1x |
e$chart$x$opts$series <- append(e$chart$x$opts$series, list(l_data)) |
| 4067 |
} else {
|
|
| 4068 | ! |
e$chart$x$opts$options[[i]]$series <- append(e$chart$x$opts$options[[i]]$series, list(l_data)) |
| 4069 |
} |
|
| 4070 |
} |
|
| 4071 |
} |
|
| 4072 | ||
| 4073 | 1x |
if (isTRUE(e$chart$x$tl) && !inherits(mod, "error")) {
|
| 4074 | ! |
if (isTRUE(legend) && !is.null(name)) {
|
| 4075 | ! |
e$chart$x$opts$baseOption$legend$data <- append(e$chart$x$opts$baseOption$legend$data, list(name)) |
| 4076 |
} |
|
| 4077 | ||
| 4078 | ! |
e$chart$x$opts$baseOption$series <- append(e$chart$x$opts$baseOption$series, list(l_opts)) |
| 4079 |
} |
|
| 4080 | ||
| 4081 | 1x |
e |
| 4082 |
} |
|
| 4083 | ||
| 4084 |
#' Histogram & Density |
|
| 4085 |
#' |
|
| 4086 |
#' Add a histogram or density plots. |
|
| 4087 |
#' |
|
| 4088 |
#' @inheritParams e_bar |
|
| 4089 |
#' @param bar_width Width of bars. |
|
| 4090 |
#' @param breaks Passed to \code{\link{hist}}.
|
|
| 4091 |
#' @param smooth Whether to use smoothed lines, passed to \code{\link{e_line}}.
|
|
| 4092 |
#' |
|
| 4093 |
#' @examples |
|
| 4094 |
#' mtcars |> |
|
| 4095 |
#' e_charts() |> |
|
| 4096 |
#' e_histogram(mpg, name = "histogram") |> |
|
| 4097 |
#' e_density(mpg, areaStyle = list(opacity = .4), smooth = TRUE, name = "density", y_index = 1) |> |
|
| 4098 |
#' e_tooltip(trigger = "axis") |
|
| 4099 |
#' |
|
| 4100 |
#' # timeline |
|
| 4101 |
#' mtcars |> |
|
| 4102 |
#' group_by(cyl) |> |
|
| 4103 |
#' e_charts(timeline = TRUE) |> |
|
| 4104 |
#' e_histogram(mpg, name = "histogram") |> |
|
| 4105 |
#' e_density(mpg, name = "density", y_index = 1) |
|
| 4106 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-bar}{Additional arguments for histogram},
|
|
| 4107 |
#' \href{https://echarts.apache.org/en/option.html#series-line}{Additional arguments for density}
|
|
| 4108 |
#' |
|
| 4109 |
#' @rdname histogram |
|
| 4110 |
#' @export |
|
| 4111 |
e_histogram <- function( |
|
| 4112 |
e, |
|
| 4113 |
serie, |
|
| 4114 |
breaks = "Sturges", |
|
| 4115 |
name = "histogram", |
|
| 4116 |
legend = TRUE, |
|
| 4117 |
bar_width = "99%", |
|
| 4118 |
x_index = 0, |
|
| 4119 |
y_index = 0, |
|
| 4120 |
... |
|
| 4121 |
) {
|
|
| 4122 | 7x |
UseMethod("e_histogram")
|
| 4123 |
} |
|
| 4124 | ||
| 4125 |
#' @export |
|
| 4126 |
#' @method e_histogram echarts4r |
|
| 4127 |
e_histogram.echarts4r <- function( |
|
| 4128 |
e, |
|
| 4129 |
serie, |
|
| 4130 |
breaks = "Sturges", |
|
| 4131 |
name = "histogram", |
|
| 4132 |
legend = TRUE, |
|
| 4133 |
bar_width = "99%", |
|
| 4134 |
x_index = 0, |
|
| 4135 |
y_index = 0, |
|
| 4136 |
... |
|
| 4137 |
) {
|
|
| 4138 | 7x |
if (missing(e)) {
|
| 4139 | 1x |
stop("must pass e", call. = FALSE)
|
| 4140 |
} |
|
| 4141 | 6x |
if (missing(serie)) {
|
| 4142 | 1x |
stop("must pass serie", call. = FALSE)
|
| 4143 |
} |
|
| 4144 | ||
| 4145 | 5x |
e_histogram_(e, deparse(substitute(serie)), breaks, name, legend, bar_width, x_index, y_index, ...) |
| 4146 |
} |
|
| 4147 | ||
| 4148 |
#' @export |
|
| 4149 |
#' @method e_histogram echarts4rProxy |
|
| 4150 |
e_histogram.echarts4rProxy <- function( |
|
| 4151 |
e, |
|
| 4152 |
serie, |
|
| 4153 |
breaks = "Sturges", |
|
| 4154 |
name = "histogram", |
|
| 4155 |
legend = TRUE, |
|
| 4156 |
bar_width = "99%", |
|
| 4157 |
x_index = 0, |
|
| 4158 |
y_index = 0, |
|
| 4159 |
... |
|
| 4160 |
) {
|
|
| 4161 | 2x |
if (missing(serie)) {
|
| 4162 | 1x |
stop("must pass serie", call. = FALSE)
|
| 4163 |
} |
|
| 4164 | ||
| 4165 | 1x |
e$chart <- e_histogram_(e$chart, deparse(substitute(serie)), breaks, name, legend, bar_width, x_index, y_index, ...) |
| 4166 | 1x |
return(e) |
| 4167 |
} |
|
| 4168 | ||
| 4169 |
#' @rdname histogram |
|
| 4170 |
#' @export |
|
| 4171 |
e_density <- function( |
|
| 4172 |
e, |
|
| 4173 |
serie, |
|
| 4174 |
breaks = "Sturges", |
|
| 4175 |
name = NULL, |
|
| 4176 |
legend = TRUE, |
|
| 4177 |
x_index = 0, |
|
| 4178 |
y_index = 0, |
|
| 4179 |
smooth = TRUE, |
|
| 4180 |
...) {
|
|
| 4181 | 6x |
UseMethod("e_density")
|
| 4182 |
} |
|
| 4183 | ||
| 4184 |
#' @export |
|
| 4185 |
#' @method e_density echarts4r |
|
| 4186 |
e_density.echarts4r <- function( |
|
| 4187 |
e, |
|
| 4188 |
serie, |
|
| 4189 |
breaks = "Sturges", |
|
| 4190 |
name = NULL, |
|
| 4191 |
legend = TRUE, |
|
| 4192 |
x_index = 0, |
|
| 4193 |
y_index = 0, |
|
| 4194 |
smooth = TRUE, |
|
| 4195 |
...) {
|
|
| 4196 | ||
| 4197 | 6x |
if (missing(e)) {
|
| 4198 | 1x |
stop("must pass e", call. = FALSE)
|
| 4199 |
} |
|
| 4200 | ||
| 4201 | 5x |
if (missing(serie)) {
|
| 4202 | 1x |
stop("must pass serie", call. = FALSE)
|
| 4203 |
} |
|
| 4204 | ||
| 4205 | 4x |
e_density_(e, deparse(substitute(serie)), breaks, name, legend, x_index, y_index, ...) |
| 4206 |
} |
|
| 4207 | ||
| 4208 |
#' @export |
|
| 4209 |
#' @method e_density echarts4rProxy |
|
| 4210 |
e_density.echarts4rProxy <- function( |
|
| 4211 |
e, |
|
| 4212 |
serie, |
|
| 4213 |
breaks = "Sturges", |
|
| 4214 |
name = NULL, |
|
| 4215 |
legend = TRUE, |
|
| 4216 |
x_index = 0, |
|
| 4217 |
y_index = 0, |
|
| 4218 |
smooth = TRUE, |
|
| 4219 |
...) {
|
|
| 4220 | ||
| 4221 | 2x |
if (missing(serie)) {
|
| 4222 | 1x |
stop("must pass serie", call. = FALSE)
|
| 4223 |
} |
|
| 4224 | ||
| 4225 | 1x |
e$chart <- e_density_(e$chart, deparse(substitute(serie)), breaks, name, legend, x_index, y_index, ...) |
| 4226 | 1x |
return(e) |
| 4227 |
} |
|
| 4228 | ||
| 4229 |
# TODO I can't get this to work. I need an example. |
|
| 4230 |
# flights |> e_charts() |> e_lines_gl(flights) |
|
| 4231 | ||
| 4232 |
#' Lines WebGL |
|
| 4233 |
#' |
|
| 4234 |
#' Draw WebGL lines. |
|
| 4235 |
#' |
|
| 4236 |
#' @inheritParams e_bar |
|
| 4237 |
#' @param data A list. |
|
| 4238 |
#' @param ... Any other options (this series type is mostly undocumented). |
|
| 4239 |
#' |
|
| 4240 |
#' @export |
|
| 4241 | ! |
e_lines_gl <- function(e, data, coord_system = "geo", ...) UseMethod("e_lines_gl")
|
| 4242 | ||
| 4243 |
#' @export |
|
| 4244 |
#' @method e_lines_gl echarts4r |
|
| 4245 |
e_lines_gl.echarts4r <- function(e, data, coord_system = "geo", ...) {
|
|
| 4246 | ||
| 4247 | ! |
if (missing(e)) {
|
| 4248 | ! |
stop("must pass e", call. = FALSE)
|
| 4249 |
} |
|
| 4250 | ! |
if (missing(data)) {
|
| 4251 | ! |
stop("missing data", call. = FALSE)
|
| 4252 |
} |
|
| 4253 | ||
| 4254 | ! |
serie <- list( |
| 4255 | ! |
type = "linesGL", |
| 4256 | ! |
coordinateSystem = coord_system, |
| 4257 | ! |
data = data, |
| 4258 |
... |
|
| 4259 |
) |
|
| 4260 | ||
| 4261 | ! |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 4262 | ! |
e |
| 4263 |
} |
|
| 4264 | ||
| 4265 |
#' @export |
|
| 4266 |
#' @method e_lines_gl echarts4rProxy |
|
| 4267 |
e_lines_gl.echarts4rProxy <- function(e, data, coord_system = "geo", ...) {
|
|
| 4268 | ! |
if (missing(data)) {
|
| 4269 | ! |
stop("missing data", call. = FALSE)
|
| 4270 |
} |
|
| 4271 | ||
| 4272 | ! |
serie <- list( |
| 4273 | ! |
type = "linesGL", |
| 4274 | ! |
coordinateSystem = coord_system, |
| 4275 | ! |
data = data, |
| 4276 |
... |
|
| 4277 |
) |
|
| 4278 | ||
| 4279 | ! |
e$chart$x$opts$series <- append(e$chart$x$opts$series, list(serie)) |
| 4280 | ! |
e |
| 4281 |
} |
|
| 4282 | ||
| 4283 |
#' Confidence bands |
|
| 4284 |
#' |
|
| 4285 |
#' Add confidence bands |
|
| 4286 |
#' |
|
| 4287 |
#' @inheritParams e_bar |
|
| 4288 |
#' @param min,max series. |
|
| 4289 |
#' @param stack Name of stack. |
|
| 4290 |
#' @param symbol Whether to show symbols on lower and upper band lines. |
|
| 4291 |
#' @param areaStyle The style of lower and upper bands, i.e.: color. |
|
| 4292 |
#' @param legend Whether to show \code{min} and \code{max} in legend.
|
|
| 4293 |
#' @param ... All options must be of vectors or lists of length 2 where the first argument is |
|
| 4294 |
#' for the lower bound and the second for the upper bound, see examples. |
|
| 4295 |
#' |
|
| 4296 |
#' @examples |
|
| 4297 |
#' df <- data.frame( |
|
| 4298 |
#' x = 1:10, |
|
| 4299 |
#' y = runif(10, 5, 10) |
|
| 4300 |
#' ) |> |
|
| 4301 |
#' dplyr::mutate( |
|
| 4302 |
#' lwr = y - runif(10, 1, 3), |
|
| 4303 |
#' upr = y + runif(10, 2, 4) |
|
| 4304 |
#' ) |
|
| 4305 |
#' |
|
| 4306 |
#' df |> |
|
| 4307 |
#' e_charts(x) |> |
|
| 4308 |
#' e_line(y) |> |
|
| 4309 |
#' e_band(lwr, upr) |
|
| 4310 |
#' @name band |
|
| 4311 |
#' @export |
|
| 4312 |
e_band <- function( |
|
| 4313 |
e, |
|
| 4314 |
min, |
|
| 4315 |
max, |
|
| 4316 |
stack = "confidence-band", |
|
| 4317 |
symbol = c("none", "none"),
|
|
| 4318 |
areaStyle = list(list(color = "rgba(0,0,0,0)"), list()), |
|
| 4319 |
legend = list(FALSE, FALSE), |
|
| 4320 |
...) {
|
|
| 4321 | ||
| 4322 | 5x |
UseMethod("e_band")
|
| 4323 |
} |
|
| 4324 | ||
| 4325 |
#' @export |
|
| 4326 |
#' @method e_band echarts4r |
|
| 4327 |
e_band.echarts4r <- function( |
|
| 4328 |
e, |
|
| 4329 |
min, |
|
| 4330 |
max, |
|
| 4331 |
stack = "confidence-band", |
|
| 4332 |
symbol = c("none", "none"),
|
|
| 4333 |
areaStyle = list(list(color = "rgba(0,0,0,0)"), list()), |
|
| 4334 |
legend = list(FALSE, FALSE), |
|
| 4335 |
...) {
|
|
| 4336 | ||
| 4337 | 5x |
if (missing(e)) {
|
| 4338 | 1x |
stop("must pass e", call. = FALSE)
|
| 4339 |
} |
|
| 4340 | ||
| 4341 | 4x |
if (missing(min) || missing(max)) {
|
| 4342 | 1x |
stop("must pass min and max", call. = FALSE)
|
| 4343 |
} |
|
| 4344 | ||
| 4345 | 3x |
e_band_( |
| 4346 | 3x |
e, |
| 4347 | 3x |
deparse(substitute(min)), |
| 4348 | 3x |
deparse(substitute(max)), |
| 4349 | 3x |
stack = "confidence-band", |
| 4350 | 3x |
symbol = symbol, |
| 4351 | 3x |
areaStyle = areaStyle, |
| 4352 | 3x |
legend = legend, |
| 4353 |
... |
|
| 4354 |
) |
|
| 4355 |
} |
|
| 4356 | ||
| 4357 |
#' @export |
|
| 4358 |
#' @method e_band echarts4rProxy |
|
| 4359 |
e_band.echarts4rProxy <- function( |
|
| 4360 |
e, |
|
| 4361 |
min, |
|
| 4362 |
max, |
|
| 4363 |
stack = "confidence-band", |
|
| 4364 |
symbol = c("none", "none"),
|
|
| 4365 |
areaStyle = list(list(color = "rgba(0,0,0,0)"), list()), |
|
| 4366 |
legend = list(FALSE, FALSE), |
|
| 4367 |
...) {
|
|
| 4368 | 2x |
if (missing(min) || missing(max)) {
|
| 4369 | 1x |
stop("must pass min and max", call. = FALSE)
|
| 4370 |
} |
|
| 4371 | ||
| 4372 | 1x |
e$chart <- e_band_( |
| 4373 | 1x |
e$chart, |
| 4374 | 1x |
deparse(substitute(min)), |
| 4375 | 1x |
deparse(substitute(max)), |
| 4376 | 1x |
stack = "confidence-band", |
| 4377 | 1x |
symbol = symbol, |
| 4378 | 1x |
areaStyle = areaStyle, |
| 4379 | 1x |
legend = legend, |
| 4380 |
... |
|
| 4381 |
) |
|
| 4382 | 1x |
return(e) |
| 4383 |
} |
|
| 4384 | ||
| 4385 |
#' Correlation |
|
| 4386 |
#' |
|
| 4387 |
#' @inheritParams e_bar |
|
| 4388 |
#' @param visual_map Whether to add the visual map. |
|
| 4389 |
#' @param order Ordering method, passed to \link[corrplot]{corrMatOrder}.
|
|
| 4390 |
#' |
|
| 4391 |
#' @param ... Any argument to pass to \code{\link{e_heatmap}} and \code{\link{e_visual_map}}.
|
|
| 4392 |
#' |
|
| 4393 |
#' @examples |
|
| 4394 |
#' cor(mtcars) |> |
|
| 4395 |
#' e_charts() |> |
|
| 4396 |
#' e_correlations( |
|
| 4397 |
#' order = "hclust", |
|
| 4398 |
#' visual_map = FALSE |
|
| 4399 |
#' ) |> |
|
| 4400 |
#' e_visual_map( |
|
| 4401 |
#' min = -1, |
|
| 4402 |
#' max = 1 |
|
| 4403 |
#' ) |
|
| 4404 |
#' @export |
|
| 4405 | 3x |
e_correlations <- function(e, order = NULL, visual_map = TRUE, ...) UseMethod("e_correlations")
|
| 4406 | ||
| 4407 |
#' @export |
|
| 4408 |
#' @method e_correlations echarts4r |
|
| 4409 |
e_correlations.echarts4r <- function(e, order = NULL, visual_map = TRUE, ...) {
|
|
| 4410 | 3x |
if (missing(e)) {
|
| 4411 | 1x |
stop("must pass e", call. = FALSE)
|
| 4412 |
} |
|
| 4413 | ||
| 4414 | 2x |
mat <- e$x$data[[1]] |
| 4415 | ||
| 4416 | 2x |
if (!is.null(order)) {
|
| 4417 | 2x |
order <- corrplot::corrMatOrder(mat, order = order) |
| 4418 | 2x |
mat <- mat[order, order] |
| 4419 |
} |
|
| 4420 | ||
| 4421 | 2x |
row.names(mat) <- colnames(mat) |
| 4422 | 2x |
mat <- as.data.frame(as.table(mat)) |
| 4423 | 2x |
names(mat) <- c("x", "y", "correlation")
|
| 4424 | ||
| 4425 | 2x |
e <- e |> |
| 4426 | 2x |
e_data(mat, x) |> |
| 4427 | 2x |
e_heatmap_("y", "correlation", ...)
|
| 4428 | ||
| 4429 | 2x |
if (isTRUE(visual_map)) {
|
| 4430 | 1x |
e <- e |> e_visual_map_(min = -1, max = 1, ...) |
| 4431 |
} |
|
| 4432 | ||
| 4433 | 2x |
return(e) |
| 4434 |
} |
|
| 4435 | ||
| 4436 |
#' @export |
|
| 4437 |
#' @method e_correlations echarts4rProxy |
|
| 4438 |
e_correlations.echarts4rProxy <- function(e, order = NULL, visual_map = TRUE, ...) {
|
|
| 4439 | ||
| 4440 | 1x |
mat <- e$chart$x$data[[1]] |
| 4441 | ||
| 4442 | 1x |
if (!is.null(order)) {
|
| 4443 | 1x |
order <- corrplot::corrMatOrder(mat, order = order) |
| 4444 | 1x |
mat <- mat[order, order] |
| 4445 |
} |
|
| 4446 | ||
| 4447 | 1x |
row.names(mat) <- colnames(mat) |
| 4448 | 1x |
mat <- as.data.frame(as.table(mat)) |
| 4449 | 1x |
names(mat) <- c("x", "y", "correlation")
|
| 4450 | ||
| 4451 | 1x |
e$chart <- e$chart |> |
| 4452 | 1x |
e_data(mat, x) |> |
| 4453 | 1x |
e_heatmap_("y", "correlation", ...)
|
| 4454 | ||
| 4455 | 1x |
if (isTRUE(visual_map)) {
|
| 4456 | 1x |
e$chart <- e$chart |> e_visual_map_(min = -1, max = 1, ...) |
| 4457 |
} |
|
| 4458 | ||
| 4459 | 1x |
return(e) |
| 4460 |
} |
|
| 4461 | ||
| 4462 |
#' Error bar |
|
| 4463 |
#' |
|
| 4464 |
#' Add error bars. |
|
| 4465 |
#' |
|
| 4466 |
#' @inheritParams e_bar |
|
| 4467 |
#' @param lower,upper Lower and upper error bands. |
|
| 4468 |
#' @param renderer mame of render function from renderers.js |
|
| 4469 |
#' @param itemStyle mostly used for borderWidth, default 1.5 |
|
| 4470 |
#' |
|
| 4471 |
#' @examples |
|
| 4472 |
#' df <- data.frame( |
|
| 4473 |
#' x = factor(c(1, 2)), |
|
| 4474 |
#' y = c(1, 5), |
|
| 4475 |
#' upper = c(1.1, 5.3), |
|
| 4476 |
#' lower = c(0.8, 4.6) |
|
| 4477 |
#' ) |
|
| 4478 |
#' |
|
| 4479 |
#' df |> |
|
| 4480 |
#' e_charts(x) |> |
|
| 4481 |
#' e_bar(y) |> |
|
| 4482 |
#' e_error_bar(lower, upper) |
|
| 4483 |
#' |
|
| 4484 |
#' # timeline |
|
| 4485 |
#' df <- data.frame( |
|
| 4486 |
#' x = factor(c(1, 1, 2, 2)), |
|
| 4487 |
#' y = c(1, 5, 3, 4), |
|
| 4488 |
#' step = factor(c(1, 2, 1, 2)), |
|
| 4489 |
#' upper = c(1.1, 5.3, 3.3, 4.2), |
|
| 4490 |
#' lower = c(0.8, 4.6, 2.4, 3.6) |
|
| 4491 |
#' ) |
|
| 4492 |
#' |
|
| 4493 |
#' df |> |
|
| 4494 |
#' group_by(step) |> |
|
| 4495 |
#' e_charts(x, timeline = TRUE) |> |
|
| 4496 |
#' e_bar(y) |> |
|
| 4497 |
#' e_error_bar(lower, upper) |
|
| 4498 |
#' @rdname errorbar |
|
| 4499 |
#' @export |
|
| 4500 |
e_error_bar <- function( |
|
| 4501 |
e, |
|
| 4502 |
lower, |
|
| 4503 |
upper, |
|
| 4504 |
name = NULL, |
|
| 4505 |
legend = FALSE, |
|
| 4506 |
y_index = 0, |
|
| 4507 |
x_index = 0, |
|
| 4508 |
coord_system = "cartesian2d", |
|
| 4509 |
...) {
|
|
| 4510 | 6x |
UseMethod("e_error_bar")
|
| 4511 |
} |
|
| 4512 | ||
| 4513 |
#' @export |
|
| 4514 |
#' @method e_error_bar echarts4r |
|
| 4515 |
e_error_bar.echarts4r <- function( |
|
| 4516 |
e, |
|
| 4517 |
lower, |
|
| 4518 |
upper, |
|
| 4519 |
name = NULL, |
|
| 4520 |
legend = FALSE, |
|
| 4521 |
y_index = 0, |
|
| 4522 |
x_index = 0, |
|
| 4523 |
coord_system = "cartesian2d", |
|
| 4524 |
...) {
|
|
| 4525 | ||
| 4526 | 6x |
if (missing(e)) {
|
| 4527 | 1x |
stop("must pass e", call. = FALSE)
|
| 4528 |
} |
|
| 4529 | ||
| 4530 | 5x |
if (missing(lower) || missing(upper)) {
|
| 4531 | 1x |
stop("must pass lower and upper", call. = FALSE)
|
| 4532 |
} |
|
| 4533 | ||
| 4534 | 4x |
e_error_bar_( |
| 4535 | 4x |
e, |
| 4536 | 4x |
deparse(substitute(lower)), |
| 4537 | 4x |
deparse(substitute(upper)), |
| 4538 | 4x |
name = name, |
| 4539 | 4x |
legend = legend, |
| 4540 | 4x |
y_index = y_index, |
| 4541 | 4x |
x_index = x_index, |
| 4542 | 4x |
coord_system = coord_system, |
| 4543 |
... |
|
| 4544 |
) |
|
| 4545 |
} |
|
| 4546 | ||
| 4547 |
#' @export |
|
| 4548 |
#' @method e_error_bar echarts4rProxy |
|
| 4549 |
e_error_bar.echarts4rProxy <- function( |
|
| 4550 |
e, |
|
| 4551 |
lower, |
|
| 4552 |
upper, |
|
| 4553 |
name = NULL, |
|
| 4554 |
legend = FALSE, |
|
| 4555 |
y_index = 0, |
|
| 4556 |
x_index = 0, |
|
| 4557 |
coord_system = "cartesian2d", |
|
| 4558 |
...) {
|
|
| 4559 | 2x |
if (missing(lower) || missing(upper)) {
|
| 4560 | 1x |
stop("must pass lower, or upper", call. = FALSE)
|
| 4561 |
} |
|
| 4562 | ||
| 4563 | 1x |
e$chart <- e_error_bar_( |
| 4564 | 1x |
e$chart, |
| 4565 | 1x |
deparse(substitute(lower)), |
| 4566 | 1x |
deparse(substitute(upper)), |
| 4567 | 1x |
name = name, |
| 4568 | 1x |
legend = legend, |
| 4569 | 1x |
y_index = y_index, |
| 4570 | 1x |
x_index = x_index, |
| 4571 | 1x |
coord_system = coord_system, |
| 4572 |
... |
|
| 4573 |
) |
|
| 4574 | 1x |
return(e) |
| 4575 |
} |
|
| 4576 | ||
| 4577 | ||
| 4578 |
#' Area bands |
|
| 4579 |
#' |
|
| 4580 |
#' Add area bands |
|
| 4581 |
#' |
|
| 4582 |
#' @inheritParams e_bar |
|
| 4583 |
#' @param lower,upper series of lower and upper borders of the band |
|
| 4584 |
#' @param itemStyle mostly used for borderWidth, default 0.5 |
|
| 4585 |
#' @param ... additional options |
|
| 4586 |
#' |
|
| 4587 |
#' @examples |
|
| 4588 |
#' data(EuStockMarkets) |
|
| 4589 |
#' as.data.frame(EuStockMarkets) |> |
|
| 4590 |
#' dplyr::slice_head(n = 200) |> |
|
| 4591 |
#' dplyr::mutate(day = 1:dplyr::n()) |> |
|
| 4592 |
#' e_charts(day) |> |
|
| 4593 |
#' e_line(CAC, symbol = "none") |> |
|
| 4594 |
#' e_band2(DAX, FTSE, color = "lemonchiffon") |> |
|
| 4595 |
#' e_band2(DAX, SMI, color = "lightblue", itemStyle = list(borderWidth = 0)) |> |
|
| 4596 |
#' e_y_axis(scale = TRUE) |> |
|
| 4597 |
#' e_datazoom(start = 50) |
|
| 4598 |
#' @name band2 |
|
| 4599 |
#' @export |
|
| 4600 |
e_band2 <- function(e, lower, upper, name = NULL, |
|
| 4601 |
legend = TRUE, |
|
| 4602 |
y_index = 0, |
|
| 4603 |
x_index = 0, |
|
| 4604 |
coord_system = "cartesian2d", |
|
| 4605 |
itemStyle = list(borderWidth = 0.5), |
|
| 4606 |
...) {
|
|
| 4607 | 6x |
UseMethod("e_band2")
|
| 4608 |
} |
|
| 4609 | ||
| 4610 |
#' @export |
|
| 4611 |
#' @method e_band2 echarts4r |
|
| 4612 |
e_band2.echarts4r <- function(e, lower, upper, name = NULL, |
|
| 4613 |
legend = TRUE, |
|
| 4614 |
y_index = 0, |
|
| 4615 |
x_index = 0, |
|
| 4616 |
coord_system = "cartesian2d", |
|
| 4617 |
itemStyle = list(borderWidth = 0.5), ...) {
|
|
| 4618 | ||
| 4619 | 7x |
if (missing(e)) {
|
| 4620 | 1x |
stop("must pass e", call. = FALSE)
|
| 4621 |
} |
|
| 4622 | ||
| 4623 | 6x |
if (missing(lower) || missing(upper)) {
|
| 4624 | 1x |
stop("must pass lower and upper", call. = FALSE)
|
| 4625 |
} |
|
| 4626 | 5x |
if (coord_system != "cartesian2d") {
|
| 4627 | 1x |
stop("only cartesian2d supported", call. = FALSE)
|
| 4628 |
} |
|
| 4629 | ||
| 4630 | 4x |
e_band2_( |
| 4631 | 4x |
e, |
| 4632 | 4x |
deparse(substitute(lower)), |
| 4633 | 4x |
deparse(substitute(upper)), |
| 4634 | 4x |
name, |
| 4635 | 4x |
legend, |
| 4636 | 4x |
y_index, |
| 4637 | 4x |
x_index, |
| 4638 | 4x |
coord_system, |
| 4639 | 4x |
itemStyle, |
| 4640 |
... |
|
| 4641 |
) |
|
| 4642 |
} |
|
| 4643 | ||
| 4644 |
#' @export |
|
| 4645 |
#' @method e_band2 echarts4rProxy |
|
| 4646 |
e_band2.echarts4rProxy <- function(e, lower, upper, name = NULL, |
|
| 4647 |
legend = TRUE, |
|
| 4648 |
y_index = 0, |
|
| 4649 |
x_index = 0, |
|
| 4650 |
coord_system = "cartesian2d", |
|
| 4651 |
itemStyle = list(borderWidth = 0.5), ...) {
|
|
| 4652 | 2x |
if (missing(lower) || missing(upper)) {
|
| 4653 | 1x |
stop("must pass lower and upper", call. = FALSE)
|
| 4654 |
} |
|
| 4655 | ||
| 4656 | 1x |
e$chart <- e_band2_( |
| 4657 | 1x |
e$chart, |
| 4658 | 1x |
deparse(substitute(lower)), |
| 4659 | 1x |
deparse(substitute(upper)), |
| 4660 | 1x |
name, |
| 4661 | 1x |
legend, |
| 4662 | 1x |
y_index, |
| 4663 | 1x |
x_index, |
| 4664 | 1x |
coord_system, |
| 4665 | 1x |
itemStyle, |
| 4666 |
... |
|
| 4667 |
) |
|
| 4668 | 1x |
return(e) |
| 4669 |
} |
|
| 4670 | ||
| 4671 |
#' Chord |
|
| 4672 |
#' |
|
| 4673 |
#' Draw a Chord chart. |
|
| 4674 |
#' |
|
| 4675 |
#' @inheritParams e_bar |
|
| 4676 |
#' @param source,target Source and target columns. |
|
| 4677 |
#' @param value Value shared between \code{source} and \code{target}.
|
|
| 4678 |
#' @param rm_x,rm_y Whether to remove the x and y axis, defaults to \code{TRUE}.
|
|
| 4679 |
#' |
|
| 4680 |
#' @examples |
|
| 4681 |
#' chord_data <- data.frame( |
|
| 4682 |
#' source = c("a", "b", "c", "d", "c"),
|
|
| 4683 |
#' target = c("b", "c", "d", "e", "e"),
|
|
| 4684 |
#' value = ceiling(rnorm(5, 10, 1)), |
|
| 4685 |
#' stringsAsFactors = FALSE |
|
| 4686 |
#' ) |
|
| 4687 |
#' |
|
| 4688 |
#' chord_data |> |
|
| 4689 |
#' e_charts() |> |
|
| 4690 |
#' e_chord(source, target, value) |
|
| 4691 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-chord}{Additional arguments}
|
|
| 4692 |
#' |
|
| 4693 |
#' @rdname e_chord |
|
| 4694 |
#' @export |
|
| 4695 | 5x |
e_chord <- function(e, source, target, value, rm_x = TRUE, rm_y = TRUE, ...) UseMethod("e_chord")
|
| 4696 | ||
| 4697 |
#' @export |
|
| 4698 |
#' @method e_chord echarts4r |
|
| 4699 |
e_chord.echarts4r <- function(e, source, target, value, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 4700 | 5x |
if (missing(e)) {
|
| 4701 | 1x |
stop("must pass e", call. = FALSE)
|
| 4702 |
} |
|
| 4703 | ||
| 4704 | 4x |
if (missing(source) || missing(target) || missing(value)) {
|
| 4705 | 1x |
stop("missing source, target or values", call. = FALSE)
|
| 4706 |
} |
|
| 4707 | ||
| 4708 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 4709 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 4710 | ||
| 4711 | 3x |
e_chord_( |
| 4712 | 3x |
e, |
| 4713 | 3x |
deparse(substitute(source)), |
| 4714 | 3x |
deparse(substitute(target)), |
| 4715 | 3x |
deparse(substitute(value)), |
| 4716 | 3x |
rm_x, |
| 4717 | 3x |
rm_y, |
| 4718 |
... |
|
| 4719 |
) |
|
| 4720 |
} |
|
| 4721 | ||
| 4722 |
#' @export |
|
| 4723 |
#' @method e_chord echarts4rProxy |
|
| 4724 |
e_chord.echarts4rProxy <- function(e, source, target, value, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 4725 | 2x |
if (missing(source) || missing(target) || missing(value)) {
|
| 4726 | 1x |
stop("missing source, target or values", call. = FALSE)
|
| 4727 |
} |
|
| 4728 | ||
| 4729 | 1x |
e$chart <- e_chord_( |
| 4730 | 1x |
e$chart, |
| 4731 | 1x |
deparse(substitute(source)), |
| 4732 | 1x |
deparse(substitute(target)), |
| 4733 | 1x |
deparse(substitute(value)), |
| 4734 | 1x |
rm_x, |
| 4735 | 1x |
rm_y, |
| 4736 |
... |
|
| 4737 |
) |
|
| 4738 | ||
| 4739 | 1x |
return(e) |
| 4740 |
} |
| 1 |
#' Visual Map |
|
| 2 |
#' |
|
| 3 |
#' @inheritParams e_bar |
|
| 4 |
#' @param serie Column name of serie to scale against. |
|
| 5 |
#' @param calculable Whether show handles, which can be dragged to adjust "selected range". |
|
| 6 |
#' @param type One of \code{continuous} or \code{piecewise}.
|
|
| 7 |
#' @param scale A function that takes a vector of \code{numeric} and returns a vector of \code{numeric}
|
|
| 8 |
#' of the same length. |
|
| 9 |
#' |
|
| 10 |
#' @section Scaling function: defaults to \code{e_scale} which is a basic function that rescales \code{size}
|
|
| 11 |
#' between 1 and 20 for that makes for decent sized points on the chart. |
|
| 12 |
#' |
|
| 13 |
#' @examples |
|
| 14 |
#' # scaled data |
|
| 15 |
#' mtcars |> |
|
| 16 |
#' e_charts(mpg) |> |
|
| 17 |
#' e_scatter(wt, qsec, scale = e_scale) |> |
|
| 18 |
#' e_visual_map(qsec, scale = e_scale) |
|
| 19 |
#' |
|
| 20 |
#' # dimension |
|
| 21 |
#' # color according to y axis |
|
| 22 |
#' mtcars |> |
|
| 23 |
#' e_charts(mpg) |> |
|
| 24 |
#' e_scatter(wt) |> |
|
| 25 |
#' e_visual_map(wt, dimension = 1) |
|
| 26 |
#' |
|
| 27 |
#' # color according to x axis |
|
| 28 |
#' mtcars |> |
|
| 29 |
#' e_charts(mpg) |> |
|
| 30 |
#' e_scatter(wt) |> |
|
| 31 |
#' e_visual_map(mpg, dimension = 0) |
|
| 32 |
#' |
|
| 33 |
#' v <- LETTERS[1:10] |
|
| 34 |
#' matrix <- data.frame( |
|
| 35 |
#' x = sample(v, 300, replace = TRUE), |
|
| 36 |
#' y = sample(v, 300, replace = TRUE), |
|
| 37 |
#' z = rnorm(300, 10, 1), |
|
| 38 |
#' color = rnorm(300, 10, 1), |
|
| 39 |
#' size = rnorm(300, 10, 1), |
|
| 40 |
#' stringsAsFactors = FALSE |
|
| 41 |
#' ) |> |
|
| 42 |
#' dplyr::group_by(x, y) |> |
|
| 43 |
#' dplyr::summarise( |
|
| 44 |
#' z = sum(z), |
|
| 45 |
#' color = sum(color), |
|
| 46 |
#' size = sum(size) |
|
| 47 |
#' ) |> |
|
| 48 |
#' dplyr::ungroup() |
|
| 49 |
#' |
|
| 50 |
#' matrix |> |
|
| 51 |
#' e_charts(x) |> |
|
| 52 |
#' e_scatter_3d(y, z, color, size) |> |
|
| 53 |
#' e_visual_map( |
|
| 54 |
#' z, |
|
| 55 |
#' # scale to z |
|
| 56 |
#' inRange = list(symbolSize = c(1, 30)), |
|
| 57 |
#' # scale size |
|
| 58 |
#' dimension = 3 # third dimension 0 = x, y = 1, z = 2, size = 3 |
|
| 59 |
#' ) |> |
|
| 60 |
#' e_visual_map( |
|
| 61 |
#' z, |
|
| 62 |
#' # scale to z |
|
| 63 |
#' inRange = list(color = c("#bf444c", "#d88273", "#f6efa6")),
|
|
| 64 |
#' # scale colors |
|
| 65 |
#' dimension = 4, |
|
| 66 |
#' # third dimension 0 = x, y = 1, z = 2, size = 3, color = 4 |
|
| 67 |
#' bottom = 300 # padding to avoid visual maps overlap |
|
| 68 |
#' ) |
|
| 69 |
#' @seealso \href{https://echarts.apache.org/en/option.html#visualMap}{Additional arguments}
|
|
| 70 |
#' |
|
| 71 |
#' @rdname e_visual_map |
|
| 72 |
#' @export |
|
| 73 |
e_visual_map <- function(e, serie, calculable = TRUE, type = c("continuous", "piecewise"), scale = NULL, ...) {
|
|
| 74 | 24x |
if (missing(e)) {
|
| 75 | 1x |
stop("must pass e", call. = FALSE)
|
| 76 |
} |
|
| 77 | ||
| 78 | 23x |
if (!missing(serie)) {
|
| 79 | 14x |
serie <- deparse(substitute(serie)) |
| 80 |
} else {
|
|
| 81 | 9x |
serie <- NULL |
| 82 |
} |
|
| 83 | ||
| 84 | 23x |
e_visual_map_(e, serie, calculable, type, scale, ...) |
| 85 |
} |
|
| 86 | ||
| 87 |
#' @rdname e_visual_map |
|
| 88 |
#' @export |
|
| 89 |
e_visual_map_ <- function(e, serie = NULL, calculable = TRUE, type = c("continuous", "piecewise"), scale = NULL, ...) {
|
|
| 90 | 26x |
if (missing(e)) {
|
| 91 | 1x |
stop("must pass e", call. = FALSE)
|
| 92 |
} |
|
| 93 | ||
| 94 | 25x |
if (!length(e$x$opts$visualMap)) {
|
| 95 | 25x |
e$x$opts$visualMap <- list() |
| 96 |
} |
|
| 97 | ||
| 98 | 25x |
vm <- list(...) |
| 99 | 25x |
vm$calculable <- calculable |
| 100 | 25x |
vm$type <- type[1] |
| 101 | ||
| 102 | 25x |
if (!is.null(serie)) {
|
| 103 | 14x |
dat <- .get_data(e, serie) |
| 104 | 14x |
if (!is.null(scale)) {
|
| 105 | 3x |
dat <- scale(dat) |
| 106 |
} |
|
| 107 | 14x |
rng <- range(dat, na.rm = TRUE) |> .get_validate_range() |
| 108 | 14x |
vm$min <- rng$min |
| 109 | 14x |
vm$max <- rng$max |
| 110 |
} |
|
| 111 | ||
| 112 | 25x |
if (!e$x$tl) {
|
| 113 | 24x |
e$x$opts$visualMap <- append(e$x$opts$visualMap, list(vm)) |
| 114 |
} else {
|
|
| 115 | 1x |
e$x$opts$baseOption$visualMap <- append(e$x$opts$baseOption$visualMap, list(vm)) |
| 116 |
} |
|
| 117 | ||
| 118 | 25x |
e |
| 119 |
} |
|
| 120 | ||
| 121 |
#' Tooltip |
|
| 122 |
#' |
|
| 123 |
#' Customise tooltip |
|
| 124 |
#' |
|
| 125 |
#' @inheritParams e_bar |
|
| 126 |
#' @param trigger What triggers the tooltip, one of \code{item} or \code{axis}.
|
|
| 127 |
#' @param formatter Item and Pointer formatter as returned |
|
| 128 |
#' by \code{\link{e_tooltip_item_formatter}}, \code{\link{e_tooltip_pointer_formatter}},
|
|
| 129 |
#' \code{\link{e_tooltip_pie_formatter}}.
|
|
| 130 |
#' @param style Formatter style, one of \code{decimal}, \code{percent}, or \code{currency}.
|
|
| 131 |
#' @param currency Currency to to display. |
|
| 132 |
#' @param digits Number of decimals. |
|
| 133 |
#' @param locale Locale, if \code{NULL} then it is inferred from \code{Sys.getlocale}.
|
|
| 134 |
#' |
|
| 135 |
#' @section Formatters: |
|
| 136 |
#' \itemize{
|
|
| 137 |
#' \item{\code{\link{e_tooltip_pie_formatter}}: special helper for \code{\link{e_pie}}.}
|
|
| 138 |
#' \item{\code{\link{e_tooltip_item_formatter}}: general helper, this is passed to the \href{https://echarts.apache.org/en/option.html#tooltip.formatter}{tooltip \code{formatter}}.}
|
|
| 139 |
#' \item{\code{\link{e_tooltip_pointer_formatter}}: helper for pointer, this is passed to the
|
|
| 140 |
#' \href{https://echarts.apache.org/en/option.html#tooltip.axisPointer.label}{\code{label} parameter under \code{axisPointer}}.}
|
|
| 141 |
#' } |
|
| 142 |
#' |
|
| 143 |
#' @examples |
|
| 144 |
#' # basic |
|
| 145 |
#' USArrests |> |
|
| 146 |
#' e_charts(Assault) |> |
|
| 147 |
#' e_scatter(Murder) |> |
|
| 148 |
#' e_tooltip() |
|
| 149 |
#' |
|
| 150 |
#' # formatter |
|
| 151 |
#' cars |> |
|
| 152 |
#' dplyr::mutate( |
|
| 153 |
#' dist = dist / 120 |
|
| 154 |
#' ) |> |
|
| 155 |
#' e_charts(speed) |> |
|
| 156 |
#' e_scatter(dist, symbol_size = 5) |> |
|
| 157 |
#' e_tooltip( |
|
| 158 |
#' formatter = e_tooltip_item_formatter("percent")
|
|
| 159 |
#' ) |
|
| 160 |
#' |
|
| 161 |
#' # axis pointer |
|
| 162 |
#' cars |> |
|
| 163 |
#' e_charts(speed) |> |
|
| 164 |
#' e_scatter(dist, symbol_size = 5) |> |
|
| 165 |
#' e_tooltip( |
|
| 166 |
#' formatter = e_tooltip_pointer_formatter("currency"),
|
|
| 167 |
#' axisPointer = list( |
|
| 168 |
#' type = "cross" |
|
| 169 |
#' ) |
|
| 170 |
#' ) |
|
| 171 |
#' @seealso \href{https://echarts.apache.org/en/option.html#tooltip}{Additional arguments}
|
|
| 172 |
#' |
|
| 173 |
#' @rdname e-tooltip |
|
| 174 |
#' @export |
|
| 175 |
e_tooltip <- function(e, trigger = c("item", "axis"), formatter = NULL, ...) {
|
|
| 176 | 16x |
if (missing(e)) {
|
| 177 | 2x |
stop("must pass e", call. = FALSE)
|
| 178 |
} |
|
| 179 | ||
| 180 | 14x |
tooltip <- list(trigger = trigger[1], ...) |
| 181 | ||
| 182 | 14x |
if (!is.null(formatter)) {
|
| 183 | 1x |
if (inherits(formatter, "item_formatter") || inherits(formatter, "pie_formatter") || inherits(formatter, "JS_EVAL")) {
|
| 184 | 1x |
tooltip$formatter <- formatter |
| 185 |
} |
|
| 186 | ||
| 187 | 1x |
if (inherits(formatter, "pointer_formatter")) {
|
| 188 | ! |
tooltip$axisPointer$label <- formatter |
| 189 |
} |
|
| 190 |
} |
|
| 191 | ||
| 192 | 14x |
if (!e$x$tl) {
|
| 193 | 13x |
e$x$opts$tooltip <- tooltip |
| 194 |
} else {
|
|
| 195 | 1x |
e$x$opts$baseOption$tooltip <- tooltip |
| 196 |
} |
|
| 197 | ||
| 198 | 14x |
e |
| 199 |
} |
|
| 200 | ||
| 201 |
#' @rdname e-tooltip |
|
| 202 |
#' @export |
|
| 203 |
e_tooltip_item_formatter <- function( |
|
| 204 |
style = c("decimal", "percent", "currency"),
|
|
| 205 |
digits = 0, |
|
| 206 |
locale = NULL, |
|
| 207 |
currency = "USD" |
|
| 208 |
) {
|
|
| 209 | 2x |
if (is.null(locale)) {
|
| 210 | 2x |
locale <- .get_locale() |
| 211 |
} |
|
| 212 | ||
| 213 | 2x |
style <- match.arg(style) |
| 214 | 2x |
opts <- list( |
| 215 | 2x |
style = style, |
| 216 | 2x |
minimumFractionDigits = digits, |
| 217 | 2x |
maximumFractionDigits = digits, |
| 218 | 2x |
currency = currency |
| 219 |
) |
|
| 220 | ||
| 221 | 2x |
tip <- htmlwidgets::JS(sprintf("function(params, ticket, callback) {
|
| 222 | 2x |
var fmt = new Intl.NumberFormat('%s', %s);
|
| 223 | 2x |
var idx = 0; |
| 224 | 2x |
if (params.name == params.value[0]) {
|
| 225 | 2x |
idx = 1; |
| 226 |
} |
|
| 227 | 2x |
return params.marker + ' ' + |
| 228 | 2x |
params.seriesName + ': ' + fmt.format(parseFloat(params.value[idx])); |
| 229 | 2x |
}", locale, jsonlite::toJSON(opts, auto_unbox = TRUE))) |
| 230 | ||
| 231 | 2x |
tip <- structure(tip, class = c("JS_EVAL", "item_formatter"))
|
| 232 | 2x |
return(tip) |
| 233 |
} |
|
| 234 | ||
| 235 |
#' @rdname e-tooltip |
|
| 236 |
#' @export |
|
| 237 |
e_tooltip_choro_formatter <- function( |
|
| 238 |
style = c("decimal", "percent", "currency"),
|
|
| 239 |
digits = 0, |
|
| 240 |
locale = NULL, |
|
| 241 |
currency = "USD" |
|
| 242 |
) {
|
|
| 243 | 1x |
if (is.null(locale)) {
|
| 244 | 1x |
locale <- .get_locale() |
| 245 |
} |
|
| 246 | ||
| 247 | 1x |
style <- match.arg(style) |
| 248 | 1x |
opts <- list( |
| 249 | 1x |
style = style, |
| 250 | 1x |
minimumFractionDigits = digits, |
| 251 | 1x |
maximumFractionDigits = digits, |
| 252 | 1x |
currency = currency |
| 253 |
) |
|
| 254 | ||
| 255 | 1x |
tip <- htmlwidgets::JS(sprintf("function(params, ticket, callback) {
|
| 256 | 1x |
var fmt = new Intl.NumberFormat('%s', %s);
|
| 257 | 1x |
var value = parseFloat(params.value) || 0; |
| 258 | 1x |
return params.marker + ' ' + |
| 259 | 1x |
params.name + ': ' + fmt.format(value); |
| 260 | 1x |
}", locale, jsonlite::toJSON(opts, auto_unbox = TRUE))) |
| 261 | ||
| 262 | 1x |
tip <- structure(tip, class = c("JS_EVAL", "item_formatter"))
|
| 263 | 1x |
return(tip) |
| 264 |
} |
|
| 265 | ||
| 266 |
#' @rdname e-tooltip |
|
| 267 |
#' @export |
|
| 268 |
e_tooltip_pie_formatter <- function( |
|
| 269 |
style = c("decimal", "percent", "currency"),
|
|
| 270 |
digits = 0, |
|
| 271 |
locale = NULL, |
|
| 272 |
currency = "USD", |
|
| 273 |
... |
|
| 274 |
) {
|
|
| 275 | 1x |
if (is.null(locale)) {
|
| 276 | 1x |
locale <- .get_locale() |
| 277 |
} |
|
| 278 | ||
| 279 | 1x |
style <- match.arg(style) |
| 280 | ||
| 281 | 1x |
opts <- list( |
| 282 | 1x |
style = style, |
| 283 | 1x |
minimumFractionDigits = digits, |
| 284 | 1x |
maximumFractionDigits = digits, |
| 285 | 1x |
currency = currency |
| 286 |
) |
|
| 287 | ||
| 288 | 1x |
tip <- htmlwidgets::JS(sprintf("function(params, ticket, callback) {
|
| 289 | 1x |
var fmt = new Intl.NumberFormat('%s', %s);
|
| 290 | 1x |
return params.marker + ' ' + |
| 291 | 1x |
params.name + ': ' + |
| 292 | 1x |
fmt.format(params.value); |
| 293 | 1x |
}", locale, jsonlite::toJSON(opts, auto_unbox = TRUE))) |
| 294 | ||
| 295 | 1x |
tip <- structure(tip, class = c("JS_EVAL", "item_formatter"))
|
| 296 | 1x |
return(tip) |
| 297 |
} |
|
| 298 | ||
| 299 |
#' @rdname e-tooltip |
|
| 300 |
#' @export |
|
| 301 |
e_tooltip_pointer_formatter <- function( |
|
| 302 |
style = c("decimal", "percent", "currency"),
|
|
| 303 |
digits = 0, |
|
| 304 |
locale = NULL, |
|
| 305 |
currency = "USD" |
|
| 306 |
) {
|
|
| 307 | 1x |
if (is.null(locale)) {
|
| 308 | 1x |
locale <- .get_locale() |
| 309 |
} |
|
| 310 | ||
| 311 | 1x |
style <- match.arg(style) |
| 312 | 1x |
opts <- list( |
| 313 | 1x |
style = style, |
| 314 | 1x |
minimumFractionDigits = digits, |
| 315 | 1x |
maximumFractionDigits = digits, |
| 316 | 1x |
currency = currency |
| 317 |
) |
|
| 318 | ||
| 319 | 1x |
tip <- htmlwidgets::JS(sprintf("function(params, ticket, callback) {
|
| 320 | 1x |
var fmt = new Intl.NumberFormat('%s', %s);
|
| 321 | 1x |
var res = params[0].value[0]; |
| 322 | 1x |
for (i = 0; i < params.length; i++) {
|
| 323 | 1x |
res += '<br />' + |
| 324 | 1x |
params[i].marker + ' ' + |
| 325 | 1x |
params[i].seriesName + ': ' + |
| 326 | 1x |
fmt.format(parseFloat(params[i].value[1])); |
| 327 |
} |
|
| 328 | 1x |
return res; |
| 329 | 1x |
}", locale, jsonlite::toJSON(opts, auto_unbox = TRUE))) |
| 330 | 1x |
tip <- structure(tip, class = c("JS_EVAL", "pointer_formatter"))
|
| 331 | 1x |
return(tip) |
| 332 |
} |
|
| 333 | ||
| 334 |
#' Legend |
|
| 335 |
#' |
|
| 336 |
#' Customise the legend. |
|
| 337 |
#' |
|
| 338 |
#' @inheritParams e_bar |
|
| 339 |
#' @param show Set to \code{FALSE} to hide the legend.
|
|
| 340 |
#' @param type Type of legend, \code{plain} or \code{scroll}.
|
|
| 341 |
#' @param icons A optional list of icons the same length as there are series, see example. |
|
| 342 |
#' |
|
| 343 |
#' @examples |
|
| 344 |
#' e <- cars |> |
|
| 345 |
#' e_charts(speed) |> |
|
| 346 |
#' e_scatter(dist, symbol_size = 5) |
|
| 347 |
#' |
|
| 348 |
#' # with legend |
|
| 349 |
#' e |
|
| 350 |
#' |
|
| 351 |
#' # without legend |
|
| 352 |
#' e |> |
|
| 353 |
#' e_legend(show = FALSE) |
|
| 354 |
#' |
|
| 355 |
#' # with icon |
|
| 356 |
#' # path is taken from http://svgicons.sparkk.fr/ |
|
| 357 |
#' path <- paste0( |
|
| 358 |
#' "path://M11.344,5.71c0-0.73,0.074-1.122,1.199-1.122", |
|
| 359 |
#' "h1.502V1.871h-2.404c-2.886,0-3.903,1.36-3.903,3.646", |
|
| 360 |
#' "v1.765h-1.8V10h1.8v8.128h3.601V10h2.403l0.32-2.718h", |
|
| 361 |
#' "-2.724L11.344,5.71z" |
|
| 362 |
#' ) |
|
| 363 |
#' |
|
| 364 |
#' e |> |
|
| 365 |
#' e_legend( |
|
| 366 |
#' icons = list(path) |
|
| 367 |
#' ) |
|
| 368 |
#' @seealso \href{https://echarts.apache.org/en/option.html#legend}{Additional arguments}
|
|
| 369 |
#' |
|
| 370 |
#' @export |
|
| 371 |
e_legend <- function(e, show = TRUE, type = c("plain", "scroll"), icons = NULL, ...) {
|
|
| 372 | 10x |
if (missing(e)) {
|
| 373 | 1x |
stop("must pass e", call. = FALSE)
|
| 374 |
} |
|
| 375 | 9x |
if (!is.null(icons)) {
|
| 376 | 3x |
if (length(icons) < length(e$x$opts$legend$data)) {
|
| 377 | 1x |
stop( |
| 378 | 1x |
"invalid number of icons; ", |
| 379 | 1x |
length(icons), |
| 380 | 1x |
" icons passed but ", |
| 381 | 1x |
length(e$x$opts$legend$data), |
| 382 | 1x |
" legend items." |
| 383 |
) |
|
| 384 | 2x |
} else if (length(icons) == 1) {
|
| 385 | 1x |
e$x$opts$legend$icon <- icons |
| 386 | 1x |
icons <- NULL |
| 387 |
} |
|
| 388 | ||
| 389 | ||
| 390 | 2x |
for (i in seq_along(e$x$opts$legend$data)) {
|
| 391 | 3x |
e$x$opts$legend$data[[i]] <- list(name = e$x$opts$legend$data[[i]]) |
| 392 | 3x |
e$x$opts$legend$data[[i]]$icon <- icons[[i]] |
| 393 |
} |
|
| 394 |
} |
|
| 395 | ||
| 396 | 8x |
legend <- list( |
| 397 | 8x |
show = show, |
| 398 | 8x |
type = type[1], |
| 399 |
... |
|
| 400 |
) |
|
| 401 | ||
| 402 | 8x |
if (!e$x$tl) {
|
| 403 | 7x |
e$x$opts$legend <- append(e$x$opts$legend, legend) |
| 404 |
} else {
|
|
| 405 | 1x |
e$x$opts$baseOption$legend <- append(e$x$opts$baseOption$legend, legend) |
| 406 |
} |
|
| 407 | ||
| 408 | 8x |
e |
| 409 |
} |
|
| 410 | ||
| 411 |
#' Toolbox |
|
| 412 |
#' |
|
| 413 |
#' Add toolbox interface. |
|
| 414 |
#' |
|
| 415 |
#' @inheritParams e_bar |
|
| 416 |
#' @param feature Feature to add, defaults to all. |
|
| 417 |
#' |
|
| 418 |
#' @details Valid \code{feature}:
|
|
| 419 |
#' \itemize{
|
|
| 420 |
#' \item{\code{saveAsImage}}
|
|
| 421 |
#' \item{\code{brush}}
|
|
| 422 |
#' \item{\code{restore}}
|
|
| 423 |
#' \item{\code{dataView}}
|
|
| 424 |
#' \item{\code{dataZoom}}
|
|
| 425 |
#' \item{\code{magicType}}
|
|
| 426 |
#' } |
|
| 427 |
#' |
|
| 428 |
#' @examples |
|
| 429 |
#' USArrests |> |
|
| 430 |
#' e_charts(UrbanPop) |> |
|
| 431 |
#' e_line(Assault) |> |
|
| 432 |
#' e_area(Murder, y_index = 1, x_index = 1) |> |
|
| 433 |
#' e_datazoom(x_index = 0) |
|
| 434 |
#' |
|
| 435 |
#' mtcars |> |
|
| 436 |
#' tibble::rownames_to_column("model") |>
|
|
| 437 |
#' e_charts(model) |> |
|
| 438 |
#' e_line(qsec) |> |
|
| 439 |
#' e_toolbox() |> |
|
| 440 |
#' e_toolbox_feature( |
|
| 441 |
#' feature = "magicType", |
|
| 442 |
#' type = list("line", "bar")
|
|
| 443 |
#' ) |
|
| 444 |
#' @seealso \href{https://echarts.apache.org/en/option.html#toolbox}{Additional arguments}
|
|
| 445 |
#' |
|
| 446 |
#' @rdname toolbox |
|
| 447 |
#' @export |
|
| 448 |
e_toolbox_feature <- function(e, feature, ...) {
|
|
| 449 | 18x |
if (missing(e)) {
|
| 450 | 1x |
stop("must pass e", call. = FALSE)
|
| 451 |
} |
|
| 452 | ||
| 453 | 17x |
if (missing(feature)) {
|
| 454 | 3x |
feature <- c("saveAsImage", "restore", "dataView", "dataZoom", "magicType", "brush")
|
| 455 |
} |
|
| 456 | ||
| 457 | 17x |
options <- list(...) |
| 458 | ||
| 459 | 17x |
if (!e$x$tl) {
|
| 460 | 13x |
if (!length(e$x$opts$toolbox)) {
|
| 461 | 12x |
e$x$opts$toolbox <- list(feature = list()) |
| 462 |
} |
|
| 463 | ||
| 464 | 13x |
for (i in seq_along(feature)) {
|
| 465 | 23x |
e$x$opts$toolbox$feature[[feature[i]]] <- list() |
| 466 |
} |
|
| 467 | ||
| 468 | 13x |
if (length(options)) {
|
| 469 | 1x |
for (i in seq_along(options)) {
|
| 470 | 1x |
e$x$opts$toolbox$feature[[feature]][[names(options)[i]]] <- options[[i]] |
| 471 |
} |
|
| 472 |
} |
|
| 473 |
} else {
|
|
| 474 | 4x |
if (!length(e$x$opts$baseOption$toolbox)) {
|
| 475 | 3x |
e$x$opts$baseOption$toolbox <- list(feature = list()) |
| 476 |
} |
|
| 477 | ||
| 478 | 4x |
for (i in seq_along(feature)) {
|
| 479 | 9x |
e$x$opts$baseOption$toolbox$feature[[feature[i]]] <- list() |
| 480 |
} |
|
| 481 | ||
| 482 | 4x |
if (length(options)) {
|
| 483 | 1x |
for (i in seq_along(options)) {
|
| 484 | 1x |
e$x$opts$baseOption$toolbox$feature[[feature]][[names(options)[i]]] <- options[[i]] |
| 485 |
} |
|
| 486 |
} |
|
| 487 |
} |
|
| 488 | ||
| 489 | 17x |
e |
| 490 |
} |
|
| 491 | ||
| 492 |
#' @rdname toolbox |
|
| 493 |
#' @export |
|
| 494 |
e_toolbox <- function(e, ...) {
|
|
| 495 | 3x |
if (missing(e)) {
|
| 496 | 1x |
stop("must pass e", call. = FALSE)
|
| 497 |
} |
|
| 498 | 2x |
if (!e$x$tl) {
|
| 499 | 1x |
e$x$opts$toolbox <- list(...) |
| 500 |
} else {
|
|
| 501 | 1x |
e$x$opts$baseOption$toolbox <- list(...) |
| 502 |
} |
|
| 503 | ||
| 504 | 2x |
e |
| 505 |
} |
|
| 506 | ||
| 507 |
#' Data zoom |
|
| 508 |
#' |
|
| 509 |
#' Add data zoom. |
|
| 510 |
#' |
|
| 511 |
#' @inheritParams e_bar |
|
| 512 |
#' @param toolbox Whether to add the toolbox, \code{\link{e_toolbox_feature}}, (\code{e_toolbox_feature(e, "dataZoom")}).
|
|
| 513 |
#' |
|
| 514 |
#' @examples |
|
| 515 |
#' USArrests |> |
|
| 516 |
#' e_charts(UrbanPop) |> |
|
| 517 |
#' e_line(Assault) |> |
|
| 518 |
#' e_area(Murder, y_index = 1, x_index = 1) |> |
|
| 519 |
#' e_y_axis(gridIndex = 1) |> |
|
| 520 |
#' e_x_axis(gridIndex = 1) |> |
|
| 521 |
#' e_grid(height = "35%") |> |
|
| 522 |
#' e_grid(height = "35%", top = "50%") |> |
|
| 523 |
#' e_toolbox_feature("dataZoom", title = list(zoom = "zoom", back = "back")) |>
|
|
| 524 |
#' e_datazoom(x_index = c(0, 1)) |
|
| 525 |
#' @seealso \href{https://echarts.apache.org/en/option.html#dataZoom}{Additional arguments}
|
|
| 526 |
#' |
|
| 527 |
#' @export |
|
| 528 |
e_datazoom <- function(e, x_index = NULL, y_index = NULL, toolbox = TRUE, ...) {
|
|
| 529 | 8x |
if (missing(e)) {
|
| 530 | 1x |
stop("must pass e", call. = FALSE)
|
| 531 |
} |
|
| 532 | ||
| 533 | 7x |
if (!is.null(x_index) && !is.null(y_index)) {
|
| 534 | 1x |
stop("pass x_index or y_index, not both", call. = FALSE)
|
| 535 |
} |
|
| 536 | ||
| 537 | 6x |
if (!e$x$tl) {
|
| 538 | 5x |
if (!length(e$x$opts$dataZoom)) { # initialize if not existing
|
| 539 | 5x |
e$x$opts$dataZoom <- list() |
| 540 |
} |
|
| 541 |
} else {
|
|
| 542 | 1x |
if (!length(e$x$opts$baseOption$dataZoom)) { # initialize if not existing
|
| 543 | 1x |
e$x$opts$baseOption$dataZoom <- list() |
| 544 |
} |
|
| 545 |
} |
|
| 546 | ||
| 547 | 6x |
if (!e$x$tl) {
|
| 548 | 5x |
if (!length(e$x$opts$toolbox$feature$dataZoom) && isTRUE(toolbox)) {
|
| 549 | 5x |
e <- e_toolbox_feature(e, "dataZoom") |
| 550 |
} |
|
| 551 |
} else {
|
|
| 552 | 1x |
if (!length(e$x$opts$baseOption$toolbox$feature$dataZoom) && isTRUE(toolbox)) {
|
| 553 | 1x |
e <- e_toolbox_feature(e, "dataZoom") |
| 554 |
} |
|
| 555 |
} |
|
| 556 | ||
| 557 | 6x |
opts <- list(...) |
| 558 | ! |
if (!is.null(x_index)) opts$xAxisIndex <- x_index |
| 559 | 2x |
if (!is.null(y_index)) opts$yAxisIndex <- y_index |
| 560 | ||
| 561 | 6x |
if (!e$x$tl) {
|
| 562 | 5x |
e$x$opts$dataZoom <- append(e$x$opts$dataZoom, list(opts)) |
| 563 |
} else {
|
|
| 564 | 1x |
e$x$opts$baseOption$dataZoom <- append(e$x$opts$baseOption$dataZoom, list(opts)) |
| 565 |
} |
|
| 566 | ||
| 567 | 6x |
e |
| 568 |
} |
|
| 569 | ||
| 570 |
#' Brush |
|
| 571 |
#' |
|
| 572 |
#' Add a brush. |
|
| 573 |
#' |
|
| 574 |
#' @inheritParams e_bar |
|
| 575 |
#' @param brush_link Links interaction between selected items in different series. |
|
| 576 |
#' |
|
| 577 |
#' @section \code{brush_link}:$
|
|
| 578 |
#' \itemize{
|
|
| 579 |
#' \item{\code{c(3, 4, 5)}, for interacting series with seriesIndex as 3, 4, or 5.}
|
|
| 580 |
#' \item{\code{all}, for interacting all series.}
|
|
| 581 |
#' \item{\code{none} for disabling.}
|
|
| 582 |
#' } |
|
| 583 |
#' |
|
| 584 |
#' @examples |
|
| 585 |
#' quakes |> |
|
| 586 |
#' e_charts(long) |> |
|
| 587 |
#' e_geo( |
|
| 588 |
#' boundingCoords = list( |
|
| 589 |
#' c(190, -10), |
|
| 590 |
#' c(180, -40) |
|
| 591 |
#' ) |
|
| 592 |
#' ) |> |
|
| 593 |
#' e_scatter(lat, mag, stations, coord_system = "geo", name = "mag") |> |
|
| 594 |
#' e_data(quakes, depth) |> |
|
| 595 |
#' e_scatter(mag, mag, stations, name = "mag & depth") |> |
|
| 596 |
#' e_grid(right = 40, top = 100, width = "30%") |> |
|
| 597 |
#' e_y_axis(type = "value", name = "depth", min = 3.5) |> |
|
| 598 |
#' e_brush() |> |
|
| 599 |
#' e_theme("dark")
|
|
| 600 |
#' @seealso \href{https://echarts.apache.org/en/option.html#brush}{Additional arguments}
|
|
| 601 |
#' |
|
| 602 |
#' @export |
|
| 603 |
e_brush <- function(e, x_index = NULL, y_index = NULL, brush_link = "all", ...) {
|
|
| 604 | 8x |
if (missing(e)) {
|
| 605 | 1x |
stop("must pass e", call. = FALSE)
|
| 606 |
} |
|
| 607 | ||
| 608 | 7x |
if (!is.null(x_index) && !is.null(y_index)) {
|
| 609 | 1x |
stop("pass x_index or y_index, not both", call. = FALSE)
|
| 610 |
} |
|
| 611 | ||
| 612 | 6x |
if (!e$x$tl) {
|
| 613 | 5x |
if (!length(e$x$opts$brush)) { # initiatilise if not existing
|
| 614 | 5x |
e$x$opts$brush <- list() |
| 615 |
} |
|
| 616 |
} else {
|
|
| 617 | 1x |
if (!length(e$x$opts$baseOption$brush)) { # initiatilise if not existing
|
| 618 | 1x |
e$x$opts$baseOption$brush <- list() |
| 619 |
} |
|
| 620 |
} |
|
| 621 | ||
| 622 | 6x |
if (!e$x$tl) {
|
| 623 | 5x |
if (!length(e$x$opts$toolbox$feature$brush)) {
|
| 624 | 5x |
e <- e_toolbox_feature(e, "brush") |
| 625 |
} |
|
| 626 |
} else {
|
|
| 627 | 1x |
if (!length(e$x$opts$baseOption$toolbox$feature$brush)) {
|
| 628 | 1x |
e <- e_toolbox_feature(e, "brush") |
| 629 |
} |
|
| 630 |
} |
|
| 631 | ||
| 632 | 6x |
opts <- list(brushLink = brush_link, ...) |
| 633 | 6x |
opts$xAxisIndex <- x_index |
| 634 | 6x |
opts$yAxisIndex <- y_index |
| 635 | ||
| 636 | 6x |
if (!e$x$tl) {
|
| 637 | 5x |
e$x$opts$brush <- append(e$x$opts$brush, opts) |
| 638 |
} else {
|
|
| 639 | 1x |
e$x$opts$baseOption$brush <- append(e$x$opts$baseOption$brush, opts) |
| 640 |
} |
|
| 641 | ||
| 642 | 6x |
e |
| 643 |
} |
|
| 644 | ||
| 645 |
#' Title |
|
| 646 |
#' |
|
| 647 |
#' Add title. |
|
| 648 |
#' |
|
| 649 |
#' @inheritParams e_bar |
|
| 650 |
#' @param text,subtext Title and Subtitle. |
|
| 651 |
#' @param link,sublink Title and Subtitle link. |
|
| 652 |
#' |
|
| 653 |
#' @examples |
|
| 654 |
#' quakes |> |
|
| 655 |
#' dplyr::mutate(mag = exp(mag) / 60) |> |
|
| 656 |
#' e_charts(stations) |> |
|
| 657 |
#' e_scatter(depth, mag) |> |
|
| 658 |
#' e_visual_map(min = 3, max = 7) |> |
|
| 659 |
#' e_title("Quakes", "Stations and Magnitude")
|
|
| 660 |
#' @seealso \href{https://echarts.apache.org/en/option.html#title}{Additional arguments}
|
|
| 661 |
#' |
|
| 662 |
#' @export |
|
| 663 |
e_title <- function(e, text = NULL, subtext = NULL, link = NULL, sublink = NULL, ...) {
|
|
| 664 | 18x |
if (missing(e)) {
|
| 665 | 1x |
stop("must pass e", call. = FALSE)
|
| 666 |
} |
|
| 667 | ||
| 668 | 17x |
title <- list(...) |
| 669 | 17x |
title$text <- text |
| 670 | 17x |
title$subtext <- subtext |
| 671 | 17x |
title$link <- link |
| 672 | 17x |
title$sublink <- sublink |
| 673 | ||
| 674 | 17x |
if (!e$x$tl) {
|
| 675 | 16x |
e$x$opts$title <- append(e$x$opts$title, list(title)) |
| 676 |
} else {
|
|
| 677 | 1x |
e$x$opts$baseOption$title <- append(e$x$opts$baseOption$title, list(title)) |
| 678 |
} |
|
| 679 | ||
| 680 | 17x |
e |
| 681 |
} |
|
| 682 | ||
| 683 |
#' Polar |
|
| 684 |
#' |
|
| 685 |
#' Customise polar coordinates. |
|
| 686 |
#' |
|
| 687 |
#' @inheritParams e_bar |
|
| 688 |
#' @param show Whether to display the axis. |
|
| 689 |
#' |
|
| 690 |
#' @examples |
|
| 691 |
#' df <- data.frame(x = 1:10, y = seq(1, 20, by = 2)) |
|
| 692 |
#' |
|
| 693 |
#' df |> |
|
| 694 |
#' e_charts(x) |> |
|
| 695 |
#' e_polar() |> |
|
| 696 |
#' e_angle_axis() |> |
|
| 697 |
#' e_radius_axis() |> |
|
| 698 |
#' e_line(y, coord_system = "polar", smooth = TRUE) |
|
| 699 |
#' @seealso \href{https://echarts.apache.org/en/option.html#polar}{Additional arguments}
|
|
| 700 |
#' |
|
| 701 |
#' @export |
|
| 702 |
e_polar <- function(e, show = TRUE, ...) {
|
|
| 703 | 12x |
if (missing(e)) {
|
| 704 | 1x |
stop("must pass e", call. = FALSE)
|
| 705 |
} |
|
| 706 | ||
| 707 | 11x |
if (!e$x$tl) {
|
| 708 | 10x |
e$x$opts$yAxis <- NULL |
| 709 | 10x |
e$x$opts$xAxis <- NULL |
| 710 | ||
| 711 | 10x |
e$x$opts$polar <- list(show = show, ...) |
| 712 |
} else {
|
|
| 713 | 1x |
e$x$opts$baseOption$yAxis <- NULL |
| 714 | 1x |
e$x$opts$baseOption$xAxis <- NULL |
| 715 | ||
| 716 | 1x |
e$x$opts$baseOption$polar <- list(show = show, ...) |
| 717 |
} |
|
| 718 | ||
| 719 | 11x |
e |
| 720 |
} |
|
| 721 | ||
| 722 |
#' Axis pointer |
|
| 723 |
#' |
|
| 724 |
#' Customise axis pointer. |
|
| 725 |
#' |
|
| 726 |
#' @inheritParams e_bar |
|
| 727 |
#' |
|
| 728 |
#' @seealso \href{https://echarts.apache.org/en/option.html#axisPointer}{Additional arguments}
|
|
| 729 |
#' |
|
| 730 |
#' @export |
|
| 731 |
e_axis_pointer <- function(e, ...) {
|
|
| 732 | 4x |
if (missing(e)) {
|
| 733 | 1x |
stop("must pass e", call. = FALSE)
|
| 734 |
} |
|
| 735 | ||
| 736 | 3x |
if (!e$x$tl) {
|
| 737 | 2x |
e$x$opts$axisPointer <- list(...) |
| 738 |
} else {
|
|
| 739 | 1x |
e$x$opts$baseOption$axisPointer <- list(...) |
| 740 |
} |
|
| 741 | ||
| 742 | 3x |
e |
| 743 |
} |
|
| 744 | ||
| 745 |
#' Animation |
|
| 746 |
#' |
|
| 747 |
#' Customise animations. |
|
| 748 |
#' |
|
| 749 |
#' @inheritParams e_bar |
|
| 750 |
#' @param show Set to show animation. |
|
| 751 |
#' @param threshold Whether to set graphic number threshold to animation. |
|
| 752 |
#' Animation will be disabled when graphic number is larger than threshold. |
|
| 753 |
#' @param duration Duration of the first animation. |
|
| 754 |
#' @param easing Easing method used for the first animation. |
|
| 755 |
#' @param delay Delay before updating the first animation. |
|
| 756 |
#' @param duration.update Time for animation to complete. |
|
| 757 |
#' @param easing.update Easing method used for animation. |
|
| 758 |
#' @param delay.update Delay before updating animation. |
|
| 759 |
#' |
|
| 760 |
#' |
|
| 761 |
#' @examples |
|
| 762 |
#' mtcars |> |
|
| 763 |
#' e_charts(mpg) |> |
|
| 764 |
#' e_area(drat) |> |
|
| 765 |
#' e_animation(duration = 10000) |
|
| 766 |
#' @seealso \href{https://echarts.apache.org/en/option.html#animation}{Additional arguments}
|
|
| 767 |
#' |
|
| 768 |
#' @export |
|
| 769 |
e_animation <- function( |
|
| 770 |
e, |
|
| 771 |
show = TRUE, |
|
| 772 |
threshold = NULL, |
|
| 773 |
duration = NULL, |
|
| 774 |
easing = NULL, |
|
| 775 |
delay = NULL, |
|
| 776 |
duration.update = NULL, |
|
| 777 |
easing.update = NULL, |
|
| 778 |
delay.update = NULL |
|
| 779 |
) {
|
|
| 780 | 5x |
if (missing(e)) {
|
| 781 | 1x |
stop("must pass e", call. = FALSE)
|
| 782 |
} |
|
| 783 | 4x |
if (!e$x$tl) {
|
| 784 | 3x |
e$x$opts$animation <- show |
| 785 | 3x |
e$x$opts$animationThreshold <- threshold |
| 786 | 3x |
e$x$opts$animationDuration <- duration |
| 787 | 3x |
e$x$opts$animationEasing <- easing |
| 788 | 3x |
e$x$opts$animationDelay <- delay |
| 789 | 3x |
e$x$opts$animationDurationUpdate <- duration.update |
| 790 | 3x |
e$x$opts$animationEasingUpdate <- easing.update |
| 791 | 3x |
e$x$opts$animationDelayUpdate <- delay.update |
| 792 |
} else {
|
|
| 793 | 1x |
e$x$opts$baseOption$animation <- show |
| 794 | 1x |
e$x$opts$baseOption$animationThreshold <- threshold |
| 795 | 1x |
e$x$opts$baseOption$animationDuration <- duration |
| 796 | 1x |
e$x$opts$baseOption$animationEasing <- easing |
| 797 | 1x |
e$x$opts$baseOption$animationDelay <- delay |
| 798 | 1x |
e$x$opts$baseOption$animationDurationUpdate <- duration.update |
| 799 | 1x |
e$x$opts$baseOption$animationEasingUpdate <- easing.update |
| 800 | 1x |
e$x$opts$baseOption$animationDelayUpdate <- delay.update |
| 801 |
} |
|
| 802 | ||
| 803 | 4x |
e |
| 804 |
} |
|
| 805 | ||
| 806 |
#' Use UTC |
|
| 807 |
#' |
|
| 808 |
#' Use UTC |
|
| 809 |
#' |
|
| 810 |
#' @inheritParams e_bar |
|
| 811 |
#' |
|
| 812 |
#' @export |
|
| 813 |
e_utc <- function(e) {
|
|
| 814 | 4x |
if (missing(e)) {
|
| 815 | 1x |
stop("must pass e", call. = FALSE)
|
| 816 |
} |
|
| 817 | 3x |
if (!e$x$tl) {
|
| 818 | 2x |
e$x$opts$useUTC <- TRUE |
| 819 |
} else {
|
|
| 820 | 1x |
e$x$opts$baseOption$useUTC <- TRUE |
| 821 |
} |
|
| 822 | ||
| 823 | 3x |
e |
| 824 |
} |
|
| 825 | ||
| 826 |
#' Flip coordinates |
|
| 827 |
#' |
|
| 828 |
#' Flip cartesian 2D coordinates. |
|
| 829 |
#' |
|
| 830 |
#' @inheritParams e_bar |
|
| 831 |
#' |
|
| 832 |
#' @examples |
|
| 833 |
#' df <- data.frame( |
|
| 834 |
#' x = LETTERS[1:5], |
|
| 835 |
#' y = runif(5, 1, 5), |
|
| 836 |
#' z = runif(5, 3, 10) |
|
| 837 |
#' ) |
|
| 838 |
#' |
|
| 839 |
#' df |> |
|
| 840 |
#' e_charts(x) |> |
|
| 841 |
#' e_bar(y) |> |
|
| 842 |
#' e_line(z) -> plot |
|
| 843 |
#' |
|
| 844 |
#' plot # normal |
|
| 845 |
#' e_flip_coords(plot) # flip |
|
| 846 |
#' @export |
|
| 847 |
e_flip_coords <- function(e) {
|
|
| 848 | 6x |
if (missing(e)) {
|
| 849 | 1x |
stop("must pass e", call. = FALSE)
|
| 850 |
} |
|
| 851 | 5x |
if (!e$x$tl) {
|
| 852 | 3x |
n <- names(e$x$opts) |
| 853 | 3x |
n <- gsub("xAxis", "RENAME", n)
|
| 854 | 3x |
n <- gsub("yAxis", "xAxis", n)
|
| 855 | 3x |
n <- gsub("RENAME", "yAxis", n)
|
| 856 | ||
| 857 | 3x |
names(e$x$opts) <- n |
| 858 | ||
| 859 | 3x |
for (i in seq_along(e$x$opts$series)) {
|
| 860 | 3x |
for (j in seq_along(e$x$opts$series[[i]]$data)) {
|
| 861 | ||
| 862 | 10x |
if(!is.null(names(e$x$opts$series[[i]]$data[[j]]))){
|
| 863 | 10x |
try({
|
| 864 | 10x |
vals <- e$x$opts$series[[i]]$data[[j]]$value |
| 865 | 10x |
e$x$opts$series[[i]]$data[[j]]$value <- rev(vals) |
| 866 | 10x |
}, silent = TRUE) |
| 867 |
} else {
|
|
| 868 | ! |
try({
|
| 869 | ! |
vals <- e$x$opts$series[[i]]$data[[j]] |
| 870 | ! |
e$x$opts$series[[i]]$data[[j]] <- rev(vals) |
| 871 | ! |
}, silent = TRUE) |
| 872 |
} |
|
| 873 |
} |
|
| 874 |
} |
|
| 875 |
} else {
|
|
| 876 | 2x |
n <- names(e$x$opts$baseOption) |
| 877 | 2x |
n <- gsub("xAxis", "RENAME", n)
|
| 878 | 2x |
n <- gsub("yAxis", "xAxis", n)
|
| 879 | 2x |
n <- gsub("RENAME", "yAxis", n)
|
| 880 | ||
| 881 | 2x |
names(e$x$opts$baseOption) <- n |
| 882 | ||
| 883 | 2x |
for (i in seq_along(e$x$opts$options)) {
|
| 884 | 4x |
for (j in seq_along(e$x$opts$options[[i]]$series)) {
|
| 885 | 4x |
for (k in seq_along(e$x$opts$options[[i]]$series[[j]]$data)) {
|
| 886 | 10x |
try({
|
| 887 | 10x |
vals <- e$x$opts$options[[i]]$series[[j]]$data[[k]]$value |
| 888 | 10x |
e$x$opts$options[[i]]$series[[j]]$data[[k]]$value <- rev(vals) |
| 889 | 10x |
}, silent = TRUE) |
| 890 |
} |
|
| 891 |
} |
|
| 892 |
} |
|
| 893 |
} |
|
| 894 | ||
| 895 | 5x |
e |
| 896 |
} |
|
| 897 | ||
| 898 |
#' Text style |
|
| 899 |
#' |
|
| 900 |
#' Define global font style. Font styles may have to be specified in their |
|
| 901 |
#' respected element. |
|
| 902 |
#' |
|
| 903 |
#' @inheritParams e_bar |
|
| 904 |
#' |
|
| 905 |
#' |
|
| 906 |
#' @seealso \href{https://echarts.apache.org/en/option.html#textStyle}{official
|
|
| 907 |
#' documentation} |
|
| 908 |
#' |
|
| 909 |
#' @examples |
|
| 910 |
#' cars |> |
|
| 911 |
#' e_charts(dist) |> |
|
| 912 |
#' e_scatter(speed) |> |
|
| 913 |
#' e_labels(color = "#1E90FF") |> |
|
| 914 |
#' e_text_style( |
|
| 915 |
#' fontStyle = "italic" |
|
| 916 |
#' ) |
|
| 917 |
#' @export |
|
| 918 |
e_text_style <- function(e, ...) {
|
|
| 919 | 6x |
if (missing(e)) {
|
| 920 | 1x |
stop("must pass e", call. = FALSE)
|
| 921 |
} |
|
| 922 | 5x |
if (!e$x$tl) {
|
| 923 | 4x |
e$x$opts$textStyle <- list(...) |
| 924 |
} else {
|
|
| 925 | 1x |
e$x$opts$baseOption$textStyle <- list(...) |
| 926 |
} |
|
| 927 | ||
| 928 | 5x |
e |
| 929 |
} |
|
| 930 | ||
| 931 |
#' Connect charts |
|
| 932 |
#' |
|
| 933 |
#' Connect charts together. |
|
| 934 |
#' |
|
| 935 |
#' @inheritParams e_bar |
|
| 936 |
#' @param ids Scalar, vector or list of ids of chart to connect with. |
|
| 937 |
#' @param rows,cols Number of rows and columns. |
|
| 938 |
#' @param ... Any \code{echarts} objects.
|
|
| 939 |
#' @param width Width of columns, one of \code{xs}, \code{md}, \code{lg}.
|
|
| 940 |
#' @param group Group name. |
|
| 941 |
#' @param title Title of charts. |
|
| 942 |
#' |
|
| 943 |
#' @section Functions: |
|
| 944 |
#' \itemize{
|
|
| 945 |
#' \item{\code{e_connect}: connects charts by \code{ids}, \emph{cannot} be disconnected.}
|
|
| 946 |
#' \item{\code{e_group}: assigns a group to chart.}
|
|
| 947 |
#' \item{\code{e_connect_group}: connects chart with another group.}
|
|
| 948 |
#' \item{\code{e_disconnect_group}: diconnects chart from group.}
|
|
| 949 |
#' \item{\code{e_arrange}: arrange charts.}
|
|
| 950 |
#' } |
|
| 951 |
#' |
|
| 952 |
#' @return \code{e_arrange}: in an interactive session, returns a \code{htmltools::browsable}, in \code{rmarkdown} returns a
|
|
| 953 |
#' container (\code{htmltools::div}).
|
|
| 954 |
#' |
|
| 955 |
#' @examples |
|
| 956 |
#' # linked datazoom |
|
| 957 |
#' e1 <- cars |> |
|
| 958 |
#' e_charts( |
|
| 959 |
#' speed, |
|
| 960 |
#' height = 200 |
|
| 961 |
#' ) |> |
|
| 962 |
#' e_scatter(dist) |> |
|
| 963 |
#' e_datazoom(show = FALSE) |> |
|
| 964 |
#' e_group("grp") # assign group
|
|
| 965 |
#' |
|
| 966 |
#' e2 <- cars |> |
|
| 967 |
#' e_charts( |
|
| 968 |
#' dist, |
|
| 969 |
#' height = 200 |
|
| 970 |
#' ) |> |
|
| 971 |
#' e_scatter(speed) |> |
|
| 972 |
#' e_datazoom() |> |
|
| 973 |
#' e_group("grp") |> # assign group
|
|
| 974 |
#' e_connect_group("grp") # connect
|
|
| 975 |
#' |
|
| 976 |
#' if (interactive()) {
|
|
| 977 |
#' e_arrange(e1, e2, title = "Linked datazoom") |
|
| 978 |
#' } |
|
| 979 |
#' @note \code{e_arrange} may not work properly in the RStudio viewer.
|
|
| 980 |
#' |
|
| 981 |
#' @name connections |
|
| 982 |
#' @export |
|
| 983 |
e_connect <- function(e, ids) {
|
|
| 984 | 3x |
if (missing(e)) {
|
| 985 | 1x |
stop("must pass e", call. = FALSE)
|
| 986 |
} |
|
| 987 | 1x |
if (missing(ids)) stop("missing ids", call. = FALSE)
|
| 988 | 1x |
e$x$connect <- as.list(ids) |
| 989 | 1x |
return(e) |
| 990 |
} |
|
| 991 | ||
| 992 |
#' @rdname connections |
|
| 993 |
#' @export |
|
| 994 |
e_group <- function(e, group) {
|
|
| 995 | 4x |
if (missing(e)) {
|
| 996 | 1x |
stop("must pass e", call. = FALSE)
|
| 997 |
} |
|
| 998 | 3x |
if (missing(group)) {
|
| 999 | 1x |
stop("missing group", call. = FALSE)
|
| 1000 |
} |
|
| 1001 | ||
| 1002 | 2x |
e$x$chartGroup <- group |
| 1003 | ||
| 1004 | 2x |
e |
| 1005 |
} |
|
| 1006 | ||
| 1007 |
#' @rdname connections |
|
| 1008 |
#' @export |
|
| 1009 |
e_connect_group <- function(e, group) {
|
|
| 1010 | 3x |
if (missing(e)) {
|
| 1011 | 1x |
stop("must pass e", call. = FALSE)
|
| 1012 |
} |
|
| 1013 | 2x |
if (missing(group)) {
|
| 1014 | 1x |
stop("missing group", call. = FALSE)
|
| 1015 |
} |
|
| 1016 | ||
| 1017 | 1x |
e$x$groupConnect <- group |
| 1018 | ||
| 1019 | 1x |
e |
| 1020 |
} |
|
| 1021 | ||
| 1022 |
#' @rdname connections |
|
| 1023 |
#' @export |
|
| 1024 |
e_disconnect_group <- function(e, group = NULL) {
|
|
| 1025 | 2x |
if (missing(e)) {
|
| 1026 | 1x |
stop("must pass e", call. = FALSE)
|
| 1027 |
} |
|
| 1028 | 1x |
e$x$groupDisconnect <- group |
| 1029 | ||
| 1030 | 1x |
e |
| 1031 |
} |
|
| 1032 | ||
| 1033 |
#' @rdname connections |
|
| 1034 |
#' @export |
|
| 1035 |
e_arrange <- function(..., rows = NULL, cols = NULL, width = "xs", title = NULL) {
|
|
| 1036 | 7x |
plots <- list(...) |
| 1037 | ||
| 1038 | 7x |
if (is.null(rows)) {
|
| 1039 | 5x |
rows <- length(plots) |
| 1040 |
} |
|
| 1041 | ||
| 1042 | 7x |
if (is.null(cols)) {
|
| 1043 | 5x |
cols <- 1 |
| 1044 |
} |
|
| 1045 | ||
| 1046 | 7x |
w <- "-xs" |
| 1047 | 7x |
if (!isTRUE(getOption("knitr.in.progress"))) {
|
| 1048 | 1x |
w <- "" |
| 1049 |
} |
|
| 1050 | ||
| 1051 | 7x |
x <- 0 |
| 1052 | 7x |
tg <- htmltools::tagList() |
| 1053 | 7x |
for (i in 1:rows) {
|
| 1054 | 11x |
r <- htmltools::div(class = "row") |
| 1055 | ||
| 1056 | 11x |
for (j in 1:cols) {
|
| 1057 | 14x |
x <- x + 1 |
| 1058 | 14x |
cl <- paste0("col", w, "-", 12 / cols)
|
| 1059 | 14x |
if (x <= length(plots)) {
|
| 1060 | 11x |
c <- htmltools::div(class = cl, plots[[x]]) |
| 1061 |
} else {
|
|
| 1062 | 3x |
c <- htmltools::div(class = cl) |
| 1063 |
} |
|
| 1064 | 14x |
r <- htmltools::tagAppendChild(r, c) |
| 1065 |
} |
|
| 1066 | 11x |
tg <- htmltools::tagAppendChild(tg, r) |
| 1067 |
} |
|
| 1068 | ||
| 1069 | 7x |
if (!isTRUE(getOption("knitr.in.progress"))) {
|
| 1070 | 1x |
htmltools::browsable( |
| 1071 | 1x |
htmltools::div( |
| 1072 | 1x |
class = "container-fluid", |
| 1073 | 1x |
htmltools::tags$head( |
| 1074 | 1x |
htmltools::tags$link( |
| 1075 | 1x |
rel="stylesheet", |
| 1076 | 1x |
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css", |
| 1077 | 1x |
integrity="sha384-YEteyAKbjYSLDrpbBY0aFgK1CrQHo0Erb5XT/MU5FYvkb3SmPSZSisW7LOYTKvTx", |
| 1078 | 1x |
crossorigin="anonymous" |
| 1079 |
) |
|
| 1080 |
), |
|
| 1081 | 1x |
htmltools::h3(title), |
| 1082 | 1x |
tg |
| 1083 |
) |
|
| 1084 |
) |
|
| 1085 |
} else {
|
|
| 1086 | 6x |
if (!is.null(title)) {
|
| 1087 | 1x |
htmltools::div(title, tg) |
| 1088 |
} else {
|
|
| 1089 | 5x |
tg |
| 1090 |
} |
|
| 1091 |
} |
|
| 1092 |
} |
|
| 1093 | ||
| 1094 |
#' Dimensions |
|
| 1095 |
#' |
|
| 1096 |
#' Sets the dimensions of the chart _internally._ |
|
| 1097 |
#' This will only affect the dimensions of the |
|
| 1098 |
#' chart within its parent container. |
|
| 1099 |
#' Use the `height` and `width` arguments of |
|
| 1100 |
#' [e_charts] if you want to change the dimensions |
|
| 1101 |
#' of said parent (recommended). |
|
| 1102 |
#' |
|
| 1103 |
#' @inheritParams e_bar |
|
| 1104 |
#' @param height,width Dimensions in pixels, percentage or string. |
|
| 1105 |
#' |
|
| 1106 |
#' @export |
|
| 1107 |
e_dims <- function(e, height = "auto", width = "auto") {
|
|
| 1108 | 3x |
if (missing(e)) {
|
| 1109 | 1x |
stop("must pass e", call. = FALSE)
|
| 1110 |
} |
|
| 1111 | 2x |
e$x$mainOpts$width <- width |
| 1112 | 2x |
e$x$mainOpts$height <- height |
| 1113 | 2x |
return(e) |
| 1114 |
} |
|
| 1115 | ||
| 1116 |
#' Locale |
|
| 1117 |
#' |
|
| 1118 |
#' Change the locale to auto-translate |
|
| 1119 |
#' days of the week, etc. |
|
| 1120 |
#' |
|
| 1121 |
#' @section Locales: |
|
| 1122 |
#' - AR |
|
| 1123 |
#' - CS |
|
| 1124 |
#' - DE |
|
| 1125 |
#' - EN |
|
| 1126 |
#' - ES |
|
| 1127 |
#' - FA |
|
| 1128 |
#' - FI |
|
| 1129 |
#' - FR |
|
| 1130 |
#' - HU |
|
| 1131 |
#' - IT |
|
| 1132 |
#' - JA |
|
| 1133 |
#' - KO |
|
| 1134 |
#' - NL |
|
| 1135 |
#' - PL |
|
| 1136 |
#' - PT (brazil) |
|
| 1137 |
#' - RO |
|
| 1138 |
#' - RU |
|
| 1139 |
#' - SI |
|
| 1140 |
#' - SV |
|
| 1141 |
#' - TH |
|
| 1142 |
#' - TR |
|
| 1143 |
#' - UK |
|
| 1144 |
#' - VI |
|
| 1145 |
#' - ZH |
|
| 1146 |
#' |
|
| 1147 |
#' @examples |
|
| 1148 |
#' # top right corner zoom is in |
|
| 1149 |
#' # French |
|
| 1150 |
#' cars |> |
|
| 1151 |
#' e_charts(speed) |> |
|
| 1152 |
#' e_scatter(dist) |> |
|
| 1153 |
#' e_datazoom() |> |
|
| 1154 |
#' e_locale("FR")
|
|
| 1155 |
#' |
|
| 1156 |
#' @inheritParams e_bar |
|
| 1157 |
#' @param locale Locale to set to. |
|
| 1158 |
#' @param path Path to the local file to use. |
|
| 1159 |
#' |
|
| 1160 |
#' @details The "manual" function expects a file |
|
| 1161 |
#' to use for translations. |
|
| 1162 |
#' You can browse the `.js` files |
|
| 1163 |
#' [here](https://github.com/apache/echarts/tree/master/i18n) |
|
| 1164 |
#' to have an idea of what they should look like. |
|
| 1165 |
#' |
|
| 1166 |
#' @name e_locale |
|
| 1167 |
#' @export |
|
| 1168 |
e_locale <- function(e, locale){
|
|
| 1169 | 6x |
if (missing(e)) {
|
| 1170 | 1x |
stop("must pass e", call. = FALSE)
|
| 1171 |
} |
|
| 1172 | 5x |
if(missing(locale)) |
| 1173 | 1x |
stop("missing locale", call. = FALSE)
|
| 1174 | ||
| 1175 | 4x |
locale <- toupper(locale) |
| 1176 | ||
| 1177 | 4x |
if(!locale %in% c("ZH", "EN")){
|
| 1178 | 2x |
dep <- htmltools::htmlDependency( |
| 1179 | 2x |
name = sprintf("%s-echarts4r-locale", locale),
|
| 1180 | 2x |
version = utils::packageVersion("echarts4r"),
|
| 1181 | 2x |
src = "htmlwidgets/lib/echarts-6.1.0/i18n", |
| 1182 | 2x |
package = "echarts4r", |
| 1183 | 2x |
script = sprintf("lang%s.js", locale)
|
| 1184 |
) |
|
| 1185 | ||
| 1186 | 2x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 1187 |
} |
|
| 1188 | ||
| 1189 | 4x |
e$x$mainOpts$locale <- locale |
| 1190 | 4x |
return(e) |
| 1191 |
} |
|
| 1192 | ||
| 1193 |
#' @rdname e_locale |
|
| 1194 |
#' @export |
|
| 1195 |
e_locale_manual <- function(e, locale, path){
|
|
| 1196 | 4x |
if (missing(e)) {
|
| 1197 | 1x |
stop("must pass e", call. = FALSE)
|
| 1198 |
} |
|
| 1199 | 3x |
if(missing(locale)) |
| 1200 | 1x |
stop("missing locale", call. = FALSE)
|
| 1201 | ||
| 1202 | 2x |
if(missing(path)) |
| 1203 | 1x |
stop("missing path", call. = FALSE)
|
| 1204 | ||
| 1205 | 1x |
path <- normalizePath(path) |
| 1206 | ||
| 1207 | 1x |
file <- basename(path) |
| 1208 | 1x |
dir <- gsub(sprintf("%s$", file), "", path)
|
| 1209 | ||
| 1210 | 1x |
dep <- htmltools::htmlDependency( |
| 1211 | 1x |
name = sprintf("%s-manual-echarts4r-locale", locale),
|
| 1212 | 1x |
version = utils::packageVersion("echarts4r"),
|
| 1213 | 1x |
src = c( |
| 1214 | 1x |
file = dir |
| 1215 |
), |
|
| 1216 | 1x |
script = file |
| 1217 |
) |
|
| 1218 | ||
| 1219 | 1x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 1220 | ||
| 1221 | 1x |
e$x$mainOpts$locale <- locale |
| 1222 | 1x |
return(e) |
| 1223 |
} |
| 1 |
#' Wordcloud |
|
| 2 |
#' |
|
| 3 |
#' Draw a wordcloud. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param word,freq Terms and their frequencies. |
|
| 7 |
#' @param color Word color. |
|
| 8 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 9 |
#' |
|
| 10 |
#' @examples |
|
| 11 |
#' words <- function(n = 5000) {
|
|
| 12 |
#' a <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE)) |
|
| 13 |
#' paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
|
|
| 14 |
#' } |
|
| 15 |
#' |
|
| 16 |
#' tf <- data.frame( |
|
| 17 |
#' terms = words(100), |
|
| 18 |
#' freq = rnorm(100, 55, 10) |
|
| 19 |
#' ) |> |
|
| 20 |
#' dplyr::arrange(-freq) |
|
| 21 |
#' |
|
| 22 |
#' tf |> |
|
| 23 |
#' e_color_range(freq, color) |> |
|
| 24 |
#' e_charts() |> |
|
| 25 |
#' e_cloud(terms, freq, color, shape = "circle", sizeRange = c(3, 15)) |
|
| 26 |
#' @seealso \href{https://github.com/ecomfe/echarts-wordcloud}{official documentation}
|
|
| 27 |
#' |
|
| 28 |
#' @rdname e_cloud |
|
| 29 |
#' @export |
|
| 30 |
e_cloud <- function(e, word, freq, color, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 31 | 3x |
if (missing(e)) {
|
| 32 | 1x |
stop("must pass e", call. = FALSE)
|
| 33 |
} |
|
| 34 | ||
| 35 | 2x |
if (!missing(color)) {
|
| 36 | 1x |
cl <- deparse(substitute(color)) |
| 37 |
} else {
|
|
| 38 | 1x |
cl <- NULL |
| 39 |
} |
|
| 40 | ||
| 41 | 2x |
e_cloud_(e, deparse(substitute(word)), deparse(substitute(freq)), cl, rm_x, rm_y, ...) |
| 42 |
} |
|
| 43 | ||
| 44 |
#' @rdname e_cloud |
|
| 45 |
#' @export |
|
| 46 |
e_cloud_ <- function(e, word, freq, color = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 47 | 3x |
if (missing(e)) {
|
| 48 | 1x |
stop("must pass e", call. = FALSE)
|
| 49 |
} |
|
| 50 | ||
| 51 | 2x |
e <- .rm_axis(e, rm_x, "x") |
| 52 | 2x |
e <- .rm_axis(e, rm_y, "y") |
| 53 | ||
| 54 | 2x |
data <- .build_data(e, freq) |
| 55 | 2x |
data <- .add_bind(e, data, word) |
| 56 | ||
| 57 | 2x |
if (!is.null(color)) {
|
| 58 | 1x |
color <- .get_data(e, color) |
| 59 | 1x |
for (i in seq_along(data)) {
|
| 60 | 5x |
col <- list( |
| 61 | 5x |
color = color[i] |
| 62 |
) |
|
| 63 | 5x |
data[[i]]$textStyle <- col |
| 64 |
} |
|
| 65 |
} |
|
| 66 | ||
| 67 | 2x |
serie <- list( |
| 68 | 2x |
type = "wordCloud", |
| 69 | 2x |
data = data, |
| 70 |
... |
|
| 71 |
) |
|
| 72 | ||
| 73 | 2x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 74 | ||
| 75 |
# add dependency |
|
| 76 | 2x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 77 | 2x |
dep <- htmltools::htmlDependency( |
| 78 | 2x |
name = "echarts-wordcloud", |
| 79 | 2x |
version = "1.0.0", |
| 80 | 2x |
src = c(file = path), |
| 81 | 2x |
script = "echarts-wordcloud.min.js" |
| 82 |
) |
|
| 83 | ||
| 84 | 2x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 85 | ||
| 86 | 2x |
e |
| 87 |
} |
|
| 88 | ||
| 89 |
#' Liquid fill |
|
| 90 |
#' |
|
| 91 |
#' Draw liquid fill. |
|
| 92 |
#' |
|
| 93 |
#' @inheritParams e_bar |
|
| 94 |
#' @param color Column of color to plot. |
|
| 95 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 96 |
#' |
|
| 97 |
#' @examples |
|
| 98 |
#' df <- data.frame(val = c(0.6, 0.5, 0.4)) |
|
| 99 |
#' |
|
| 100 |
#' df |> |
|
| 101 |
#' e_charts() |> |
|
| 102 |
#' e_liquid(val) |> |
|
| 103 |
#' e_theme("dark")
|
|
| 104 |
#' @seealso \href{https://github.com/ecomfe/echarts-liquidfill}{official documentation}
|
|
| 105 |
#' |
|
| 106 |
#' @rdname e_liquid |
|
| 107 |
#' @export |
|
| 108 |
e_liquid <- function(e, serie, color, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 109 | 5x |
if (missing(e)) {
|
| 110 | 1x |
stop("must pass e", call. = FALSE)
|
| 111 |
} |
|
| 112 | 1x |
if(e$x$tl)( |
| 113 | 1x |
stop("timeline not supported")
|
| 114 |
) |
|
| 115 | ||
| 116 | 3x |
if (!missing(color)) {
|
| 117 | 1x |
cl <- deparse(substitute(color)) |
| 118 |
} else {
|
|
| 119 | 2x |
cl <- NULL |
| 120 |
} |
|
| 121 | ||
| 122 | 3x |
e_liquid_(e, deparse(substitute(serie)), cl, rm_x, rm_y, ...) |
| 123 |
} |
|
| 124 | ||
| 125 |
#' @rdname e_liquid |
|
| 126 |
#' @export |
|
| 127 |
e_liquid_ <- function(e, serie, color = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 128 | 4x |
if (missing(e)) {
|
| 129 | 1x |
stop("must pass e", call. = FALSE)
|
| 130 |
} |
|
| 131 | ||
| 132 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 133 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 134 | ||
| 135 | 3x |
data <- .get_data(e, serie) |> |
| 136 | 3x |
unlist() |> |
| 137 | 3x |
unname() |
| 138 | ||
| 139 | 3x |
serie <- list( |
| 140 | 3x |
type = "liquidFill", |
| 141 | 3x |
data = data, |
| 142 |
... |
|
| 143 |
) |
|
| 144 | ||
| 145 | 3x |
if (!is.null(color)) {
|
| 146 | 1x |
serie$color <- .get_data(e, color) |> |
| 147 | 1x |
unlist() |> |
| 148 | 1x |
unname() |
| 149 |
} |
|
| 150 | ||
| 151 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 152 | ||
| 153 |
# add dependency |
|
| 154 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 155 | 3x |
dep <- htmltools::htmlDependency( |
| 156 | 3x |
name = "echarts-liquidfill", |
| 157 | 3x |
version = "1.0.0", |
| 158 | 3x |
src = c(file = path), |
| 159 | 3x |
script = "echarts-liquidfill.min.js" |
| 160 |
) |
|
| 161 | ||
| 162 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 163 | ||
| 164 | 3x |
e |
| 165 |
} |
|
| 166 | ||
| 167 |
#' Modularity |
|
| 168 |
#' |
|
| 169 |
#' Graph modularity extension will do community detection and partian a graph's vertices in several subsets. |
|
| 170 |
#' Each subset will be assigned a different color. |
|
| 171 |
#' |
|
| 172 |
#' @inheritParams e_bar |
|
| 173 |
#' @param modularity Either set to \code{TRUE}, or a \code{list}.
|
|
| 174 |
#' |
|
| 175 |
#' @section Modularity: |
|
| 176 |
#' \itemize{
|
|
| 177 |
#' \item{\code{resolution} Resolution}
|
|
| 178 |
#' \item{\code{sort} Whether to sort to comunities}
|
|
| 179 |
#' } |
|
| 180 |
#' |
|
| 181 |
#' @examples |
|
| 182 |
#' nodes <- data.frame( |
|
| 183 |
#' name = paste0(LETTERS, 1:100), |
|
| 184 |
#' value = rnorm(100, 10, 2), |
|
| 185 |
#' stringsAsFactors = FALSE |
|
| 186 |
#' ) |
|
| 187 |
#' |
|
| 188 |
#' edges <- data.frame( |
|
| 189 |
#' source = sample(nodes$name, 200, replace = TRUE), |
|
| 190 |
#' target = sample(nodes$name, 200, replace = TRUE), |
|
| 191 |
#' stringsAsFactors = FALSE |
|
| 192 |
#' ) |
|
| 193 |
#' |
|
| 194 |
#' e_charts() |> |
|
| 195 |
#' e_graph() |> |
|
| 196 |
#' e_graph_nodes(nodes, name, value) |> |
|
| 197 |
#' e_graph_edges(edges, source, target) |> |
|
| 198 |
#' e_modularity( |
|
| 199 |
#' list( |
|
| 200 |
#' resolution = 5, |
|
| 201 |
#' sort = TRUE |
|
| 202 |
#' ) |
|
| 203 |
#' ) |
|
| 204 |
#' @note Does not work in RStudio viewer, open in browser. |
|
| 205 |
#' |
|
| 206 |
#' @seealso \href{https://github.com/ecomfe/echarts-graph-modularity}{Official documentation}
|
|
| 207 |
#' |
|
| 208 |
#' @export |
|
| 209 |
e_modularity <- function(e, modularity = TRUE) {
|
|
| 210 | 2x |
if (missing(e)) {
|
| 211 | 1x |
stop("must pass e", call. = FALSE)
|
| 212 |
} |
|
| 213 | 1x |
clu <- list( |
| 214 | 1x |
modularity = modularity |
| 215 |
) |
|
| 216 | ||
| 217 | 1x |
e$x$opts$series[[length(e$x$opts$series)]]$modularity <- clu |
| 218 | ||
| 219 |
# add dependency |
|
| 220 | 1x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 221 | 1x |
dep <- htmltools::htmlDependency( |
| 222 | 1x |
name = "echarts-modularity", |
| 223 | 1x |
version = "1.0.0", |
| 224 | 1x |
src = c(file = path), |
| 225 | 1x |
script = "echarts-graph-modularity.min.js" |
| 226 |
) |
|
| 227 | ||
| 228 | 1x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 229 | ||
| 230 | 1x |
e |
| 231 |
} |
|
| 232 | ||
| 233 |
#' Segmented Doughnut |
|
| 234 |
#' |
|
| 235 |
#' Draw segmented doughnut. |
|
| 236 |
#' |
|
| 237 |
#' @inheritParams e_bar |
|
| 238 |
#' @param numerator,denominator numeraetor to provide filled segments and denominator for total segments. |
|
| 239 |
#' @param formatter javascript string formatter for center text of chart. |
|
| 240 |
#' @param fontSize,fontColor font values for center text of chart. |
|
| 241 |
#' @param center,radius center provides relative position of the center of chart while radius provides the radius of your circle for outer segments. |
|
| 242 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 243 |
#' |
|
| 244 |
#' @examples |
|
| 245 |
#' |
|
| 246 |
#' |
|
| 247 |
#' e_chart() |> |
|
| 248 |
#' e_doughnut(numerator = 3, denominator = 6) |
|
| 249 |
#' |
|
| 250 |
#' @seealso \href{https://github.com/apache/echarts-custom-series/tree/main/custom-series/segmentedDoughnut}{official documentation}
|
|
| 251 |
#' |
|
| 252 |
#' @rdname e_doughnut |
|
| 253 |
#' @export |
|
| 254 |
e_doughnut <- function(e, |
|
| 255 |
numerator = NULL, |
|
| 256 |
denominator = NULL, |
|
| 257 |
formatter = "{c}/{b}",
|
|
| 258 |
fontSize = "10em", |
|
| 259 |
fontColor = "#555", |
|
| 260 |
center = c("50%","50%"),
|
|
| 261 |
radius = c("50%","65%"),
|
|
| 262 |
rm_x = TRUE, |
|
| 263 |
rm_y = TRUE, |
|
| 264 |
...) {
|
|
| 265 | ||
| 266 | 2x |
if (missing(e)) {
|
| 267 | 1x |
stop("must pass e", call. = FALSE)
|
| 268 |
} |
|
| 269 | ||
| 270 | 1x |
e <- .rm_axis(e, rm_x, "x") |
| 271 | 1x |
e <- .rm_axis(e, rm_y, "y") |
| 272 | ||
| 273 | 1x |
serie <- list( |
| 274 | 1x |
type = "custom", |
| 275 | 1x |
renderItem = "segmentedDoughnut", |
| 276 | 1x |
coordinateSystem = "none", |
| 277 | 1x |
itemPayload = list(center = as.list(center), |
| 278 | 1x |
radius = as.list(radius), |
| 279 | 1x |
segmentCount = denominator, |
| 280 | 1x |
label = list( |
| 281 | 1x |
show = TRUE, |
| 282 | 1x |
formatter = formatter, |
| 283 | 1x |
fontSize = fontSize, |
| 284 | 1x |
color = fontColor |
| 285 |
) |
|
| 286 |
), |
|
| 287 | 1x |
data = list(numerator), |
| 288 |
... |
|
| 289 |
) |
|
| 290 | ||
| 291 | 1x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 292 | ||
| 293 |
# add dependency |
|
| 294 | 1x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 295 | 1x |
dep <- htmltools::htmlDependency( |
| 296 | 1x |
name = "echarts-doughnut", |
| 297 | 1x |
version = "1.0.0", |
| 298 | 1x |
src = c(file = path), |
| 299 | 1x |
script = "echarts-doughnut.js") |
| 300 | ||
| 301 | 1x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 302 | ||
| 303 | 1x |
e |
| 304 |
} |
|
| 305 | ||
| 306 |
#' Violin chart |
|
| 307 |
#' |
|
| 308 |
#' Draw a violin chart with scattered dots. |
|
| 309 |
#' |
|
| 310 |
#' @inheritParams e_band2_ |
|
| 311 |
#' @param symbolSize overall size of symbol. |
|
| 312 |
#' @param areaOpacity opacity of violin area. |
|
| 313 |
#' @param bandWidthScale scale for the amplitude of violin area |
|
| 314 |
#' @param binCount number of bins for violin plot. More bins will provide a more detailed version of the plot, |
|
| 315 |
#' |
|
| 316 |
#' @examples |
|
| 317 |
#' |
|
| 318 |
#' PlantGrowth |> |
|
| 319 |
#' e_charts(group) |> |
|
| 320 |
#' e_scatter(weight) |> |
|
| 321 |
#' e_violin(binCount = 200) |
|
| 322 |
#' |
|
| 323 |
#' @seealso \href{https://github.com/apache/echarts-custom-series/tree/main/custom-series/violin}{official documentation}
|
|
| 324 |
#' |
|
| 325 |
#' @rdname e_violin |
|
| 326 |
#' @export |
|
| 327 |
e_violin <- function(e, |
|
| 328 |
name = "violin", |
|
| 329 |
legend = TRUE, |
|
| 330 |
y_index = 0, |
|
| 331 |
x_index = 0, |
|
| 332 |
symbolSize = 10, |
|
| 333 |
areaOpacity = 0.5, |
|
| 334 |
binCount = 100, |
|
| 335 |
bandWidthScale = 1, |
|
| 336 |
...){
|
|
| 337 | 5x |
if (missing(e)) {
|
| 338 | 1x |
stop("must pass e", call. = FALSE)
|
| 339 |
} |
|
| 340 | ||
| 341 | 4x |
if (e$x$tl) {
|
| 342 | 1x |
stop("timeline not supported", call. = FALSE)
|
| 343 |
} |
|
| 344 | ||
| 345 | 3x |
no_tl <- e$x$opts$series[[1]]$type |
| 346 | 3x |
tl <- e$x$opts$baseOption$series[[1]]$type |
| 347 | 3x |
is_scatter <- identical(no_tl, "scatter") || identical(tl, "scatter") |
| 348 | ||
| 349 | 3x |
if (is_scatter == FALSE & (is.null(no_tl) & is.null(tl)) |
| 350 |
) {
|
|
| 351 | 1x |
stop("violin is only supported with scatter plots", call. = FALSE)
|
| 352 |
} |
|
| 353 | ||
| 354 | 2x |
for (i in seq_along(e$x$opts$series)) {
|
| 355 | ||
| 356 |
# adjust scatter data to necessary format for violin polygons |
|
| 357 | 2x |
vector <- list() |
| 358 | 2x |
for(j in seq_along(e$x$opts$series[[i]]$data)){
|
| 359 | 300x |
vector <- append(vector,list(as.list(e$x$opts$series[[i]]$data[[j]]$value))) |
| 360 |
} |
|
| 361 | 2x |
e_serie <- list(data = vector) |
| 362 | ||
| 363 | 2x |
if (y_index != 0) {
|
| 364 | 1x |
serie <- e$x$opts$series[[i]]$name |
| 365 | 1x |
e <- .set_y_axis(e, serie, y_index, i) |
| 366 |
} |
|
| 367 | 2x |
if (x_index != 0) {
|
| 368 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 369 |
} |
|
| 370 | 2x |
serie <- list( |
| 371 | 2x |
type = "custom", |
| 372 | 2x |
renderItem = 'violin', |
| 373 | 2x |
silent = TRUE, |
| 374 | 2x |
itemPayload = list( |
| 375 | 2x |
symbolSize = symbolSize, |
| 376 | 2x |
areaOpacity = areaOpacity, |
| 377 | 2x |
binCount = binCount, |
| 378 | 2x |
bandWidthScale = bandWidthScale |
| 379 |
), |
|
| 380 |
... |
|
| 381 |
) |
|
| 382 | ||
| 383 | 2x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 384 | ||
| 385 | 2x |
if (isTRUE(legend)) {
|
| 386 |
# Adding these 2 lines and legend is added |
|
| 387 | 2x |
current_trace <- length(e$x$opts$series) |
| 388 | 2x |
e$x$opts$series[[current_trace]]$name <- name |
| 389 | 2x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(name)) |
| 390 |
} |
|
| 391 | 2x |
e$x$opts$dataset <- list(source = vector) |
| 392 |
} |
|
| 393 | ||
| 394 |
# add dependency |
|
| 395 | 2x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 396 | 2x |
dep <- htmltools::htmlDependency( |
| 397 | 2x |
name = "echarts-violin", |
| 398 | 2x |
version = "1.0.0", |
| 399 | 2x |
src = c(file = path), |
| 400 | 2x |
script = "echarts-violin.js") |
| 401 | ||
| 402 | 2x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 403 | ||
| 404 | 2x |
e |
| 405 |
} |
|
| 406 | ||
| 407 |
#' Bar range chart |
|
| 408 |
#' |
|
| 409 |
#' Draw a bar range plot with labels on each end. |
|
| 410 |
#' |
|
| 411 |
#' @inheritParams e_band2_ |
|
| 412 |
#' @param barWidth width of each bar |
|
| 413 |
#' @param borderRadius roundness of the bar ends |
|
| 414 |
#' @param margin space between the text labels and the bar |
|
| 415 |
#' @param textSymbol string for the label to end with |
|
| 416 |
#' |
|
| 417 |
#' @examples |
|
| 418 |
#' df <- iris |> |
|
| 419 |
#' dplyr::group_by(Species) |> |
|
| 420 |
#' dplyr::summarise(min_length = min(Sepal.Length), |
|
| 421 |
#' max_length = max(Sepal.Length)) |
|
| 422 |
#' |
|
| 423 |
#' df |> e_chart(Species) |> |
|
| 424 |
#' e_barRange(lower = min_length, |
|
| 425 |
#' upper = max_length, |
|
| 426 |
#' textSymbol = '"' |
|
| 427 |
#' ) |
|
| 428 |
#' @seealso \href{https://github.com/apache/echarts-custom-series/tree/main/custom-series/barRange}{official documentation}
|
|
| 429 |
#' |
|
| 430 |
#' @rdname e_barRange |
|
| 431 |
#' @export |
|
| 432 |
e_barRange <- function(e, |
|
| 433 |
lower, |
|
| 434 |
upper, |
|
| 435 |
name = "barRange", |
|
| 436 |
legend = TRUE, |
|
| 437 |
y_index = 0, |
|
| 438 |
x_index = 0, |
|
| 439 |
barWidth = 10, |
|
| 440 |
borderRadius = 5, |
|
| 441 |
margin = 10, |
|
| 442 |
textSymbol = "F", |
|
| 443 |
...){
|
|
| 444 | ||
| 445 | 4x |
if (missing(e)) {
|
| 446 | 1x |
stop("must pass e", call. = FALSE)
|
| 447 |
} |
|
| 448 | ||
| 449 | 3x |
for (i in seq_along(e$x$data)) {
|
| 450 | ||
| 451 | 5x |
vector <- .build_data2( |
| 452 | 5x |
e$x$data[[i]], |
| 453 | 5x |
e$x$mapping$x, |
| 454 | 5x |
deparse(substitute(lower)), |
| 455 | 5x |
deparse(substitute(upper)) |
| 456 |
) |
|
| 457 | 5x |
e_serie <- list(data = vector) |
| 458 | ||
| 459 | 5x |
if (y_index != 0) {
|
| 460 | 1x |
e <- .set_y_axis(e, deparse(substitute(upper)), y_index, i) |
| 461 |
} |
|
| 462 | 5x |
if (x_index != 0) {
|
| 463 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 464 |
} |
|
| 465 | ||
| 466 | 5x |
if (!e$x$tl) {
|
| 467 |
# generate series list |
|
| 468 | 2x |
serie <- list( |
| 469 | 2x |
type = "custom", |
| 470 | 2x |
renderItem = 'barRange', |
| 471 | 2x |
name = name, |
| 472 | 2x |
encode = list(x= 0, |
| 473 | 2x |
y = c(1,2), |
| 474 | 2x |
tooltip = c(1,2)), |
| 475 | 2x |
data = vector, |
| 476 | 2x |
itemPayload = list( |
| 477 | 2x |
textSymbol = textSymbol, |
| 478 | 2x |
barWidth = barWidth, |
| 479 | 2x |
borderRadius = borderRadius, |
| 480 | 2x |
margin = margin |
| 481 |
), |
|
| 482 |
... |
|
| 483 |
) |
|
| 484 | 2x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 485 | ||
| 486 | 2x |
if (isTRUE(legend)) {
|
| 487 | 2x |
current_trace <- length(e$x$opts$series) |
| 488 | 2x |
e$x$opts$series[[current_trace]]$name <- name |
| 489 | 2x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(name)) |
| 490 |
} |
|
| 491 | 2x |
e$x$opts$series <- append(e$x$opts$series, list(e_serie)) |
| 492 |
} |
|
| 493 |
else {
|
|
| 494 | 3x |
if (isTRUE(legend)) {
|
| 495 | 3x |
e$x$opts$legend$data <- append( |
| 496 | 3x |
e$x$opts$legend$data, |
| 497 | 3x |
list(name) |
| 498 |
) |
|
| 499 |
} |
|
| 500 | ||
| 501 | 3x |
e$x$opts$options[[i]]$series <- append( |
| 502 | 3x |
e$x$opts$options[[i]]$series, |
| 503 | 3x |
list(e_serie) |
| 504 |
) |
|
| 505 |
} |
|
| 506 |
} |
|
| 507 | 3x |
if (isTRUE(e$x$tl)) {
|
| 508 | 1x |
series_opts <- list( |
| 509 | 1x |
type = "custom", |
| 510 | 1x |
renderItem = 'barRange', |
| 511 | 1x |
name = name, |
| 512 | 1x |
encode = list(x= 0, |
| 513 | 1x |
y = c(1,2), |
| 514 | 1x |
tooltip = c(1,2)), |
| 515 | 1x |
data = vector, |
| 516 | 1x |
itemPayload = list( |
| 517 | 1x |
textSymbol = textSymbol, |
| 518 | 1x |
barWidth = barWidth, |
| 519 | 1x |
borderRadius = borderRadius, |
| 520 | 1x |
margin = margin |
| 521 |
), |
|
| 522 |
... |
|
| 523 |
) |
|
| 524 | ||
| 525 | 1x |
if (isTRUE(legend)) {
|
| 526 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 527 |
} |
|
| 528 | 1x |
e$x$opts$baseOption$series <- append( |
| 529 | 1x |
e$x$opts$baseOption$series, |
| 530 | 1x |
list(series_opts) |
| 531 |
) |
|
| 532 |
} |
|
| 533 | ||
| 534 |
# In the js I replaced "°C" with 't.itemPayload.textSymbol' since the symbol |
|
| 535 |
# was hard-coded |
|
| 536 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 537 | 3x |
dep <- htmltools::htmlDependency( |
| 538 | 3x |
name = "echarts-barRange", |
| 539 | 3x |
version = "1.0.0", |
| 540 | 3x |
src = c(file = path), |
| 541 | 3x |
script = "echarts-barRange.js") |
| 542 | ||
| 543 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 544 | ||
| 545 | 3x |
e |
| 546 |
} |
|
| 547 | ||
| 548 |
#' Contour chart |
|
| 549 |
#' |
|
| 550 |
#' Draw a contour plot. x and y must each be numbers. |
|
| 551 |
#' |
|
| 552 |
#' @inheritParams e_bar |
|
| 553 |
#' @param thresholds contour density |
|
| 554 |
#' @param bandwidth the size of the contours |
|
| 555 |
#' @param lineStyle list of properties of the line |
|
| 556 |
#' @param contourOpacity opacity of the countours |
|
| 557 |
#' @param contourColors colors used for the contours |
|
| 558 |
#' |
|
| 559 |
#' @examples |
|
| 560 |
#' |
|
| 561 |
#' mtcars |> |
|
| 562 |
#' e_charts(mpg) |> |
|
| 563 |
#' e_contour(serie = mpg) |
|
| 564 |
#' @seealso \href{https://github.com/apache/echarts-custom-series/tree/main/custom-series/contour}{official documentation}
|
|
| 565 |
#' |
|
| 566 |
#' @rdname e_contour |
|
| 567 |
#' @export |
|
| 568 |
e_contour <- function(e, |
|
| 569 |
serie, |
|
| 570 |
name = "contour", |
|
| 571 |
legend = TRUE, |
|
| 572 |
thresholds = 8, |
|
| 573 |
bandwidth = 20, |
|
| 574 |
lineStyle = list(opacity = 0.3, color = "black", width=1), |
|
| 575 |
contourOpacity = 0.8, |
|
| 576 |
contourColors = list('#5470c6', '#91cc75', '#fac858', '#ee6666'),
|
|
| 577 |
...){
|
|
| 578 | ||
| 579 | 3x |
if (missing(e)) {
|
| 580 | 1x |
stop("must pass e", call. = FALSE)
|
| 581 |
} |
|
| 582 | ||
| 583 | 2x |
for (i in seq_along(e$x$data)) {
|
| 584 | 3x |
vector <- .build_data2(e$x$data[[i]], e$x$mapping$x, deparse(substitute(serie))) |
| 585 | ||
| 586 | 3x |
e_serie <- list(data = vector) |
| 587 | ||
| 588 | 3x |
e <- e |> e_axis(type = 'value') |
| 589 | ||
| 590 | 3x |
if (!e$x$tl) {
|
| 591 | 1x |
opts <- list( |
| 592 | 1x |
type = "custom", |
| 593 | 1x |
renderItem = 'contour', |
| 594 | 1x |
encode = list(x= 0, |
| 595 | 1x |
y = 1, |
| 596 | 1x |
tooltip = 2), |
| 597 | 1x |
data = vector, |
| 598 | 1x |
name = name, |
| 599 | 1x |
itemPayload = list( |
| 600 | 1x |
thresholds = thresholds, |
| 601 | 1x |
bandwidth = bandwidth, |
| 602 | 1x |
lineStyle = lineStyle, |
| 603 | 1x |
itemStyle = list(opacity = contourOpacity, |
| 604 | 1x |
color = contourColors) |
| 605 |
), |
|
| 606 |
... |
|
| 607 |
) |
|
| 608 | ||
| 609 | 1x |
e_serie <- append(opts, e_serie) # data after renderItem, data used for Y-sizing only |
| 610 | ||
| 611 | 1x |
if (isTRUE(legend)) {
|
| 612 | 1x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(name)) |
| 613 |
} |
|
| 614 | 1x |
e$x$opts$series <- append(e$x$opts$series, list(e_serie)) |
| 615 | ||
| 616 |
} |
|
| 617 |
else {
|
|
| 618 | 2x |
if (isTRUE(legend)) {
|
| 619 | 2x |
e$x$opts$legend$data <- append( |
| 620 | 2x |
e$x$opts$legend$data, |
| 621 | 2x |
list(name) |
| 622 |
) |
|
| 623 |
} |
|
| 624 | 2x |
e$x$opts$options[[i]]$series <- append( |
| 625 | 2x |
e$x$opts$options[[i]]$series, |
| 626 | 2x |
list(e_serie) |
| 627 |
) |
|
| 628 |
} |
|
| 629 |
} |
|
| 630 | 2x |
if (isTRUE(e$x$tl)) {
|
| 631 |
# generate series list |
|
| 632 | 1x |
series_opts <- list( |
| 633 | 1x |
type = "custom", |
| 634 | 1x |
renderItem = 'contour', |
| 635 | 1x |
encode = list(x= 0, |
| 636 | 1x |
y = 1, |
| 637 | 1x |
tooltip = 2), |
| 638 | 1x |
data = vector, |
| 639 | 1x |
name = name, |
| 640 | 1x |
itemPayload = list( |
| 641 | 1x |
thresholds = thresholds, |
| 642 | 1x |
bandwidth = bandwidth, |
| 643 | 1x |
lineStyle = lineStyle, |
| 644 | 1x |
itemStyle = list(opacity = contourOpacity, |
| 645 | 1x |
color = contourColors) |
| 646 |
), |
|
| 647 |
... |
|
| 648 |
) |
|
| 649 | ||
| 650 | 1x |
if (isTRUE(legend)) {
|
| 651 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 652 |
} |
|
| 653 | 1x |
e$x$opts$baseOption$series <- append( |
| 654 | 1x |
e$x$opts$baseOption$series, |
| 655 | 1x |
list(series_opts) |
| 656 |
) |
|
| 657 |
} |
|
| 658 | ||
| 659 | 2x |
path_d3 <- system.file("htmlwidgets/lib/d3", package = "echarts4r")
|
| 660 | 2x |
dep_d3 <- htmltools::htmlDependency( |
| 661 | 2x |
name = "d3", |
| 662 | 2x |
version = "7.9.0", |
| 663 | 2x |
src = c(file = path_d3), |
| 664 | 2x |
script = "d3.min.js") |
| 665 | ||
| 666 | 2x |
e$dependencies <- append(e$dependencies, list(dep_d3)) |
| 667 | ||
| 668 | 2x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 669 | 2x |
dep <- htmltools::htmlDependency( |
| 670 | 2x |
name = "echarts-contour", |
| 671 | 2x |
version = "1.0.0", |
| 672 | 2x |
src = c(file = path), |
| 673 | 2x |
script = "echarts-contour.js") |
| 674 | ||
| 675 | 2x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 676 | ||
| 677 | 2x |
e |
| 678 |
} |
|
| 679 | ||
| 680 |
#' Line range chart |
|
| 681 |
#' |
|
| 682 |
#' Draw a line range area plot. |
|
| 683 |
#' |
|
| 684 |
#' @inheritParams e_band2_ |
|
| 685 |
#' @param lineStyle properties of the border lines |
|
| 686 |
#' @param areaStyle properties of the area between the lines |
|
| 687 |
#' |
|
| 688 |
#' @examples |
|
| 689 |
#' |
|
| 690 |
#' df <- iris |> |
|
| 691 |
#' dplyr::group_by(Species) |> |
|
| 692 |
#' dplyr::summarise(lower = min(Sepal.Length), |
|
| 693 |
#' upper = max(Sepal.Length)) |
|
| 694 |
#' df |> |
|
| 695 |
#' e_chart(Species) |> |
|
| 696 |
#' e_lineRange(lower = lower, upper = upper) |
|
| 697 |
#' @seealso \href{https://github.com/apache/echarts-custom-series/tree/main/custom-series/lineRange}{official documentation}
|
|
| 698 |
#' |
|
| 699 |
#' @rdname e_lineRange |
|
| 700 |
#' @export |
|
| 701 |
e_lineRange <- function(e, |
|
| 702 |
lower, |
|
| 703 |
upper, |
|
| 704 |
name = "lineRange", |
|
| 705 |
legend = TRUE, |
|
| 706 |
y_index = 0, |
|
| 707 |
x_index = 0, |
|
| 708 |
lineStyle = list(opacity = 0.3, color = "#000", width=1), |
|
| 709 |
areaStyle = list(opacity = 0.3, color = "#032", width=1), |
|
| 710 |
...){
|
|
| 711 | ||
| 712 | 5x |
if (missing(e)) {
|
| 713 | 1x |
stop("must pass e", call. = FALSE)
|
| 714 |
} |
|
| 715 | 4x |
if (missing(lower) || missing(upper)) {
|
| 716 | 1x |
stop("must pass lower, or upper", call. = FALSE)
|
| 717 |
} |
|
| 718 | ||
| 719 | 3x |
if (e$x$tl) {
|
| 720 | 1x |
stop("timeline not supported", call. = FALSE)
|
| 721 |
} |
|
| 722 | ||
| 723 |
# Extract the color from areaStyle to use in legend |
|
| 724 | 2x |
legend_color <- if (!is.null(areaStyle$color)) areaStyle$color else "#032" |
| 725 | ||
| 726 | 2x |
for (i in seq_along(e$x$data)) {
|
| 727 | 2x |
vector <- .build_data2( |
| 728 | 2x |
e$x$data[[i]], |
| 729 | 2x |
e$x$mapping$x, |
| 730 | 2x |
deparse(substitute(lower)), |
| 731 | 2x |
deparse(substitute(upper)) |
| 732 |
) |
|
| 733 | 2x |
e_serie <- list(data = vector) |
| 734 | ||
| 735 | 2x |
if (y_index != 0) {
|
| 736 | 1x |
e <- .set_y_axis(e, deparse(substitute(upper)), y_index, i) |
| 737 |
} |
|
| 738 | 2x |
if (x_index != 0) {
|
| 739 | 1x |
e <- .set_x_axis(e, x_index, i) |
| 740 |
} |
|
| 741 | ||
| 742 | 2x |
opts <- list( |
| 743 | 2x |
type = "custom", |
| 744 | 2x |
name = name, |
| 745 | 2x |
renderItem = 'lineRange', |
| 746 | 2x |
encode = list(x= 0, |
| 747 | 2x |
y = c(1,2), |
| 748 | 2x |
tooltip = c(1,2)), |
| 749 | 2x |
data = vector, |
| 750 | 2x |
itemPayload = list( |
| 751 | 2x |
areaStyle = areaStyle, |
| 752 | 2x |
lineStyle = lineStyle |
| 753 |
), |
|
| 754 |
... |
|
| 755 |
) |
|
| 756 | ||
| 757 | 2x |
e_serie <- append(opts, e_serie) # data after renderItem, data used for Y-sizing only |
| 758 | ||
| 759 | 2x |
if (isTRUE(legend)) {
|
| 760 | 2x |
legend_entry <- list( |
| 761 | 2x |
name = name, |
| 762 | 2x |
icon = "rect", # Use rectangle icon |
| 763 | 2x |
itemStyle = list(color = legend_color) |
| 764 |
) |
|
| 765 | 2x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(legend_entry)) |
| 766 |
} |
|
| 767 | 2x |
e$x$opts$series <- append(e$x$opts$series, list(e_serie)) |
| 768 | ||
| 769 |
} |
|
| 770 | 2x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 771 | 2x |
dep <- htmltools::htmlDependency( |
| 772 | 2x |
name = "echarts-lineRange", |
| 773 | 2x |
version = "1.0.0", |
| 774 | 2x |
src = c(file = path), |
| 775 | 2x |
script = "echarts-lineRange.js") |
| 776 | ||
| 777 | 2x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 778 | ||
| 779 | 2x |
e |
| 780 |
} |
|
| 781 | ||
| 782 |
#' Stage chart |
|
| 783 |
#' |
|
| 784 |
#' Draw a stage plot. |
|
| 785 |
#' |
|
| 786 |
#' @inheritParams e_band2_ |
|
| 787 |
#' @param start column for start of stage on x axis |
|
| 788 |
#' @param end column for end of stage on x axis |
|
| 789 |
#' @param stage column for stage on y axis |
|
| 790 |
#' @param borderRadius The border radius of the stage. |
|
| 791 |
#' @param verticalMargin The vertical margin of the bars. |
|
| 792 |
#' @param minHorizontalSize The minimum width of the bars. |
|
| 793 |
#' @param envelope The envelope of the stage. |
|
| 794 |
#' @param axisLabel The style of the axis label. |
|
| 795 |
#' |
|
| 796 |
#' @export |
|
| 797 |
#' |
|
| 798 |
#' @examples |
|
| 799 |
#' |
|
| 800 |
#' df <- data.frame( |
|
| 801 |
#' start = as.POSIXct(c( |
|
| 802 |
#' "2024-09-07 06:12", "2024-09-07 06:15", "2024-09-07 05:45", |
|
| 803 |
#' "2024-09-07 04:57", "2024-09-07 06:12", "2024-09-07 06:18" |
|
| 804 |
#' )), |
|
| 805 |
#' |
|
| 806 |
#' end = as.POSIXct(c( |
|
| 807 |
#' "2024-09-07 06:12", "2024-09-07 06:18", "2024-09-07 06:12", |
|
| 808 |
#' "2024-09-07 05:45", "2024-09-07 06:15", "2024-09-07 07:37" |
|
| 809 |
#' )), |
|
| 810 |
#' |
|
| 811 |
#' stage = c( |
|
| 812 |
#' "Awake", "Awake", "REM", |
|
| 813 |
#' "Core", "Core", "Deep" |
|
| 814 |
#' ), |
|
| 815 |
#' stringsAsFactors = FALSE |
|
| 816 |
#' ) |
|
| 817 |
#' stage_order = c( "Deep", "Core","REM", "Awake") |
|
| 818 |
#' |
|
| 819 |
#' df |> |
|
| 820 |
#' e_charts() |> |
|
| 821 |
#' e_stage(start = start, |
|
| 822 |
#' end = end, |
|
| 823 |
#' stage = stage) |> |
|
| 824 |
#' e_x_axis(type = 'time') |> |
|
| 825 |
#' e_y_axis(type = 'category', data = stage_order) |
|
| 826 |
#' |
|
| 827 |
#'@seealso \href{https://github.com/apache/echarts-custom-series/tree/main/custom-series/stage}{official documentation}
|
|
| 828 |
#' |
|
| 829 |
#' @rdname e_stage |
|
| 830 |
#' @export |
|
| 831 |
e_stage <- function(e, |
|
| 832 |
start, |
|
| 833 |
end, |
|
| 834 |
stage, |
|
| 835 |
legend = TRUE, |
|
| 836 |
name = "stage", |
|
| 837 |
borderRadius = 8, |
|
| 838 |
verticalMargin = 10, |
|
| 839 |
minHorizontalSize = 3, |
|
| 840 |
envelope = list(show = TRUE, color = "#888", opacity = 0.25, externalRadius = 8 ), |
|
| 841 |
axisLabel = list(formatter = NULL, color = "#8A8A8A"), |
|
| 842 |
...){
|
|
| 843 | ||
| 844 | 3x |
if (missing(e)) {
|
| 845 | 1x |
stop("must pass e", call. = FALSE)
|
| 846 |
} |
|
| 847 | ||
| 848 | 2x |
data <- e$x$data |
| 849 | ||
| 850 | 2x |
for (i in seq_along(data)) {
|
| 851 | ||
| 852 | 5x |
df <- data[[i]] |
| 853 | 5x |
start_col <- deparse(substitute(start)) |
| 854 | 5x |
end_col <- deparse(substitute(end)) |
| 855 | 5x |
stage_col <- deparse(substitute(stage)) |
| 856 | ||
| 857 | 5x |
vector <- lapply(seq_len(nrow(df)), function(j) {
|
| 858 | 12x |
list( |
| 859 | 12x |
df[[start_col]][[j]], |
| 860 | 12x |
df[[end_col]][[j]], |
| 861 | 12x |
df[[stage_col]][[j]] |
| 862 |
) |
|
| 863 |
}) |
|
| 864 | ||
| 865 | 5x |
e_serie <- list(data = vector) |
| 866 | ||
| 867 | 5x |
if (!e$x$tl) {
|
| 868 | 1x |
opts <- list( |
| 869 | 1x |
type = "custom", |
| 870 | 1x |
renderItem = 'stage', |
| 871 | 1x |
colorBy = 'data', |
| 872 | 1x |
encode = list( |
| 873 | 1x |
x = c(0, 1), |
| 874 | 1x |
y = 2, |
| 875 | 1x |
tooltip = c(0, 1)), |
| 876 | 1x |
name = name, |
| 877 | 1x |
itemPayload = list( |
| 878 | 1x |
borderRadius = borderRadius, |
| 879 | 1x |
verticalMargin = verticalMargin, |
| 880 | 1x |
minHorizontalSize = minHorizontalSize, |
| 881 | 1x |
envelope = envelope, |
| 882 | 1x |
axisLabel = axisLabel |
| 883 |
), |
|
| 884 |
... |
|
| 885 |
) |
|
| 886 | 1x |
e$x$opts$series <- append(e$x$opts$series, list(opts)) |
| 887 | ||
| 888 | 1x |
if (isTRUE(legend)) {
|
| 889 | 1x |
e$x$opts$legend$data <- append(e$x$opts$legend$data, list(name)) |
| 890 |
} |
|
| 891 | ||
| 892 | 1x |
e$x$opts$dataset <- list(source = vector, sourceHeader = FALSE) |
| 893 | ||
| 894 |
} |
|
| 895 |
else {
|
|
| 896 | 4x |
if (isTRUE(legend)) {
|
| 897 | 4x |
e$x$opts$legend$data <- append( |
| 898 | 4x |
e$x$opts$legend$data, |
| 899 | 4x |
list(name) |
| 900 |
) |
|
| 901 |
} |
|
| 902 | 4x |
e$x$opts$options[[i]]$series <- append( |
| 903 | 4x |
e$x$opts$options[[i]]$series, |
| 904 | 4x |
list(e_serie) |
| 905 |
) |
|
| 906 |
} |
|
| 907 |
} |
|
| 908 | 2x |
if (isTRUE(e$x$tl)) {
|
| 909 | ||
| 910 | 1x |
series_opts <- list( |
| 911 | 1x |
type = "custom", |
| 912 | 1x |
renderItem = 'stage', |
| 913 | 1x |
colorBy = 'data', |
| 914 | 1x |
encode = list( |
| 915 | 1x |
x = c(0, 1), |
| 916 | 1x |
y = 2, |
| 917 | 1x |
tooltip = c(0, 1)), |
| 918 | 1x |
name = name, |
| 919 | 1x |
itemPayload = list( |
| 920 | 1x |
borderRadius = borderRadius, |
| 921 | 1x |
verticalMargin = verticalMargin, |
| 922 | 1x |
minHorizontalSize = minHorizontalSize, |
| 923 | 1x |
envelope = envelope, |
| 924 | 1x |
axisLabel = axisLabel |
| 925 |
), |
|
| 926 |
... |
|
| 927 |
) |
|
| 928 | ||
| 929 | 1x |
if (isTRUE(legend)) {
|
| 930 | 1x |
e$x$opts$baseOption$legend$data <- append(e$x$opts$baseOption$legend$data, list(name)) |
| 931 |
} |
|
| 932 | 1x |
e$x$opts$baseOption$series <- append( |
| 933 | 1x |
e$x$opts$baseOption$series, |
| 934 | 1x |
list(series_opts) |
| 935 |
) |
|
| 936 |
} |
|
| 937 | ||
| 938 | 2x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 939 | 2x |
dep <- htmltools::htmlDependency( |
| 940 | 2x |
name = "echarts-stage", |
| 941 | 2x |
version = "1.0.0", |
| 942 | 2x |
src = c(file = path), |
| 943 | 2x |
script = "echarts-stage.js") |
| 944 | ||
| 945 | 2x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 946 | ||
| 947 | 2x |
e |
| 948 |
} |
| 1 |
#' Mark point |
|
| 2 |
#' |
|
| 3 |
#' Mark points and lines. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param serie Serie or vector of series to mark on, defaults to all series. |
|
| 7 |
#' @param data Placement of point, line or area. |
|
| 8 |
#' @param title A convenience argument to easily set label, see details. |
|
| 9 |
#' @param title_position Position of title. |
|
| 10 |
#' |
|
| 11 |
#' @details To set a label you need to either use the \code{title}
|
|
| 12 |
#' argument or pass a list specifying the label formatter. |
|
| 13 |
#' \code{label = list(formatter = "label")}. The former is more convenient
|
|
| 14 |
#' but more limited, e.g.: you cannot specify the placement of the label. |
|
| 15 |
#' When the \code{e_mark} series function is used with \code{e_timeline} at the
|
|
| 16 |
#' same time, if the number of marks provided does not match the series, the |
|
| 17 |
#' mark information will follow the setting of the previous frame. |
|
| 18 |
#' |
|
| 19 |
#' @examples |
|
| 20 |
#' max <- list( |
|
| 21 |
#' name = "Max", |
|
| 22 |
#' type = "max" |
|
| 23 |
#' ) |
|
| 24 |
#' |
|
| 25 |
#' min <- list( |
|
| 26 |
#' name = "Min", |
|
| 27 |
#' type = "min" |
|
| 28 |
#' ) |
|
| 29 |
#' |
|
| 30 |
#' avg <- list( |
|
| 31 |
#' type = "average", |
|
| 32 |
#' name = "AVG" |
|
| 33 |
#' ) |
|
| 34 |
#' |
|
| 35 |
#' mtcars |> |
|
| 36 |
#' e_charts(mpg) |> |
|
| 37 |
#' e_line(wt) |> |
|
| 38 |
#' e_line(drat) |> |
|
| 39 |
#' e_line(cyl) |> |
|
| 40 |
#' e_mark_point("wt", data = max) |>
|
|
| 41 |
#' e_mark_point(c("cyl", "drat"), data = min) |>
|
|
| 42 |
#' e_mark_line(data = avg) |> # applies to all |
|
| 43 |
#' e_mark_area( |
|
| 44 |
#' serie = "wt", |
|
| 45 |
#' data = list( |
|
| 46 |
#' list(xAxis = "min", yAxis = "min"), |
|
| 47 |
#' list(xAxis = "max", yAxis = "max") |
|
| 48 |
#' ) |
|
| 49 |
#' ) |
|
| 50 |
#' |
|
| 51 |
#' # Serie options, since the mark of "virginica" is not set, the mark setting |
|
| 52 |
#' # of the previous frame is used |
|
| 53 |
#' iris |> |
|
| 54 |
#' group_by(Species) |> |
|
| 55 |
#' e_charts(Sepal.Length, timeline = TRUE) |> |
|
| 56 |
#' e_line(Sepal.Width) |> |
|
| 57 |
#' e_timeline_serie( |
|
| 58 |
#' title = list( |
|
| 59 |
#' list(text = "setosa"), |
|
| 60 |
#' list(text = "versicolor"), |
|
| 61 |
#' list(text = "virginica") |
|
| 62 |
#' ) |
|
| 63 |
#' ) |> |
|
| 64 |
#' e_mark_area( |
|
| 65 |
#' serie = "setosa", |
|
| 66 |
#' data = list( |
|
| 67 |
#' list(xAxis = 4, yAxis = 2), |
|
| 68 |
#' list(xAxis = 6, yAxis = 4.5) |
|
| 69 |
#' ), |
|
| 70 |
#' itemStyle = list(color = "lightgreen") |
|
| 71 |
#' ) |> |
|
| 72 |
#' e_mark_area( |
|
| 73 |
#' serie = "versicolor", |
|
| 74 |
#' data = list( |
|
| 75 |
#' list(xAxis = 4.5), |
|
| 76 |
#' list(xAxis = 7) |
|
| 77 |
#' ), |
|
| 78 |
#' itemStyle = list(color = "lightblue") |
|
| 79 |
#' ) |
|
| 80 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-line.markPoint}{Additional point arguments},
|
|
| 81 |
#' \href{https://echarts.apache.org/en/option.html#series-line.markLine}{Additional line arguments}
|
|
| 82 |
#' |
|
| 83 |
#' @rdname mark |
|
| 84 |
#' @export |
|
| 85 |
e_mark_point <- function(e, serie = NULL, data = NULL, ..., title = NULL, title_position = NULL) {
|
|
| 86 | 4x |
if (missing(e)) {
|
| 87 | 1x |
stop("must pass e", call. = FALSE)
|
| 88 |
} |
|
| 89 | ||
| 90 | 3x |
if (is.null(serie)) {
|
| 91 | ! |
index <- seq_along(if (e$x$tl) e$x$opts$options else e$x$opts$series) |
| 92 | 3x |
} else if (e$x$tl){
|
| 93 | 2x |
index <- 1 |
| 94 |
} else {
|
|
| 95 | 1x |
index <- .get_index(e, serie) |
| 96 |
} |
|
| 97 | ||
| 98 | 3x |
for (i in index) {
|
| 99 | 3x |
point <- list(...) |
| 100 | ||
| 101 | 3x |
if (!is.null(data)) {
|
| 102 | 3x |
point$data <- list(data) |
| 103 |
} |
|
| 104 | ||
| 105 | 3x |
if (!is.null(title) && !is.null(data)) {
|
| 106 | 1x |
point$data[[1]]$label <- list(formatter = title, position = title_position) |
| 107 |
} |
|
| 108 | ||
| 109 | 3x |
if (e$x$tl) {
|
| 110 | 2x |
for(j in seq_along(e$x$opts$options[[i]]$series)){
|
| 111 | 2x |
if (is.null(e$x$opts$options[[i]]$series[[j]]$markPoint)) {
|
| 112 | 1x |
e$x$opts$options[[i]]$series[[j]]$markPoint <- append(e$x$opts$options[[i]]$series[[j]]$markPoint, point) |
| 113 |
} else {
|
|
| 114 | 1x |
e$x$opts$options[[i]]$series[[j]]$markPoint$data <- append(e$x$opts$options[[i]]$series[[j]]$markPoint$data, point$data) |
| 115 |
} |
|
| 116 |
} |
|
| 117 |
} |
|
| 118 |
else {
|
|
| 119 | 1x |
if (is.null(e$x$opts$series[[i]]$markPoint)) {
|
| 120 | 1x |
e$x$opts$series[[i]]$markPoint <- append(e$x$opts$series[[i]]$markPoint, point) |
| 121 |
} else {
|
|
| 122 | ! |
e$x$opts$series[[i]]$markPoint$data <- append(e$x$opts$series[[i]]$markPoint$data, point$data) |
| 123 |
} |
|
| 124 |
} |
|
| 125 |
} |
|
| 126 | ||
| 127 | 3x |
e |
| 128 |
} |
|
| 129 | ||
| 130 |
#' @rdname mark |
|
| 131 |
#' @export |
|
| 132 |
e_mark_line <- function(e, serie = NULL, data = NULL, ..., title = NULL, title_position = NULL) {
|
|
| 133 | 4x |
if (missing(e)) {
|
| 134 | 1x |
stop("must pass e", call. = FALSE)
|
| 135 |
} |
|
| 136 | ||
| 137 | 3x |
if (is.null(serie)) {
|
| 138 | 2x |
index <- seq_along(if (e$x$tl) e$x$opts$options else e$x$opts$series) |
| 139 |
} else {
|
|
| 140 | 1x |
index <- .get_index(e, serie) |
| 141 |
} |
|
| 142 | ||
| 143 | 3x |
for (i in index) {
|
| 144 | 5x |
point <- list(...) |
| 145 | ||
| 146 | 5x |
if (!is.null(data)) {
|
| 147 | 5x |
point$data <- list(data) |
| 148 |
} |
|
| 149 | ||
| 150 | 5x |
if (!is.null(title) && !is.null(data)) {
|
| 151 | ! |
point$data[[1]]$label <- list(formatter = title, position = title_position) |
| 152 |
} |
|
| 153 | ||
| 154 | 5x |
if (e$x$tl) {
|
| 155 | 4x |
for(j in seq_along(e$x$opts$options[[i]]$series)){
|
| 156 | 4x |
if (is.null(e$x$opts$options[[i]]$series[[j]]$markLine)) {
|
| 157 | 2x |
e$x$opts$options[[i]]$series[[j]]$markLine <- append(e$x$opts$options[[i]]$series[[j]]$markLine, point) |
| 158 |
} else {
|
|
| 159 | 2x |
e$x$opts$options[[i]]$series[[j]]$markLine$data <- append(e$x$opts$options[[i]]$series[[j]]$markLine$data, point$data) |
| 160 |
} |
|
| 161 |
} |
|
| 162 |
} |
|
| 163 |
else {
|
|
| 164 | 1x |
if (is.null(e$x$opts$series[[i]]$markLine)) {
|
| 165 | 1x |
e$x$opts$series[[i]]$markLine <- append(e$x$opts$series[[i]]$markLine, point) |
| 166 |
} else {
|
|
| 167 | ! |
e$x$opts$series[[i]]$markLine$data <- append(e$x$opts$series[[i]]$markLine$data, point$data) |
| 168 |
} |
|
| 169 |
} |
|
| 170 |
} |
|
| 171 | ||
| 172 | 3x |
e |
| 173 |
} |
|
| 174 | ||
| 175 |
#' @rdname mark |
|
| 176 |
#' @export |
|
| 177 |
e_mark_area <- function(e, serie = NULL, data = NULL, ..., title = NULL, title_position = NULL) {
|
|
| 178 | 3x |
if (missing(e)) {
|
| 179 | 1x |
stop("must pass e", call. = FALSE)
|
| 180 |
} |
|
| 181 | ||
| 182 | 2x |
if (is.null(serie)) {
|
| 183 | 1x |
index <- seq_along(if (e$x$tl) e$x$opts$options else e$x$opts$series) |
| 184 |
} else {
|
|
| 185 | 1x |
index <- .get_index(e, serie) |
| 186 |
} |
|
| 187 | ||
| 188 | 2x |
for (i in index) {
|
| 189 | 3x |
point <- list(...) |
| 190 | ||
| 191 | 3x |
if (!is.null(data)) {
|
| 192 | 3x |
point$data <- list(data) |
| 193 |
} |
|
| 194 | ||
| 195 | 3x |
if (!is.null(title) && !is.null(data)) {
|
| 196 | ! |
point$data[[1]]$label <- list(formatter = title, position = title_position) |
| 197 |
} |
|
| 198 | ||
| 199 | 3x |
if (e$x$tl) {
|
| 200 | 2x |
for(j in seq_along(e$x$opts$options[[i]]$series)){
|
| 201 | 2x |
if (is.null(e$x$opts$options[[i]]$series[[j]]$markArea)) {
|
| 202 | 2x |
e$x$opts$options[[i]]$series[[j]]$markArea <- append(e$x$opts$options[[i]]$series[[j]]$markArea, point) |
| 203 |
} else {
|
|
| 204 | ! |
e$x$opts$options[[i]]$series[[j]]$markArea$data <- append(e$x$opts$options[[i]]$series[[j]]$markArea$data, point$data) |
| 205 |
} |
|
| 206 |
} |
|
| 207 |
} |
|
| 208 |
else {
|
|
| 209 | 1x |
if (is.null(e$x$opts$series[[i]]$markArea)) {
|
| 210 | 1x |
e$x$opts$series[[i]]$markArea <- append(e$x$opts$series[[i]]$markArea, point) |
| 211 |
} else {
|
|
| 212 | ! |
e$x$opts$series[[i]]$markArea$data <- append(e$x$opts$series[[i]]$markArea$data, point$data) |
| 213 |
} |
|
| 214 |
} |
|
| 215 |
} |
|
| 216 | ||
| 217 | 2x |
e |
| 218 |
} |
|
| 219 | ||
| 220 |
#' Mark |
|
| 221 |
#' |
|
| 222 |
#' Mark points, lines, and areas with a proxy ([echarts4rProxy()]). |
|
| 223 |
#' |
|
| 224 |
#' @inheritParams e_bar |
|
| 225 |
#' @param type Type of mark: 'point','line' or 'area', defaults to 'point'. |
|
| 226 |
#' @param serie_index Single index of serie to mark on, defaults to 1. |
|
| 227 |
#' Proxy doesn't know series' names, so it only uses index. |
|
| 228 |
#' @param data Location of point, line or area, defaults to NULL. |
|
| 229 |
#' |
|
| 230 |
#' @details Allows the three type of marks to work with [echarts4rProxy()] |
|
| 231 |
#' |
|
| 232 |
#' @examples |
|
| 233 |
#' library(shiny) |
|
| 234 |
#' library(dplyr) |
|
| 235 |
#' |
|
| 236 |
#' ui <- fluidPage( |
|
| 237 |
#' fluidRow( |
|
| 238 |
#' column(3, actionButton("pxy", "Marks")),
|
|
| 239 |
#' column( |
|
| 240 |
#' 3, |
|
| 241 |
#' checkboxInput("tln", "Timeline", value = FALSE)
|
|
| 242 |
#' ) |
|
| 243 |
#' ), |
|
| 244 |
#' echarts4rOutput("plot")
|
|
| 245 |
#' ) |
|
| 246 |
#' |
|
| 247 |
#' server <- function(input, output) {
|
|
| 248 |
#' data(EuStockMarkets) |
|
| 249 |
#' |
|
| 250 |
#' bb <- as.data.frame(EuStockMarkets) |> |
|
| 251 |
#' slice_head(n = 150) |> |
|
| 252 |
#' mutate(day = 1:n()) |
|
| 253 |
#' |
|
| 254 |
#' output$plot <- renderEcharts4r({
|
|
| 255 |
#' react() |
|
| 256 |
#' }) |
|
| 257 |
#' |
|
| 258 |
#' observeEvent(input$pxy, {
|
|
| 259 |
#' echarts4rProxy("plot", data = NULL) |>
|
|
| 260 |
#' e_mark_p( |
|
| 261 |
#' type = "line", |
|
| 262 |
#' serie_index = 1, |
|
| 263 |
#' data = list(type = "average"), |
|
| 264 |
#' lineStyle = list(type = "dashed", color = "cyan") |
|
| 265 |
#' ) |> |
|
| 266 |
#' e_mark_p( |
|
| 267 |
#' serie_index = 2, |
|
| 268 |
#' data = list( |
|
| 269 |
#' xAxis = bb$day[60], |
|
| 270 |
#' yAxis = bb$SMI[60], |
|
| 271 |
#' value = "pnt" |
|
| 272 |
#' ) |
|
| 273 |
#' ) |> |
|
| 274 |
#' e_mark_p( |
|
| 275 |
#' type = "line", |
|
| 276 |
#' serie_index = 2, |
|
| 277 |
#' data = list( |
|
| 278 |
#' list(xAxis = bb$day[10], yAxis = bb$SMI[10]), |
|
| 279 |
#' list(xAxis = bb$day[37], yAxis = bb$SMI[37]) |
|
| 280 |
#' ), |
|
| 281 |
#' lineStyle = list(type = "solid", color = "yellow") |
|
| 282 |
#' ) |> |
|
| 283 |
#' e_mark_p( |
|
| 284 |
#' type = "area", |
|
| 285 |
#' serie_index = 1, |
|
| 286 |
#' data = list( |
|
| 287 |
#' list(xAxis = bb$day[95]), |
|
| 288 |
#' list(xAxis = bb$day[105]) |
|
| 289 |
#' ), |
|
| 290 |
#' itemStyle = list(color = "lightblue"), |
|
| 291 |
#' label = list(formatter = "X-area", position = "middle") |
|
| 292 |
#' ) |> |
|
| 293 |
#' e_merge() |
|
| 294 |
#' }) |
|
| 295 |
#' |
|
| 296 |
#' react <- eventReactive(input$tln, {
|
|
| 297 |
#' tmp <- bb |
|
| 298 |
#' if (input$tln) tmp <- tmp |> group_by(day < 75) |
|
| 299 |
#' |
|
| 300 |
#' tmp |> |
|
| 301 |
#' e_charts( |
|
| 302 |
#' day, |
|
| 303 |
#' backgroundColor = "#181818", |
|
| 304 |
#' legend = list(textStyle = list(color = "#aaa")), |
|
| 305 |
#' timeline = input$tln |
|
| 306 |
#' ) |> |
|
| 307 |
#' e_y_axis(scale = TRUE, axisLabel = list(color = "#aaa")) |> |
|
| 308 |
#' e_line(CAC, symbol = "none", color = "#ff33b8") |> |
|
| 309 |
#' e_line(SMI, symbol = "none", color = "green") |
|
| 310 |
#' }) |
|
| 311 |
#' } |
|
| 312 |
#' if (interactive()) {
|
|
| 313 |
#' shinyApp(ui, server) |
|
| 314 |
#' } |
|
| 315 |
#' @name e_mark_p |
|
| 316 |
#' @export |
|
| 317 | 2x |
e_mark_p <- function(e, type, serie_index, data, ...) UseMethod("e_mark_p")
|
| 318 | ||
| 319 |
#' @method e_mark_p echarts4r |
|
| 320 |
#' @export |
|
| 321 |
e_mark_p.echarts4r <- function(e, type = "point", serie_index = NULL, data = NULL, ...) {
|
|
| 322 | 1x |
if (missing(e)) stop("must pass e", call. = FALSE)
|
| 323 | 2x |
e_mark_p_(e, type, serie_index, data, ...) |
| 324 |
} |
|
| 325 | ||
| 326 |
#' @method e_mark_p echarts4rProxy |
|
| 327 |
#' @export |
|
| 328 |
e_mark_p.echarts4rProxy <- function(e, type = "point", serie_index = NULL, data = NULL, ...) {
|
|
| 329 | ! |
e$chart <- e_mark_p_(e$chart, type, serie_index, data, ...) |
| 330 | ! |
return(e) |
| 331 |
} |
|
| 332 | ||
| 333 |
#' @rdname e_mark_p |
|
| 334 |
#' @export |
|
| 335 |
e_mark_p_ <- function(e, type, serie_index, data = NULL, ...) {
|
|
| 336 | 1x |
if (missing(e)) stop("must pass e", call. = FALSE)
|
| 337 | 1x |
if (missing(type)) stop("must pass type", call. = FALSE)
|
| 338 | 3x |
mtype <- type |
| 339 | ||
| 340 | 1x |
if (!mtype %in% c("point", "line", "area")) stop("type must be line,point or area", call. = FALSE)
|
| 341 | 2x |
if (!startsWith(mtype, "mark")) {
|
| 342 | 2x |
mtype <- switch(type, "point" = "markPoint", "line" = "markLine", "area" = "markArea") |
| 343 |
} |
|
| 344 | ||
| 345 | 2x |
index <- ifelse(is.null(serie_index), 1, as.numeric(serie_index)) |
| 346 | ||
| 347 | 2x |
for (i in 1:index) {
|
| 348 | 2x |
if (length(e$x$opts$series) < i) {
|
| 349 | 1x |
e$x$opts$series[[i]] <- list() |
| 350 |
} # init |
|
| 351 | ! |
if (i < index) next |
| 352 | ||
| 353 | 2x |
point <- list(...) |
| 354 | ||
| 355 | 2x |
if (!is.null(data)) point$data <- list(data) |
| 356 | ||
| 357 | 2x |
suppressWarnings( # eval generates redundant warnings |
| 358 | 2x |
if (e$x$tl) {
|
| 359 | 1x |
if (mtype %in% names(e$x$opts$options[[i]]$series[[1]])) {
|
| 360 | ! |
eval(parse(text = paste0("e$x$opts$options[[i]]$series[[1]]$", mtype, "$data <- append(e$x$opts$options[[i]]$series[[1]]$", mtype, "$data, point$data)")))
|
| 361 |
} else {
|
|
| 362 | 1x |
eval(parse(text = paste0("e$x$opts$options[[i]]$series[[1]]$", mtype, " <- point")))
|
| 363 |
} |
|
| 364 |
} |
|
| 365 |
else {
|
|
| 366 | 1x |
if (mtype %in% names(e$x$opts$series[[i]])) {
|
| 367 | ! |
eval(parse(text = paste0("e$x$opts$series[[i]]$", mtype, "$data <- append(e$x$opts$series[[i]]$", mtype, "$data, point$data)")))
|
| 368 |
} else {
|
|
| 369 | 1x |
eval(parse(text = paste0("e$x$opts$series[[i]]$", mtype, " <- point")))
|
| 370 |
} |
|
| 371 |
} |
|
| 372 |
) |
|
| 373 |
} |
|
| 374 | 2x |
e |
| 375 |
} |
| 1 |
#' Graphic |
|
| 2 |
#' |
|
| 3 |
#' Low level API to define graphic elements. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' |
|
| 7 |
#' @section Functions: |
|
| 8 |
#' \itemize{
|
|
| 9 |
#' \item{\code{e_graphic_g} to initialise graphics, entirely optional.}
|
|
| 10 |
#' \item{\code{e_group_g} to create group, the children of which will share attributes.}
|
|
| 11 |
#' \item{\code{e_image_g} to a png or jpg image.}
|
|
| 12 |
#' \item{\code{e_text_g} to add text.}
|
|
| 13 |
#' \item{\code{e_rect_g} to add a rectangle.}
|
|
| 14 |
#' \item{\code{e_circle_g} to add a circle.}
|
|
| 15 |
#' \item{\code{e_ring_g} to add a ring.}
|
|
| 16 |
#' \item{\code{e_sector_g}}
|
|
| 17 |
#' \item{\code{e_arc_g} to create an arc.}
|
|
| 18 |
#' \item{\code{e_polygon_g} to create a polygon.}
|
|
| 19 |
#' \item{\code{e_polyline_g} to create a polyline.}
|
|
| 20 |
#' \item{\code{e_line_g} to draw a line.}
|
|
| 21 |
#' \item{\code{e_bezier_curve_g} to draw a quadratic bezier curve or cubic bezier curve.}
|
|
| 22 |
#' } |
|
| 23 |
#' |
|
| 24 |
#' @note Some elements, i.e.: \code{e_image_g} may not display in the RStudio
|
|
| 25 |
#' browwser but will work fine in your browser, R markdown documents and Shiny |
|
| 26 |
#' applications. |
|
| 27 |
#' |
|
| 28 |
#' @examples |
|
| 29 |
#' # may not work in RStudio viewer |
|
| 30 |
#' # Open in browser |
|
| 31 |
#' cars |> |
|
| 32 |
#' e_charts(speed) |> |
|
| 33 |
#' e_scatter(dist) |> |
|
| 34 |
#' e_image_g( |
|
| 35 |
#' right = 20, |
|
| 36 |
#' top = 20, |
|
| 37 |
#' z = -999, |
|
| 38 |
#' style = list( |
|
| 39 |
#' image = "https://www.r-project.org/logo/Rlogo.png", |
|
| 40 |
#' width = 150, |
|
| 41 |
#' height = 150, |
|
| 42 |
#' opacity = .6 |
|
| 43 |
#' ) |
|
| 44 |
#' ) |
|
| 45 |
#' |
|
| 46 |
#' anno_text <- |
|
| 47 |
#' 'xAxis represents miles per gallon, yAxis represents weight in tonnes.' |
|
| 48 |
#' # Position does change depending on window size. |
|
| 49 |
#' mtcars |> |
|
| 50 |
#' e_charts(mpg) |> |
|
| 51 |
#' e_line(wt) |> |
|
| 52 |
#' e_group_g( |
|
| 53 |
#' left = '18%', |
|
| 54 |
#' top = '55%', |
|
| 55 |
#' children = list( |
|
| 56 |
#' # Background rectangle |
|
| 57 |
#' list( |
|
| 58 |
#' type = 'rect', |
|
| 59 |
#' z = 100, |
|
| 60 |
#' left = 'center', |
|
| 61 |
#' top = 'middle', |
|
| 62 |
#' shape = list( |
|
| 63 |
#' width = 240, |
|
| 64 |
#' height = 70 |
|
| 65 |
#' ), |
|
| 66 |
#' style = list( |
|
| 67 |
#' fill = '#fff', |
|
| 68 |
#' stroke = '#555', |
|
| 69 |
#' lineWidth = 1, |
|
| 70 |
#' shadowBlur = 8, |
|
| 71 |
#' shadowOffsetX = 3, |
|
| 72 |
#' shadowOffsetY = 3, |
|
| 73 |
#' shadowColor = 'rgba(0,0,0,0.2)' |
|
| 74 |
#' ) |
|
| 75 |
#' ), |
|
| 76 |
#' # Text element |
|
| 77 |
#' list( |
|
| 78 |
#' type = 'text', |
|
| 79 |
#' z = 100, |
|
| 80 |
#' left = 'center', |
|
| 81 |
#' top = 'middle', |
|
| 82 |
#' style = list( |
|
| 83 |
#' fill = '#333', |
|
| 84 |
#' width = 220, |
|
| 85 |
#' overflow = 'break', |
|
| 86 |
#' text = anno_text, |
|
| 87 |
#' font = '14px Microsoft YaHei' |
|
| 88 |
#' ) |
|
| 89 |
#' ) |
|
| 90 |
#' ) |
|
| 91 |
#' ) |
|
| 92 |
#' #' @seealso \href{https://echarts.apache.org/en/option.html#graphic}{official documentation}
|
|
| 93 |
#' |
|
| 94 |
#' @rdname graphic |
|
| 95 |
#' @export |
|
| 96 |
e_graphic_g <- function(e, ...) {
|
|
| 97 | 3x |
if (missing(e)) {
|
| 98 | 1x |
stop("must pass e", call. = FALSE)
|
| 99 |
} |
|
| 100 | ||
| 101 | 2x |
if (!e$x$tl) {
|
| 102 | 1x |
e$x$opts$graphic <- list(...) |
| 103 |
} else {
|
|
| 104 | 1x |
e$x$opts$baseOption$graphic <- list(...) |
| 105 |
} |
|
| 106 | 2x |
e |
| 107 |
} |
|
| 108 | ||
| 109 |
#' @rdname graphic |
|
| 110 |
#' @export |
|
| 111 |
e_group_g <- function(e, ...) {
|
|
| 112 | 2x |
if (missing(e)) {
|
| 113 | 1x |
stop("must pass e", call. = FALSE)
|
| 114 |
} |
|
| 115 | ||
| 116 | 1x |
.e_graphic_elem(e, "group", ...) |
| 117 |
} |
|
| 118 | ||
| 119 |
#' @rdname graphic |
|
| 120 |
#' @export |
|
| 121 |
e_image_g <- function(e, ...) {
|
|
| 122 | 2x |
if (missing(e)) {
|
| 123 | 1x |
stop("must pass e", call. = FALSE)
|
| 124 |
} |
|
| 125 | ||
| 126 | 1x |
.e_graphic_elem(e, "image", ...) |
| 127 |
} |
|
| 128 | ||
| 129 |
#' @rdname graphic |
|
| 130 |
#' @export |
|
| 131 |
e_text_g <- function(e, ...) {
|
|
| 132 | 3x |
if (missing(e)) {
|
| 133 | 1x |
stop("must pass e", call. = FALSE)
|
| 134 |
} |
|
| 135 | ||
| 136 | 2x |
.e_graphic_elem(e, "text", ...) |
| 137 |
} |
|
| 138 | ||
| 139 |
#' @rdname graphic |
|
| 140 |
#' @export |
|
| 141 |
e_rect_g <- function(e, ...) {
|
|
| 142 | 2x |
if (missing(e)) {
|
| 143 | 1x |
stop("must pass e", call. = FALSE)
|
| 144 |
} |
|
| 145 | ||
| 146 | 1x |
.e_graphic_elem(e, "rect", ...) |
| 147 |
} |
|
| 148 | ||
| 149 |
#' @rdname graphic |
|
| 150 |
#' @export |
|
| 151 |
e_circle_g <- function(e, ...) {
|
|
| 152 | 2x |
if (missing(e)) {
|
| 153 | 1x |
stop("must pass e", call. = FALSE)
|
| 154 |
} |
|
| 155 | ||
| 156 | 1x |
.e_graphic_elem(e, "circle", ...) |
| 157 |
} |
|
| 158 | ||
| 159 |
#' @rdname graphic |
|
| 160 |
#' @export |
|
| 161 |
e_ring_g <- function(e, ...) {
|
|
| 162 | 2x |
if (missing(e)) {
|
| 163 | 1x |
stop("must pass e", call. = FALSE)
|
| 164 |
} |
|
| 165 | ||
| 166 | 1x |
.e_graphic_elem(e, "ring", ...) |
| 167 |
} |
|
| 168 | ||
| 169 |
#' @rdname graphic |
|
| 170 |
#' @export |
|
| 171 |
e_sector_g <- function(e, ...) {
|
|
| 172 | 2x |
if (missing(e)) {
|
| 173 | 1x |
stop("must pass e", call. = FALSE)
|
| 174 |
} |
|
| 175 | ||
| 176 | 1x |
.e_graphic_elem(e, "sector", ...) |
| 177 |
} |
|
| 178 | ||
| 179 |
#' @rdname graphic |
|
| 180 |
#' @export |
|
| 181 |
e_arc_g <- function(e, ...) {
|
|
| 182 | 2x |
if (missing(e)) {
|
| 183 | 1x |
stop("must pass e", call. = FALSE)
|
| 184 |
} |
|
| 185 | ||
| 186 | 1x |
.e_graphic_elem(e, "arc", ...) |
| 187 |
} |
|
| 188 | ||
| 189 |
#' @rdname graphic |
|
| 190 |
#' @export |
|
| 191 |
e_polygon_g <- function(e, ...) {
|
|
| 192 | 2x |
if (missing(e)) {
|
| 193 | 1x |
stop("must pass e", call. = FALSE)
|
| 194 |
} |
|
| 195 | ||
| 196 | 1x |
.e_graphic_elem(e, "polygon", ...) |
| 197 |
} |
|
| 198 | ||
| 199 |
#' @rdname graphic |
|
| 200 |
#' @export |
|
| 201 |
e_polyline_g <- function(e, ...) {
|
|
| 202 | 2x |
if (missing(e)) {
|
| 203 | 1x |
stop("must pass e", call. = FALSE)
|
| 204 |
} |
|
| 205 | ||
| 206 | 1x |
.e_graphic_elem(e, "polyline", ...) |
| 207 |
} |
|
| 208 | ||
| 209 |
#' @rdname graphic |
|
| 210 |
#' @export |
|
| 211 |
e_line_g <- function(e, ...) {
|
|
| 212 | 2x |
if (missing(e)) {
|
| 213 | 1x |
stop("must pass e", call. = FALSE)
|
| 214 |
} |
|
| 215 | ||
| 216 | 1x |
.e_graphic_elem(e, "line", ...) |
| 217 |
} |
|
| 218 | ||
| 219 |
#' @rdname graphic |
|
| 220 |
#' @export |
|
| 221 |
e_bezier_curve_g <- function(e, ...) {
|
|
| 222 | 2x |
if (missing(e)) {
|
| 223 | 1x |
stop("must pass e", call. = FALSE)
|
| 224 |
} |
|
| 225 | ||
| 226 | 1x |
.e_graphic_elem(e, "bezierCurve", ...) |
| 227 |
} |
|
| 228 | ||
| 229 |
#' Draft |
|
| 230 |
#' |
|
| 231 |
#' Add a draft watermark to your graph. |
|
| 232 |
#' |
|
| 233 |
#' @inheritParams e_bar |
|
| 234 |
#' @param text Text to display. |
|
| 235 |
#' @param size Font size of text. |
|
| 236 |
#' @param opacity,color Opacity and color of text. |
|
| 237 |
#' |
|
| 238 |
#' @examples |
|
| 239 |
#' cars |> |
|
| 240 |
#' e_charts(speed) |> |
|
| 241 |
#' e_scatter(dist) |> |
|
| 242 |
#' e_draft() |
|
| 243 |
#' @export |
|
| 244 |
e_draft <- function(e, text = "DRAFT", size = "120px", opacity = 0.4, color = "#d3d3d3") {
|
|
| 245 | 2x |
if (missing(e)) {
|
| 246 | 1x |
stop("must pass e", call. = FALSE)
|
| 247 |
} |
|
| 248 | 1x |
e |> |
| 249 | 1x |
e_text_g( |
| 250 | 1x |
left = "center", |
| 251 | 1x |
top = "center", |
| 252 | 1x |
z = -1000, |
| 253 | 1x |
style = list( |
| 254 | 1x |
fill = color, |
| 255 | 1x |
text = text, |
| 256 | 1x |
font = paste(size, '"STHeiti", sans-serif'), |
| 257 | 1x |
opacity = opacity |
| 258 |
) |
|
| 259 |
) |
|
| 260 |
} |
| 1 |
# group2series |
|
| 2 |
map_grps_ <- function(data, timeline = FALSE) {
|
|
| 3 | ||
| 4 | 613x |
row.names(data) <- NULL |
| 5 | ||
| 6 | 613x |
if (dplyr::is.grouped_df(data)) {
|
| 7 | 124x |
splat <- dplyr::group_split(data) |
| 8 | 124x |
keys <- dplyr::group_keys(data)[[1]] |
| 9 | 124x |
splat <- purrr::map(splat, as.data.frame) |
| 10 | 124x |
names(splat) <- keys |
| 11 | ||
| 12 | 124x |
data <- splat |
| 13 | 489x |
} else if (inherits(data, "grouped_tt")) {
|
| 14 | 1x |
splat <- split(data, factor(data[[attr(data, "groups")]])) |
| 15 | 1x |
keys <- names(splat) |
| 16 | 1x |
splat <- purrr::map(splat, as.data.frame) |
| 17 | 1x |
names(splat) <- keys |
| 18 | 1x |
data <- splat |
| 19 |
} else {
|
|
| 20 | 488x |
if (inherits(data, "data.frame")) {
|
| 21 | 484x |
data <- as.data.frame(data) |
| 22 |
} # force data frame |
|
| 23 | ||
| 24 | 488x |
data <- list(data) |
| 25 |
} |
|
| 26 | ||
| 27 |
# Remove row names and now row.names is just the index |
|
| 28 | 613x |
.no_nm <- function(x) {
|
| 29 | 937x |
row.names(x) <- NULL |
| 30 | 937x |
return(x) |
| 31 |
} |
|
| 32 | ||
| 33 | 613x |
data <- lapply(data, .no_nm) |
| 34 | ||
| 35 | 613x |
return(data) |
| 36 |
} |
| 1 |
#' Box |
|
| 2 |
#' |
|
| 3 |
#' Renders a data box in shiny. |
|
| 4 |
#' |
|
| 5 |
#' @param data A dataframe containing data to plot. |
|
| 6 |
#' @param x,y Bare column name of variables to draw. |
|
| 7 |
#' @param text,subtext Title and subtitle of box. |
|
| 8 |
#' @param type Chart type to draw. |
|
| 9 |
#' @param color Color of chart in box. |
|
| 10 |
#' @param text_color Color of text. |
|
| 11 |
#' @param background_color Color of box. |
|
| 12 |
#' @param step Step method, only used if \code{type = "step"}.
|
|
| 13 |
#' @param title_args Additional arguments to add to the title. |
|
| 14 |
#' @param tooltip Tooltip to use. |
|
| 15 |
#' @param ... Additional arguments to pass to the serie. |
|
| 16 |
#' |
|
| 17 |
#' @examples |
|
| 18 |
#' library(shiny) |
|
| 19 |
#' |
|
| 20 |
#' ui <- fluidPage( |
|
| 21 |
#' fluidRow( |
|
| 22 |
#' column(3, echarts4rBoxOutput("box1"))
|
|
| 23 |
#' ) |
|
| 24 |
#' ) |
|
| 25 |
#' |
|
| 26 |
#' server <- function(input, output) {
|
|
| 27 |
#' output$box1 <- renderEcharts4rBox({
|
|
| 28 |
#' echarts4rBox(cars, speed, dist, "Cars", type = "bar") |
|
| 29 |
#' }) |
|
| 30 |
#' } |
|
| 31 |
#' \dontrun{
|
|
| 32 |
#' shinyApp(ui, server) |
|
| 33 |
#' } |
|
| 34 |
#' @seealso \code{\link{renderEcharts4rBox}}, \code{\link{echarts4rBoxOutput}}
|
|
| 35 |
#' |
|
| 36 |
#' @export |
|
| 37 |
echarts4rBox <- function( |
|
| 38 |
data, |
|
| 39 |
x, |
|
| 40 |
y, |
|
| 41 |
text = "", |
|
| 42 |
subtext = "", |
|
| 43 |
type = c("bar", "line", "scatter", "area", "step"),
|
|
| 44 |
..., |
|
| 45 |
color = "#ffffff", |
|
| 46 |
text_color = "#ffffff", |
|
| 47 |
background_color = "#293c55", |
|
| 48 |
step = c("start", "middle", "end"),
|
|
| 49 |
title_args = list(), |
|
| 50 |
tooltip = list(trigger = "axis") |
|
| 51 |
) {
|
|
| 52 | ||
| 53 |
# for area, keep to use in series opts |
|
| 54 | 15x |
original_type <- type |
| 55 | ||
| 56 |
# inputs |
|
| 57 | 15x |
type <- match.arg(type) |
| 58 | 15x |
step <- match.arg(step) |
| 59 | ||
| 60 |
# override area |
|
| 61 | 2x |
if (type %in% c("area", "step")) type <- "line"
|
| 62 | ||
| 63 |
# Make sure x and y are strings |
|
| 64 | 15x |
x <- toString(substitute(x)) |
| 65 | 15x |
y <- toString(substitute(y)) |
| 66 | ||
| 67 |
# build expected data format |
|
| 68 | 15x |
data <- .build_data2(data, {{ x }}, {{ y }})
|
| 69 | ||
| 70 |
# build serie |
|
| 71 | 15x |
serie <- list( |
| 72 | 15x |
color = color, |
| 73 | 15x |
type = type, |
| 74 | 15x |
data = data, |
| 75 |
... |
|
| 76 |
) |
|
| 77 | ||
| 78 |
# make are |
|
| 79 | 1x |
if (original_type == "area") serie$areaStyle <- list() |
| 80 | ||
| 81 |
# make step |
|
| 82 | 1x |
if (original_type == "step") serie$step <- step |
| 83 | ||
| 84 |
# title |
|
| 85 | 15x |
title <- list( |
| 86 | 15x |
text = text, |
| 87 | 15x |
subtext = subtext |
| 88 |
) |
|
| 89 | 15x |
title <- append(title, title_args) |
| 90 | 15x |
title$textStyle$color <- text_color |
| 91 | ||
| 92 |
# build options |
|
| 93 | 15x |
opts <- list( |
| 94 | 15x |
title = title, |
| 95 | 15x |
tooltip = tooltip, |
| 96 | 15x |
xAxis = list(show = FALSE), |
| 97 | 15x |
yAxis = list(show = FALSE), |
| 98 | 15x |
grid = list( |
| 99 | 15x |
left = 0, |
| 100 | 15x |
top = 0, |
| 101 | 15x |
bottom = 0, |
| 102 | 15x |
right = 0 |
| 103 |
), |
|
| 104 | 15x |
series = list(serie) |
| 105 |
) |
|
| 106 | ||
| 107 | 15x |
list( |
| 108 | 15x |
options = opts, |
| 109 | 15x |
bgColor = background_color |
| 110 |
) |
|
| 111 |
} |
|
| 112 | ||
| 113 |
#' Render box |
|
| 114 |
#' |
|
| 115 |
#' Render an echarts4r box. |
|
| 116 |
#' |
|
| 117 |
#' @param expr An expression that produces as \code{\link{echarts4rBox}}.
|
|
| 118 |
#' @param env The environment in which to evaluate ‘expr’. |
|
| 119 |
#' @param quoted Is ‘expr’ a quoted expression (with ‘quote()’)? This is useful if you want to save an expression in a variable. |
|
| 120 |
#' |
|
| 121 |
#' @export |
|
| 122 |
renderEcharts4rBox <- function(expr, env = parent.frame(), quoted = FALSE) {
|
|
| 123 | 5x |
func <- shiny::exprToFunction(expr, env = env, quoted = quoted) |
| 124 | ||
| 125 | 5x |
function() {
|
| 126 | 4x |
val <- func() |
| 127 | ||
| 128 | 4x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 129 | ||
| 130 | 4x |
deps <- htmltools::htmlDependency( |
| 131 | 4x |
name = "echarts", |
| 132 | 4x |
version = "6.1.0", |
| 133 | 4x |
src = c(file = path), |
| 134 | 4x |
script = c("echarts.min.js")
|
| 135 |
) |
|
| 136 | ||
| 137 | 4x |
val$deps <- lapply( |
| 138 | 4x |
htmltools::resolveDependencies(list(deps)), |
| 139 | 4x |
shiny::createWebDependency |
| 140 |
) |
|
| 141 | ||
| 142 | 4x |
return(val) |
| 143 |
} |
|
| 144 |
} |
|
| 145 | ||
| 146 |
#' Box Output |
|
| 147 |
#' |
|
| 148 |
#' Place box output in Shiny ui. |
|
| 149 |
#' |
|
| 150 |
#' @param id Id of box. |
|
| 151 |
#' @param height Height of box, any valid CSS value, numerics are treated as pixels. |
|
| 152 |
#' |
|
| 153 |
#' @export |
|
| 154 |
echarts4rBoxOutput <- function(id, height = 150) {
|
|
| 155 | 8x |
if (missing(id)) {
|
| 156 | 1x |
stop("Missing id", call. = FALSE)
|
| 157 |
} |
|
| 158 | ||
| 159 |
# num to px |
|
| 160 | 7x |
if (inherits(height, "numeric")) {
|
| 161 | 6x |
height <- paste0(height, "px") |
| 162 |
} |
|
| 163 | ||
| 164 |
# make style string |
|
| 165 | 7x |
style <- sprintf("width:100%%;height:%s;", height)
|
| 166 | ||
| 167 | 7x |
el <- htmltools::div( |
| 168 | 7x |
class = "panel", |
| 169 | 7x |
id = sprintf("echarts4r-box-container-%s", id),
|
| 170 | 7x |
htmltools::div(id = id, class = "echarts4rBox", style = style) |
| 171 |
) |
|
| 172 | ||
| 173 |
# add binding and css |
|
| 174 | 7x |
path <- system.file("box", package = "echarts4r")
|
| 175 | ||
| 176 | 7x |
deps <- list( |
| 177 | 7x |
htmltools::htmlDependency( |
| 178 | 7x |
name = "echarts4rBox", |
| 179 | 7x |
version = "1.0.0", |
| 180 | 7x |
src = c(file = path), |
| 181 | 7x |
script = c("box-binding.js")
|
| 182 |
) |
|
| 183 |
) |
|
| 184 | ||
| 185 | 7x |
htmltools::attachDependencies(el, deps) |
| 186 |
} |
| 1 |
#' Themes |
|
| 2 |
#' |
|
| 3 |
#' Add a custom theme or apply a pre-built one. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param theme Theme, A json string or a see below. |
|
| 7 |
#' @param name Name of theme. |
|
| 8 |
#' |
|
| 9 |
#' @section Functions: |
|
| 10 |
#' \itemize{
|
|
| 11 |
#' \item{\code{e_theme} - Use a default theme by name.}
|
|
| 12 |
#' \item{\code{e_theme_custom} - Use a custom theme.}
|
|
| 13 |
#' \item{\code{e_theme_register} - Register a theme globally in shiny or R markdown.}
|
|
| 14 |
#' } |
|
| 15 |
#' |
|
| 16 |
#' @details The function \code{e_theme_register} can be used to register the theme globally
|
|
| 17 |
#' in R markdown or shiny (UI). This is useful because 1) the \code{e_theme_custom} registers the
|
|
| 18 |
#' theme every time and is more computationally expensive. |
|
| 19 |
#' |
|
| 20 |
#' @examples |
|
| 21 |
#' mtcars |> |
|
| 22 |
#' e_charts(mpg) |> |
|
| 23 |
#' e_line(disp) |> |
|
| 24 |
#' e_area(hp) |> |
|
| 25 |
#' e_x_axis(min = 10) -> p |
|
| 26 |
#' |
|
| 27 |
#' p |> e_theme("chalk")
|
|
| 28 |
#' p |> e_theme_custom('{"color":["#ff715e","#ffaf51"]}')
|
|
| 29 |
#' @seealso \href{https://echarts.apache.org/en/theme-builder.html}{create your own theme}.
|
|
| 30 |
#' |
|
| 31 |
#' @rdname theme |
|
| 32 |
#' @export |
|
| 33 |
e_theme <- function( |
|
| 34 |
e, |
|
| 35 |
name = c("auritus", "azul", "bee-inspired", "blue", "caravan", "carp",
|
|
| 36 |
"chalk", "cool", "dark", "dark-blue", "dark-bold", "dark-digerati", "dark-fresh-cut", |
|
| 37 |
"dark-mushroom", "eduardo", "essos", "forest", "fresh-cut", |
|
| 38 |
"fruit", "gray", "green", "halloween", "helianthus", "infographic", |
|
| 39 |
"inspired", "jazz", "london", "macarons", "macarons2", "mint", |
|
| 40 |
"purple-passion", "rainbow", "red", "red-velvet", "roma", "royal", "sakura", |
|
| 41 |
"shine", "tech-blue", "v5", "vintage", "walden", "wef", "weforum", |
|
| 42 |
"westeros", "wonderland" |
|
| 43 |
) |
|
| 44 |
) {
|
|
| 45 |
# cannot match.arg we need to support custom themes |
|
| 46 | 2x |
name <- name[1] |
| 47 | ||
| 48 |
# add name to register |
|
| 49 | 2x |
e$x$theme <- name |
| 50 | ||
| 51 |
# add dependency |
|
| 52 | 2x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/themes", package = "echarts4r")
|
| 53 | 2x |
dep <- htmltools::htmlDependency( |
| 54 | 2x |
name = name, |
| 55 | 2x |
version = "1.0.0", |
| 56 | 2x |
src = c(file = path), |
| 57 | 2x |
script = paste0(name, ".js") |
| 58 |
) |
|
| 59 | ||
| 60 | 2x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 61 | 2x |
e |
| 62 |
} |
|
| 63 | ||
| 64 |
#' @rdname theme |
|
| 65 |
#' @export |
|
| 66 |
e_theme_custom <- function(e, theme, name = "custom") {
|
|
| 67 | 4x |
if (missing(theme)) {
|
| 68 | 1x |
stop("must pass `theme`", call. = FALSE)
|
| 69 |
} |
|
| 70 | ||
| 71 | 3x |
if (grepl("\\.json", theme)) {
|
| 72 | 1x |
theme <- readLines(theme) # read the file |
| 73 | 1x |
theme <- paste0(theme, collapse = "") # collapse to a string |
| 74 |
} |
|
| 75 | ||
| 76 | 3x |
e$x$theme2 <- TRUE |
| 77 | 3x |
e$x$customTheme <- theme |
| 78 | 3x |
e$x$theme <- "custom" |
| 79 | 3x |
e$x$theme_name <- name |
| 80 | 3x |
e |
| 81 |
} |
|
| 82 | ||
| 83 |
#' @rdname theme |
|
| 84 |
#' @export |
|
| 85 |
e_theme_register <- function(theme, name = "custom") {
|
|
| 86 | 5x |
if (missing(theme)) {
|
| 87 | 1x |
stop("must pass `theme`", call. = FALSE)
|
| 88 |
} |
|
| 89 | ||
| 90 | 4x |
if (grepl("\\.json", theme)) {
|
| 91 | 1x |
theme <- readLines(theme) # read the file |
| 92 | 1x |
theme <- paste0(theme, collapse = "") # collapse to a string |
| 93 |
} |
|
| 94 | ||
| 95 | 4x |
shiny::singleton( |
| 96 | 4x |
shiny::tags$head( |
| 97 | 4x |
shiny::tags$script( |
| 98 | 4x |
paste0("window.echarts.registerTheme('", name, "', ", theme, ");")
|
| 99 |
) |
|
| 100 |
) |
|
| 101 |
) |
|
| 102 |
} |
| 1 |
#' @keywords internal |
|
| 2 |
#' @noRd |
|
| 3 |
get_base_nodes <- function(x) {
|
|
| 4 | 103x |
found_values <- list() |
| 5 | ||
| 6 | 103x |
if (is.list(x) && !is.data.frame(x)) {
|
| 7 | 39x |
current_names <- names(x) |
| 8 | ||
| 9 | 39x |
for (i in seq_along(x)) {
|
| 10 | 69x |
element <- x[[i]] |
| 11 | ||
| 12 | 69x |
is_unnamed <- is.null(current_names) || current_names[i] == "" |
| 13 | 69x |
is_atomic_data <- !is.list(element) || is.data.frame(element) |
| 14 | ||
| 15 | 69x |
if (is_unnamed && is_atomic_data) {
|
| 16 | 56x |
found_values <- c(found_values, list(element)) |
| 17 |
} |
|
| 18 | ||
| 19 | 69x |
found_values <- c(found_values, get_base_nodes(element)) |
| 20 |
} |
|
| 21 |
} |
|
| 22 | 103x |
return(found_values) |
| 23 |
} |
|
| 24 | ||
| 25 | ||
| 26 |
#' Generate Matrix |
|
| 27 |
#' |
|
| 28 |
#' helper function for generating the x and y axes for a matrix grid. |
|
| 29 |
#' |
|
| 30 |
#' @inheritParams e_bar |
|
| 31 |
#' @param xAxis,yAxis provide column name of dataframe to generate X-axis and |
|
| 32 |
#' Y-axis header cells |
|
| 33 |
#' @examples |
|
| 34 |
#' |
|
| 35 |
#' df <- data.frame("Class" = rep(c("Class1", "Class2", "Class3"),each = 3),
|
|
| 36 |
#' "Grade" = c("Grade1","Grade2", "Grade3"),
|
|
| 37 |
#' "A" = sample(1:10, 9), |
|
| 38 |
#' "B" = sample(1:10,9)) |
|
| 39 |
#' |
|
| 40 |
#' df |> e_charts() |> e_matrix(xAxis = "Class", yAxis = "Grade") |
|
| 41 |
#' |
|
| 42 |
#' @seealso \href{https://echarts.apache.org/en/option.html#matrix}{Additional
|
|
| 43 |
#' arguments} |
|
| 44 |
#' |
|
| 45 |
#' @rdname e_matrix |
|
| 46 |
#' @export |
|
| 47 |
e_matrix <- function(e, xAxis, yAxis, ...){
|
|
| 48 | 28x |
if (missing(e)) {
|
| 49 | 1x |
stop("must pass e", call. = FALSE)
|
| 50 |
} |
|
| 51 | ||
| 52 | 27x |
if(e$x$tl == TRUE){
|
| 53 | ! |
stop("timeline is not supported in matrix")
|
| 54 |
} |
|
| 55 | ||
| 56 | ||
| 57 | 27x |
if (missing(xAxis) | missing(yAxis)) {
|
| 58 | 2x |
stop("must provide both x and y values", call. = FALSE)
|
| 59 |
} |
|
| 60 |
|
|
| 61 | 25x |
e$x$opts$yAxis <- NULL |
| 62 | ||
| 63 | 25x |
e$x$opts$matrix <- append(e$x$opts$matrix, list(x = list(data = as.list(unique(e$x$data[[1]][[xAxis]])), name = xAxis))) |
| 64 | ||
| 65 | 25x |
e$x$opts$matrix <- append(e$x$opts$matrix, list(y = list(data = as.list(unique(e$x$data[[1]][[yAxis]])), name = yAxis))) |
| 66 | ||
| 67 | 25x |
e$x$opts$matrix <- append(e$x$opts$matrix, list(...)) |
| 68 | ||
| 69 | 25x |
e |
| 70 |
} |
|
| 71 | ||
| 72 |
#' Generate Matrix |
|
| 73 |
#' |
|
| 74 |
#' helper function for generating default |
|
| 75 |
#' |
|
| 76 |
#' @inheritParams e_bar |
|
| 77 |
#' @param rows,cols provide integer values for the number of rows and columns in the matrix grid |
|
| 78 |
#' @examples |
|
| 79 |
#' |
|
| 80 |
#' e_matrix_raw(rows = 3, cols = 3, backgroundStyle=list(borderWidth=0)) |
|
| 81 |
#' |
|
| 82 |
#' e_matrix_raw(rows = 3, cols = 3, body = list(itemStyle = list(borderWidth = 0))) |
|
| 83 |
#' |
|
| 84 |
#' @seealso \href{https://echarts.apache.org/en/option.html#matrix}{Additional arguments}
|
|
| 85 |
#' |
|
| 86 |
#' @rdname e_matrix_raw |
|
| 87 |
#' @export |
|
| 88 |
e_matrix_raw <- function(rows = NULL, cols = NULL, ...){
|
|
| 89 | 22x |
if(is.null(rows) || is.null(cols)) {
|
| 90 | 3x |
stop("Must provide both the number of rows and columns.")
|
| 91 |
} |
|
| 92 | ||
| 93 | 19x |
e <- e_chart() |
| 94 | 19x |
e$x$opts$yAxis <- NULL |
| 95 | 19x |
e$x$opts$matrix <- list(x = list(data = rep(list(NA), cols), show = FALSE), |
| 96 | 19x |
y = list(data = rep(list(NA),rows), show = FALSE), |
| 97 |
...) |
|
| 98 | 19x |
e |
| 99 |
} |
|
| 100 | ||
| 101 | ||
| 102 |
#' Generate Matrix Axis Parents |
|
| 103 |
#' |
|
| 104 |
#' helper function for generating parent values for x or y axis headers |
|
| 105 |
#' |
|
| 106 |
#' @inheritParams e_bar |
|
| 107 |
#' @param axis which axis the parent should be added |
|
| 108 |
#' @param value text for the new parent header cell |
|
| 109 |
#' @param children vector containing values for which current header cells will be children for the new parent cell |
|
| 110 |
#' @examples |
|
| 111 |
#' |
|
| 112 |
#' df <- data.frame("Class" = rep(c("Class1", "Class2", "Class3"),each = 3),
|
|
| 113 |
#' "Grade" = c("Grade1","Grade2", "Grade3"),
|
|
| 114 |
#' "A" = sample(1:10, 9), |
|
| 115 |
#' "B" = sample(1:10,9)) |
|
| 116 |
#' |
|
| 117 |
#' df |> e_charts() |> e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 118 |
#' e_matrix_parent(value = "Primary", children = c("Class1", "Class2")) |>
|
|
| 119 |
#' e_matrix_parent(value = "High", children = "Class3") |
|
| 120 |
#' |
|
| 121 |
#' @seealso \href{https://echarts.apache.org/en/option.html#matrix.x.data}{Additional arguments}
|
|
| 122 |
#' |
|
| 123 |
#' @rdname e_matrix_parent |
|
| 124 |
#' @export |
|
| 125 |
e_matrix_parent <- function(e, axis = "x", value, children, ...){
|
|
| 126 | 6x |
if (missing(e)) {
|
| 127 | 1x |
stop("must pass e", call. = FALSE)
|
| 128 |
} |
|
| 129 | ||
| 130 | 5x |
if(axis=="x"){
|
| 131 | ||
| 132 | 3x |
child_ndx <- which(e$x$opts$matrix$x$data %in% children) |
| 133 | ||
| 134 | 3x |
if(length(child_ndx)==0){
|
| 135 | 1x |
for(i in seq_along(e$x$opts$matrix$x$data )){
|
| 136 | 2x |
data_subset <- e$x$opts$matrix$x$data[[i]] |
| 137 | 2x |
if("value" %in% names(data_subset) && data_subset$value %in% children){
|
| 138 | ! |
child_ndx <- append(child_ndx, i) |
| 139 |
} |
|
| 140 |
} |
|
| 141 | 1x |
if(length(child_ndx)==0){
|
| 142 | 1x |
stop("No children found in the data")
|
| 143 |
} |
|
| 144 |
} |
|
| 145 | ||
| 146 | 2x |
new_node <- list(value = value, children = e$x$opts$matrix$x$data[child_ndx], ...) |
| 147 | ||
| 148 | 2x |
e$x$opts$matrix$x$data <- append(e$x$opts$matrix$x$data, list(new_node)) |
| 149 | 2x |
e$x$opts$matrix$x$data <- e$x$opts$matrix$x$data[-child_ndx] |
| 150 |
} |
|
| 151 | ||
| 152 | 4x |
if(axis=="y"){
|
| 153 | ||
| 154 | 2x |
child_ndx <- which(e$x$opts$matrix$y$data %in% children) |
| 155 | ||
| 156 | 2x |
if(length(child_ndx)==0){
|
| 157 | 1x |
for(i in seq_along(e$x$opts$matrix$y$data )){
|
| 158 | 2x |
data_subset <- e$x$opts$matrix$y$data[[i]] |
| 159 | 2x |
if("value" %in% names(data_subset) && data_subset$value %in% children){
|
| 160 | ! |
child_ndx <- append(child_ndx, i) |
| 161 |
} |
|
| 162 |
} |
|
| 163 | 1x |
if(length(child_ndx)==0){
|
| 164 | 1x |
stop("No children found in the data")
|
| 165 |
} |
|
| 166 |
} |
|
| 167 | ||
| 168 | 1x |
new_node <- list(value = value, children = e$x$opts$matrix$y$data[child_ndx], ...) |
| 169 | ||
| 170 | 1x |
e$x$opts$matrix$y$data <- append(e$x$opts$matrix$y$data, list(new_node)) |
| 171 | 1x |
e$x$opts$matrix$y$data <- e$x$opts$matrix$y$data[-child_ndx] |
| 172 |
} |
|
| 173 | ||
| 174 | 3x |
e |
| 175 | ||
| 176 |
} |
|
| 177 | ||
| 178 |
#' Fill Matrix Axis Corner |
|
| 179 |
#' |
|
| 180 |
#' helper function for adding data to the corner of matrix |
|
| 181 |
#' |
|
| 182 |
#' @inheritParams e_bar |
|
| 183 |
#' @param coord corner cell coordinate location |
|
| 184 |
#' @param value text to display in corner cell |
|
| 185 |
#' @param mergeCells whether the body cells and corner cells can be merged |
|
| 186 |
#' @param coordClamp determines whether null values can be used to indicate an entire row/column |
|
| 187 |
#' @examples |
|
| 188 |
#' |
|
| 189 |
#' df <- data.frame("Class" = rep(c("Class1", "Class2", "Class3"),each = 3),
|
|
| 190 |
#' "Grade" = c("Grade1","Grade2", "Grade3"),
|
|
| 191 |
#' "A" = sample(1:10, 9), |
|
| 192 |
#' "B" = sample(1:10,9)) |
|
| 193 |
#' |
|
| 194 |
#' df |> e_charts() |> e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 195 |
#' e_matrix_parent(value = "Primary", children = c("Class1", "Class2")) |>
|
|
| 196 |
#' e_matrix_parent(value = "High", children = "Class3") |> |
|
| 197 |
#' e_matrix_corner(value = "All School", |
|
| 198 |
#' label = list(fontSize = 24, color = "#555", position = "inside")) |
|
| 199 |
#' |
|
| 200 |
#' @seealso \href{https://echarts.apache.org/en/option.html#matrix.corner}{Additional arguments}
|
|
| 201 |
#' |
|
| 202 |
#' @rdname e_matrix_corner |
|
| 203 |
#' @export |
|
| 204 |
e_matrix_corner <- function(e, coord = c(-1,-1), value, mergeCells = TRUE, coordClamp = FALSE, ...){
|
|
| 205 | 7x |
if (missing(e)) {
|
| 206 | 1x |
stop("must pass e", call. = FALSE)
|
| 207 |
} |
|
| 208 | ||
| 209 | 6x |
data <- list(coord = coord, value = value, mergeCells = mergeCells, coordClamp = coordClamp) |
| 210 | 6x |
l <- list(data = list(data), ...) |
| 211 | ||
| 212 | 6x |
e$x$opts$matrix$corner <- append(e$x$opts$matrix$corner, l) |
| 213 | ||
| 214 | 6x |
e |
| 215 |
} |
|
| 216 | ||
| 217 |
#' Format Matrix Axis |
|
| 218 |
#' |
|
| 219 |
#' helper function for formatting the x and y axes for a matrix grid. |
|
| 220 |
#' |
|
| 221 |
#' @inheritParams e_bar |
|
| 222 |
#' @param axis indicate which axis shoud be adjusted |
|
| 223 |
#' @examples |
|
| 224 |
#' |
|
| 225 |
#' df <- data.frame("Class" = rep(c("Class1", "Class2", "Class3"),each = 3),
|
|
| 226 |
#' "Grade" = c("Grade1","Grade2", "Grade3"),
|
|
| 227 |
#' "A" = sample(1:10, 9), |
|
| 228 |
#' "B" = sample(1:10,9)) |
|
| 229 |
#' |
|
| 230 |
#' df |> e_charts() |> e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 231 |
#' e_format_matrix_axis(axis = "x", label = list(color = "red")) |
|
| 232 |
#' |
|
| 233 |
#' @seealso \href{https://echarts.apache.org/en/option.html#matrix}{Additional arguments}
|
|
| 234 |
#' |
|
| 235 |
#' @rdname e_format_matrix_axis |
|
| 236 |
#' @export |
|
| 237 |
e_format_matrix_axis <- function(e, axis = "x", ...){
|
|
| 238 | 5x |
if (missing(e)) {
|
| 239 | 1x |
stop("must pass e", call. = FALSE)
|
| 240 |
} |
|
| 241 | ||
| 242 | 4x |
if (missing(axis)) {
|
| 243 | 1x |
stop("must specify which axis", call. = FALSE)
|
| 244 |
} |
|
| 245 | ||
| 246 | 3x |
if(axis == "x"){
|
| 247 | 2x |
e$x$opts$matrix$x <- append(e$x$opts$matrix$x, list(...)) |
| 248 |
} |
|
| 249 | ||
| 250 | 3x |
if(axis == "y"){
|
| 251 | 1x |
e$x$opts$matrix$y <- append(e$x$opts$matrix$y, list(...)) |
| 252 |
} |
|
| 253 | ||
| 254 | 3x |
e |
| 255 |
} |
|
| 256 | ||
| 257 |
#' Add new chart to matrix chart |
|
| 258 |
#' |
|
| 259 |
#' Adds an already existing echart to your new matrix chart. Charts with timelines are not supported. |
|
| 260 |
#' |
|
| 261 |
#' @inheritParams e_bar |
|
| 262 |
#' @param chart An existing echart that you want to attach to your new matrix |
|
| 263 |
#' chart |
|
| 264 |
#' @param coord X,Y Coordinate of matrix to place your new chart. Charts can |
|
| 265 |
#' cover multiple coordinates such as `list(c(0,1),0)`. See |
|
| 266 |
#' \href{https://echarts.apache.org/en/option.html#matrix.body.data.coord}{Matrix
|
|
| 267 |
#' Coordinates} for more information on matrix coordinates. |
|
| 268 |
#' @param id Unique id value that will be added when chart is placed in the |
|
| 269 |
#' matrix. Defaults to `"chart1"` |
|
| 270 |
#' @param legend Whether chart contains a legend. Defaults to \code{TRUE}.
|
|
| 271 |
#' @param legend_pos Position of the legend. One of "top", "right", "bottom", |
|
| 272 |
#' "left". Determines to which side the `legend_space` argument applies. |
|
| 273 |
#' @param legend_space Space between legend and plot area. Supports |
|
| 274 |
#' integers(pixels) or strings(percent of parent cell). |
|
| 275 |
#' @param margin_trbl Adjusts the size of the outside margin around the plotting |
|
| 276 |
#' area. Default is `c(t = "15%", r = "5%", b = "5%", l = "5%")`. Supports |
|
| 277 |
#' integers(pixels) or strings(percent of parent cell) To change only e.g. two |
|
| 278 |
#' sides `c("r" = 8, "l" = 8)` could be used, other sides will use defaults.
|
|
| 279 |
#' |
|
| 280 |
#' |
|
| 281 |
#' @examples |
|
| 282 |
#' echart <- iris |> |
|
| 283 |
#' group_by(Species) |> |
|
| 284 |
#' e_charts(Sepal.Length) |> |
|
| 285 |
#' e_line(Sepal.Width) |> |
|
| 286 |
#' e_tooltip(trigger = "axis") |
|
| 287 |
#' |
|
| 288 |
#' e_matrix_raw(rows = 3, cols = 3, body = list( |
|
| 289 |
#' itemStyle = list(borderWidth = 0))) |> |
|
| 290 |
#' e_matrix_addChart(echart, coord = list( |
|
| 291 |
#' c(0,2),0), margin_trbl = c("b" = "20%"))
|
|
| 292 |
#' |
|
| 293 |
#' |
|
| 294 |
#' @rdname e_matrix_addChart |
|
| 295 |
#' @export |
|
| 296 |
e_matrix_addChart <- function(e, |
|
| 297 |
chart, |
|
| 298 |
coord, |
|
| 299 |
id = "chart1", |
|
| 300 |
legend = TRUE, |
|
| 301 |
legend_pos = "bottom", |
|
| 302 |
legend_space = "0%", |
|
| 303 |
margin_trbl = c(t = "5%", r = "5%", b = "15%", l = "5%") |
|
| 304 |
){
|
|
| 305 | 25x |
if (missing(e)) {
|
| 306 | 1x |
stop("must pass e", call. = FALSE)
|
| 307 |
} |
|
| 308 | 24x |
if (missing(chart)) {
|
| 309 | 1x |
stop("must provide an echart to add into matrix grid", call. = FALSE)
|
| 310 |
} |
|
| 311 | 23x |
if (!("echarts4r" %in% class(chart))) {
|
| 312 | 1x |
stop("the provided chart must be an echart", call. = FALSE)
|
| 313 |
} |
|
| 314 | 22x |
if (missing(coord)) {
|
| 315 | 1x |
stop("must provide coordinates for chart locations in matrix grid", call. = FALSE)
|
| 316 |
} |
|
| 317 | 21x |
if (chart$x$tl) {
|
| 318 | 1x |
stop("e_matrix_addChart does not currently support timeline charts", call. = FALSE)
|
| 319 |
} |
|
| 320 |
|
|
| 321 | 20x |
if(!is.null(chart$x$opts$parallelAxis)){
|
| 322 | ! |
stop("e_matrix_addChart does not support parallel charts")
|
| 323 |
} |
|
| 324 |
|
|
| 325 | 20x |
if(!is.null(chart$x$opts$calendar)){
|
| 326 | ! |
stop("e_matrix_addChart does not support calendar charts")
|
| 327 |
} |
|
| 328 |
|
|
| 329 | 20x |
if(!is.null(chart$x$opts$radar)){
|
| 330 | ! |
stop("e_matrix_addChart does not support radar charts")
|
| 331 |
} |
|
| 332 |
|
|
| 333 |
# Remove existing yAxis if using e_matrix instead of e_matrix_raw |
|
| 334 | 20x |
if(is.null(e$x$opts$yAxis[[1]]$id)){
|
| 335 | 15x |
e$x$opts$yAxis <- NULL |
| 336 |
} |
|
| 337 |
|
|
| 338 |
|
|
| 339 |
# ID Check |
|
| 340 | 20x |
if(!is.null(e$x$opts$yAxis)){
|
| 341 | 5x |
for(i in seq_along(e$x$opts$yAxis)){
|
| 342 | 6x |
if(e$x$opts$yAxis[[i]]$id == id){
|
| 343 | 1x |
stop("Chart id has already been used. Please supply a different id value")
|
| 344 |
} |
|
| 345 |
} |
|
| 346 |
} |
|
| 347 | ||
| 348 |
# Get count of existing charts in matrix for future ids |
|
| 349 | 19x |
chart_count <- length(e$x$opts$xAxis) |
| 350 |
# Specified margins |
|
| 351 | 19x |
spec_margin <- c("t", "r", "b", "l") %in% names(margin_trbl) # see which elements are present
|
| 352 | 19x |
margin_trbl <- c(margin_trbl, # add missing elements |
| 353 | 19x |
c(t = 5, r = 5, b = 5, l = 5)[!spec_margin]) |
| 354 | ||
| 355 |
# add new data into matrix chart |
|
| 356 | 19x |
e$x$data <- append(e$x$data, chart$x$data) |
| 357 | ||
| 358 |
#Build x and y axes if they are missing |
|
| 359 | 19x |
if(is.null(chart$x$opts$yAxis)){
|
| 360 | ! |
chart$x$opts$yAxis[[1]] <- list(show = FALSE) |
| 361 |
} |
|
| 362 | 19x |
if(is.null(chart$x$opts$xAxis)){
|
| 363 | ! |
chart$x$opts$xAxis[[1]] <- list(show = FALSE) |
| 364 |
} |
|
| 365 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
# add IDs for grid and matrix based on supplied id |
|
| 369 | 19x |
chart <- chart |> e_axis(axis = "x", id = id, gridId = id) |> e_axis(axis = "y", id = id, gridId = id) |
| 370 | ||
| 371 | 19x |
for(i in seq_along(chart$x$opts$series)){
|
| 372 | 19x |
chart$x$opts$series[[i]]$xAxisId <- id |
| 373 | 19x |
chart$x$opts$series[[i]]$yAxisId <- id |
| 374 | 19x |
chart$x$opts$series[[i]]$xAxisIndex <- chart_count |
| 375 | 19x |
chart$x$opts$series[[i]]$yAxisIndex <- chart_count |
| 376 |
|
|
| 377 | 19x |
if(chart$x$opts$series[[i]]$type == "themeRiver"){
|
| 378 | ! |
stop("e_matrix_addChart does not support themeRiver charts")
|
| 379 |
} |
|
| 380 |
|
|
| 381 | 19x |
if(chart$x$opts$series[[i]]$type == "pie"){
|
| 382 | ! |
chart$x$opts$series[[i]]$coordinateSystem <- "matrix" |
| 383 | ! |
chart$x$opts$series[[i]]$coord <- coord |
| 384 |
} |
|
| 385 |
} |
|
| 386 |
|
|
| 387 |
## Add visualMap |
|
| 388 | 19x |
if(!is.null(chart$x$opts$visualMap)){
|
| 389 | ! |
chart$x$opts$series[[1]]$id <- id |
| 390 | ! |
chart$x$opts$visualMap[[1]]$coordinateSystem <- "matrix" |
| 391 | ! |
chart$x$opts$visualMap[[1]]$seriesId <- id |
| 392 | ! |
chart$x$opts$visualMap[[1]]$coord <- coord |
| 393 | ! |
e$x$opts$visualMap <- append(e$x$opts$visualMap, chart$x$opts$visualMap) |
| 394 |
} |
|
| 395 | ||
| 396 |
# Attach series and axis information to the matrix chart |
|
| 397 | 19x |
e$x$opts$yAxis <- append(e$x$opts$yAxis, chart$x$opts$yAxis) |
| 398 | 19x |
e$x$opts$xAxis <- append(e$x$opts$xAxis, chart$x$opts$xAxis) |
| 399 | 19x |
e$x$opts$series <- append(e$x$opts$series, chart$x$opts$series) |
| 400 | ||
| 401 |
# Generate and link grid |
|
| 402 | 19x |
e$x$opts$grid <- append(e$x$opts$grid, list(list(id = id, coordinateSystem = "matrix", coord = coord |
| 403 | 19x |
, left = margin_trbl[["l"]] |
| 404 | 19x |
, top = margin_trbl[["t"]] |
| 405 | 19x |
, right = margin_trbl[["r"]] |
| 406 | 19x |
, bottom = margin_trbl[["b"]] |
| 407 |
) |
|
| 408 |
) |
|
| 409 |
) |
|
| 410 |
|
|
| 411 |
|
|
| 412 | ||
| 413 |
# Update and add legend |
|
| 414 | 19x |
if(legend){
|
| 415 | 15x |
chart$x$opts$legend$coordinateSystem <- "matrix" |
| 416 | 15x |
chart$x$opts$legend$coord <- coord |
| 417 | 15x |
if(legend_pos == "top"){
|
| 418 | 1x |
chart$x$opts$legend$top <- legend_space |
| 419 | 14x |
} else if(legend_pos == "right"){
|
| 420 | 1x |
chart$x$opts$legend$right <- legend_space |
| 421 | 13x |
} else if(legend_pos == "left"){
|
| 422 | 1x |
chart$x$opts$legend$left <- legend_space |
| 423 | 12x |
} else if(legend_pos == "bottom"){
|
| 424 | 12x |
chart$x$opts$legend$bottom <- legend_space |
| 425 |
} |
|
| 426 | 15x |
e$x$opts$legend <- append(e$x$opts$legend, list(chart$x$opts$legend)) |
| 427 |
} |
|
| 428 | ||
| 429 | 19x |
e |
| 430 |
} |
|
| 431 | ||
| 432 | ||
| 433 |
#' Generate Chart Titles for Matrix |
|
| 434 |
#' |
|
| 435 |
#' helper function for creating titles for every plot in a geofacet style |
|
| 436 |
#' matrix. This generates the title using the name of the series. |
|
| 437 |
#' |
|
| 438 |
#' @inheritParams e_bar |
|
| 439 |
#' @examples |
|
| 440 |
#' df <- data.frame(group = rep(letters[1:6], each = 20), |
|
| 441 |
#' date = seq(from = as.Date("2025-01-01"),
|
|
| 442 |
#' to = as.Date("2025-01-20"), by = "day"),
|
|
| 443 |
#' temp = sample(c(10:20), size = 60, replace = TRUE)) |
|
| 444 |
#' grid <- data.frame(name = unique(df$group), row = c(1:6), col = c(1:6)) |
|
| 445 |
#' |
|
| 446 |
#' df |> |
|
| 447 |
#' group_by(group) |> |
|
| 448 |
#' e_chart(date) |> |
|
| 449 |
#' e_line(temp, symbol = "none") |> |
|
| 450 |
#' e_x_axis(splitNumber = 2) |> |
|
| 451 |
#' e_y_axis(splitNumber = 2) |> |
|
| 452 |
#' e_geoFacet(legend = FALSE, |
|
| 453 |
#' grid = grid, |
|
| 454 |
#' margin_trbl = c("t"="25%"),
|
|
| 455 |
#' left = "5%", |
|
| 456 |
#' width = "90%") |> |
|
| 457 |
#' e_title(text = "Group Temps") |> |
|
| 458 |
#' e_title_matrix(textStyle = list(fontSize = 10), |
|
| 459 |
#' left = "center", top = "top") |
|
| 460 |
#' |
|
| 461 |
#' @seealso \href{https://echarts.apache.org/en/option.html#title}{Additional
|
|
| 462 |
#' arguments} |
|
| 463 |
#' |
|
| 464 |
#' @rdname e_title_matrix |
|
| 465 |
#' @export |
|
| 466 |
e_title_matrix <- function(e, ...){
|
|
| 467 | 4x |
if (missing(e)) {
|
| 468 | 1x |
stop("must pass e", call. = FALSE)
|
| 469 |
} |
|
| 470 | ||
| 471 | 3x |
for(i in seq_along(e$x$opts$series)){
|
| 472 | 3x |
e$x$opts$title <- append(e$x$opts$title, list(list(text = e$x$opts$series[[i]]$name, |
| 473 | 3x |
coordinateSystem = "matrix", |
| 474 | 3x |
coord = e$x$opts$grid[[i]]$coord, |
| 475 |
...) |
|
| 476 |
)) |
|
| 477 |
} |
|
| 478 | 3x |
e |
| 479 |
} |
|
| 480 | ||
| 481 | ||
| 482 |
#' Create geoFacet Echart |
|
| 483 |
#' |
|
| 484 |
#' generates a faceted chart using matrix functionality. Provides similar |
|
| 485 |
#' functionality to e_facet(). Similar to geoFacet package. Timeline is NOT |
|
| 486 |
#' supported. |
|
| 487 |
#' |
|
| 488 |
#' @inheritParams e_bar |
|
| 489 |
#' @param rows,cols Provide integer values for the number of rows and columns in |
|
| 490 |
#' the matrix grid |
|
| 491 |
#' @param grid A custom grid containing row,col positions and name. Or a string |
|
| 492 |
#' containing the name of a premade geoFacet grid from the geoFacet package. |
|
| 493 |
#' @param legend Whether chart contains a legend. Defaults to \code{TRUE}.
|
|
| 494 |
#' @param legend_pos Position of the legend. One of "top", "right", "bottom", |
|
| 495 |
#' "left". Determines to which side the `legend_space` argument applies. |
|
| 496 |
#' @param legend_space Space between legend and plot area. Supports |
|
| 497 |
#' integers(pixels) or strings(percent of parent cell). |
|
| 498 |
#' @param margin_trbl Adjusts the size of the outside margin around the plotting |
|
| 499 |
#' area. Default is `c(t = "15%", r = "5%", b = "5%", l = "5%")`. Supports |
|
| 500 |
#' integers(pixels) or strings(percent of parent cell) To change only e.g. two |
|
| 501 |
#' sides `c("r" = 8, "l" = 8)` could be used, other sides will use defaults.
|
|
| 502 |
#' |
|
| 503 |
#' |
|
| 504 |
#' @examples |
|
| 505 |
#' if (require("geofacet", quietly = TRUE)) {
|
|
| 506 |
#' library(geofacet) |
|
| 507 |
#' |
|
| 508 |
#' df <- data.frame(group = rep(letters[1:6], each = 20), |
|
| 509 |
#' date = seq(from = as.Date("2025-01-01"),
|
|
| 510 |
#' to = as.Date("2025-01-20"), by = "day"),
|
|
| 511 |
#' temp = sample(c(10:20), size = 60, replace = TRUE)) |
|
| 512 |
#' grid <- data.frame(name = unique(df$group), row = c(1:6), col = c(1:6)) |
|
| 513 |
#' |
|
| 514 |
#' df |> |
|
| 515 |
#' group_by(group) |> |
|
| 516 |
#' e_chart(date) |> |
|
| 517 |
#' e_line(temp, symbol = "none") |> |
|
| 518 |
#' e_x_axis(splitNumber = 2) |> |
|
| 519 |
#' e_y_axis(splitNumber = 2) |> |
|
| 520 |
#' e_geoFacet(legend = FALSE, |
|
| 521 |
#' grid = grid, |
|
| 522 |
#' margin_trbl = c("t"="25%"),
|
|
| 523 |
#' left = "5%", |
|
| 524 |
#' width = "90%") |> |
|
| 525 |
#' e_title(text = "Group Temps") |
|
| 526 |
#' |
|
| 527 |
#' |
|
| 528 |
#' tem <- data.frame(state.x77) |
|
| 529 |
#' sta <- cbind(state.name, tem) |
|
| 530 |
#' colnames(sta)[1] <- "State" |
|
| 531 |
#' |
|
| 532 |
#' sta |> |
|
| 533 |
#' group_by(State) |> |
|
| 534 |
#' e_charts(Population) |> |
|
| 535 |
#' e_scatter(Income) |> |
|
| 536 |
#' e_geoFacet(grid = "us_state_grid1") |> |
|
| 537 |
#' e_tooltip() |
|
| 538 |
#' } |
|
| 539 |
#' |
|
| 540 |
#' @rdname e_geoFacet |
|
| 541 |
#' @export |
|
| 542 |
e_geoFacet <- function(e, |
|
| 543 |
rows, |
|
| 544 |
cols, |
|
| 545 |
grid, |
|
| 546 |
legend = TRUE, |
|
| 547 |
legend_pos = "top", |
|
| 548 |
legend_space = "10%", |
|
| 549 |
margin_trbl = c(t = "8%", r = "5%", b = "8%", l = "8%"), |
|
| 550 |
...){
|
|
| 551 | 5x |
if (missing(e)) {
|
| 552 | 1x |
stop("must pass e", call. = FALSE)
|
| 553 |
} |
|
| 554 | 4x |
if(typeof(grid) !="character" & !is.data.frame(grid)){
|
| 555 | 1x |
stop("must provide valid grid. Either name of geofacet grid object or custom dataframe grid", call. = FALSE)
|
| 556 |
} |
|
| 557 | ||
| 558 | 3x |
if(typeof(grid)=="character"){
|
| 559 | ! |
check_installed_installer("geofacet")
|
| 560 | ! |
grid <- getExportedValue("geofacet", grid)
|
| 561 |
} |
|
| 562 | ||
| 563 | 3x |
if(missing(rows) || missing(cols)){
|
| 564 | 2x |
rows <- max(grid$row) |
| 565 | 2x |
cols <- max(grid$col) |
| 566 |
} |
|
| 567 | ||
| 568 | 3x |
e2 <- e_matrix_raw(rows = rows, cols = cols, ...) |
| 569 | ||
| 570 | 3x |
n <- length(e$x$opts$series) |
| 571 | 3x |
for(i in seq_len(nrow(grid))){
|
| 572 | 7x |
e3 <- e |
| 573 | 7x |
series_pos <- NA |
| 574 | 7x |
for(j in 1:n){
|
| 575 | 21x |
if(e$x$opts$series[[j]]$name == grid$name[i]){
|
| 576 | 5x |
series_pos <- j |
| 577 |
} |
|
| 578 |
} |
|
| 579 | 7x |
if(is.na(series_pos)){
|
| 580 | 2x |
warning(paste(grid$name[i], "data not found in series", sep = ": ")) |
| 581 | 2x |
next |
| 582 |
} |
|
| 583 | 5x |
e3$x$opts$series <- list(e3$x$opts$series[[series_pos]]) |
| 584 | 5x |
e3$x$data <- NULL |
| 585 | 5x |
e2 <- e2 |> e_matrix_addChart(e2, |
| 586 | 5x |
chart = e3, |
| 587 | 5x |
coord = list(grid$col[i]-1, grid$row[i]-1), |
| 588 | 5x |
id = paste0("chart",i),
|
| 589 | 5x |
legend = FALSE, |
| 590 | 5x |
margin_trbl = margin_trbl) |
| 591 |
} |
|
| 592 | ||
| 593 | 3x |
e2 |
| 594 |
} |
|
| 595 | ||
| 596 | ||
| 597 |
#' Generate pie chart for matrix |
|
| 598 |
#' |
|
| 599 |
#' Draw pie or donut chart in matrix coordinate system |
|
| 600 |
#' |
|
| 601 |
#' @inheritParams e_bar |
|
| 602 |
#' @param x,y x and y column names for data to be used in the pie chart |
|
| 603 |
#' @param legend Whether to add a legend, defaults to \code{TRUE}.
|
|
| 604 |
#' @examples |
|
| 605 |
#' |
|
| 606 |
#' df <- data.frame("Class" = rep(c("Class1", "Class2", "Class3"),each = 3),
|
|
| 607 |
#' "Grade" = c("Grade1","Grade2", "Grade3"),
|
|
| 608 |
#' "A" = sample(1:10, 9), |
|
| 609 |
#' "B" = sample(1:10,9), |
|
| 610 |
#' "C" = sample(1:10,9)) |
|
| 611 |
#' |
|
| 612 |
#' df |> e_chart(x = A) |> |
|
| 613 |
#' e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 614 |
#' e_matrix_parent(value = "Primary", children = c("Class1", "Class2")) |>
|
|
| 615 |
#' e_matrix_parent(value = "High", children = "Class3") |> |
|
| 616 |
#' e_matrix_corner(value = "All School", |
|
| 617 |
#' label = list(fontSize = 24, color = "#555", position = "inside")) |> |
|
| 618 |
#' e_pie(B, coord_system = "matrix", label = list(show = FALSE)) |> |
|
| 619 |
#' e_tooltip(trigger = "item") |> |
|
| 620 |
#' e_legend() |
|
| 621 |
#' |
|
| 622 |
#' df |> e_chart() |> |
|
| 623 |
#' e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 624 |
#' e_pie_(serie = c("A","B","C"),
|
|
| 625 |
#' coord_system = "matrix", |
|
| 626 |
#' label = list(show = FALSE)) |> |
|
| 627 |
#' e_tooltip(trigger = "item") |> |
|
| 628 |
#' e_legend() |
|
| 629 |
#' |
|
| 630 |
#' df |> e_chart() |> |
|
| 631 |
#' e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 632 |
#' e_pie_matrix(x = "A", y = c( "B", "C")) |
|
| 633 |
#' |
|
| 634 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-pie}{Additional arguments}
|
|
| 635 |
#' |
|
| 636 |
#' @rdname e_pie_matrix |
|
| 637 |
#' @export |
|
| 638 |
e_pie_matrix <- function(e, x, y, legend = TRUE, ...){
|
|
| 639 | ||
| 640 | 5x |
if (missing(e)) {
|
| 641 | 1x |
stop("must pass e", call. = FALSE)
|
| 642 |
} |
|
| 643 | ||
| 644 | 4x |
if(is.null(e$x$opts$matrix)){
|
| 645 | 1x |
stop("Matrix coordinate system must be built before adding data. e.g. e_matrix()")
|
| 646 |
} |
|
| 647 | 3x |
e <- .rm_axis(e, TRUE, "x") |
| 648 | 3x |
e <- .rm_axis(e, TRUE, "y") |
| 649 | ||
| 650 | 3x |
base_nodes_x <- get_base_nodes(e$x$opts$matrix$x$data) |
| 651 | 3x |
base_nodes_y <- get_base_nodes(e$x$opts$matrix$y$data) |
| 652 | ||
| 653 | 3x |
for(i in seq_along(base_nodes_x)){
|
| 654 | 5x |
for(j in seq_along(base_nodes_y)){
|
| 655 | 9x |
center <- c(base_nodes_x[[i]], base_nodes_y[[j]]) |
| 656 | 9x |
data <- e$x$data[[1]] |> dplyr::filter(.data[[e$x$opts$matrix$x$name]] == center[[1]] & .data[[e$x$opts$matrix$y$name]] == center[[2]]) |> dplyr::select(dplyr::all_of(c(x,y))) |
| 657 | 9x |
l_data <- list() |
| 658 | 9x |
for(k in seq_along(col(data))){
|
| 659 | 18x |
l <- list(name = colnames(data)[k], value = data[,k]) |
| 660 | 18x |
l_data <- append(l_data, list(l)) |
| 661 |
} |
|
| 662 | 9x |
serie <- list(type = "pie", |
| 663 | 9x |
coordinateSystem = "matrix", |
| 664 | 9x |
center = center, |
| 665 | 9x |
data = l_data, |
| 666 |
... |
|
| 667 |
) |
|
| 668 | 9x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 669 |
} |
|
| 670 |
} |
|
| 671 | ||
| 672 | 3x |
e |
| 673 |
} |
|
| 674 | ||
| 675 | ||
| 676 |
#' Generate scatter point for matrix |
|
| 677 |
#' |
|
| 678 |
#' Draw scatter points chart in matrix coordinate system |
|
| 679 |
#' |
|
| 680 |
#' @inheritParams e_bar |
|
| 681 |
#' @param z Column name for data to be used for scatter points |
|
| 682 |
#' @examples |
|
| 683 |
#' |
|
| 684 |
#' df <- data.frame("Class" = rep(c("Class1", "Class2", "Class3"),each = 3),
|
|
| 685 |
#' "Grade" = c("Grade1","Grade2", "Grade3"),
|
|
| 686 |
#' "A" = sample(1:10, 9)) |
|
| 687 |
#' |
|
| 688 |
#' df |> e_chart() |> |
|
| 689 |
#' e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 690 |
#' e_matrix_parent(value = "Primary", children = c("Class1", "Class2")) |>
|
|
| 691 |
#' e_matrix_parent(value = "High", children = "Class3") |> |
|
| 692 |
#' e_matrix_corner(value = "All School", |
|
| 693 |
#' label = list(fontSize = 24, color = "#555", position = "inside")) |> |
|
| 694 |
#' e_scatter(A, coord_system = "matrix", symbolSize = 0) |> |
|
| 695 |
#' e_labels(position = "inside", |
|
| 696 |
#' formatter = htmlwidgets::JS( |
|
| 697 |
#' 'function(params){return(params.value[2]);}'),
|
|
| 698 |
#' color = "#555", |
|
| 699 |
#' fontWeight = "bold") |
|
| 700 |
#' |
|
| 701 |
#' df |> e_chart() |> |
|
| 702 |
#' e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 703 |
#' e_scatter_matrix("A") |>
|
|
| 704 |
#' e_labels(position = "inside", |
|
| 705 |
#' formatter = htmlwidgets::JS('function(params){return(params.value[2]);}'),
|
|
| 706 |
#' color = "#555", |
|
| 707 |
#' fontWeight = "bold") |> |
|
| 708 |
#' e_visual_map(A, inRange = list(symbolSize = c(1,50))) |
|
| 709 |
#' |
|
| 710 |
#' |
|
| 711 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-scatter}{Additional arguments}
|
|
| 712 |
#' |
|
| 713 |
#' @rdname e_scatter_matrix |
|
| 714 |
#' @export |
|
| 715 |
e_scatter_matrix <- function(e, z, ...){
|
|
| 716 | 6x |
if (missing(e)) {
|
| 717 | 1x |
stop("must pass e", call. = FALSE)
|
| 718 |
} |
|
| 719 | ||
| 720 | 5x |
if(is.null(e$x$opts$matrix)){
|
| 721 | 1x |
stop("Matrix coordinate system must be built before adding data. e.g. e_matrix()")
|
| 722 |
} |
|
| 723 | 4x |
e <- .rm_axis(e, TRUE, "x") |
| 724 | 4x |
e <- .rm_axis(e, TRUE, "y") |
| 725 | ||
| 726 | 4x |
base_nodes_x <- get_base_nodes(e$x$opts$matrix$x$data) |
| 727 | 4x |
base_nodes_y <- get_base_nodes(e$x$opts$matrix$y$data) |
| 728 | ||
| 729 | 4x |
all_data <- list() |
| 730 | 4x |
for(i in seq_along(base_nodes_x)){
|
| 731 | 7x |
for(j in seq_along(base_nodes_y)){
|
| 732 | 13x |
center <- c(base_nodes_x[[i]], base_nodes_y[[j]]) |
| 733 | 13x |
data <- e$x$data[[1]] |> dplyr::filter(.data[[e$x$opts$matrix$x$name]] == center[[1]] & .data[[e$x$opts$matrix$y$name]] == center[[2]]) |> dplyr::select(dplyr::all_of(c(z))) |
| 734 | 13x |
serie <- list(center[[1]], center[[2]], data[[1]]) |
| 735 | 13x |
all_data <- append(all_data, list(serie)) |
| 736 |
} |
|
| 737 |
} |
|
| 738 | ||
| 739 | 4x |
serie <- list(type = "scatter", coordinateSystem = "matrix", data = all_data, ...) |
| 740 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 741 | ||
| 742 | 4x |
e |
| 743 |
} |
|
| 744 | ||
| 745 | ||
| 746 |
#' Generate heatmap for matrix |
|
| 747 |
#' |
|
| 748 |
#' Draw heatmap chart in matrix coordinate system |
|
| 749 |
#' |
|
| 750 |
#' @inheritParams e_bar |
|
| 751 |
#' @param z_val Column name for data to be used for heatmap |
|
| 752 |
#' @examples |
|
| 753 |
#' |
|
| 754 |
#' df <- data.frame("Class" = rep(c("Class1", "Class2", "Class3"),each = 3),
|
|
| 755 |
#' "Grade" = c("Grade1","Grade2", "Grade3"),
|
|
| 756 |
#' "A" = sample(1:10, 9)) |
|
| 757 |
#' |
|
| 758 |
#' df |> e_chart() |> |
|
| 759 |
#' e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 760 |
#' e_matrix_parent(value = "Primary", children = c("Class1", "Class2")) |>
|
|
| 761 |
#' e_matrix_parent(value = "High", children = "Class3") |> |
|
| 762 |
#' e_matrix_corner(value = "All School", label = list( |
|
| 763 |
#' fontSize = 24, color = "#555", position = "inside")) |> |
|
| 764 |
#' e_heatmap(A, coord_system = "matrix") |> |
|
| 765 |
#' e_labels(position = "inside", |
|
| 766 |
#' formatter = htmlwidgets::JS( |
|
| 767 |
#' 'function(params){return(params.value[2]);}'),
|
|
| 768 |
#' color = "#555", |
|
| 769 |
#' fontWeight = "bold") |
|
| 770 |
#' |
|
| 771 |
#' df |> e_chart() |> |
|
| 772 |
#' e_matrix(xAxis = "Class", yAxis = "Grade") |> |
|
| 773 |
#' e_heatmap_matrix("A") |>
|
|
| 774 |
#' e_labels(position = "inside", |
|
| 775 |
#' formatter = htmlwidgets::JS( |
|
| 776 |
#' 'function(params){return(params.value[2]);}'),
|
|
| 777 |
#' fontWeight = "bold") |> |
|
| 778 |
#' e_visual_map(A, inRange = list(color = c("#bf444c", "#d88273", "#f6efa6")))
|
|
| 779 |
#' |
|
| 780 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-scatter}{Additional arguments}
|
|
| 781 |
#' |
|
| 782 |
#' @rdname e_heatmap_matrix |
|
| 783 |
#' @export |
|
| 784 |
e_heatmap_matrix <- function(e, z_val, ...){
|
|
| 785 | 6x |
if (missing(e)) {
|
| 786 | 1x |
stop("must pass e", call. = FALSE)
|
| 787 |
} |
|
| 788 | ||
| 789 | 5x |
if(is.null(e$x$opts$matrix)){
|
| 790 | 1x |
stop("Matrix coordinate system must be built before adding data. e.g. e_matrix()")
|
| 791 |
} |
|
| 792 | 4x |
e <- .rm_axis(e, TRUE, "x") |
| 793 | 4x |
e <- .rm_axis(e, TRUE, "y") |
| 794 | ||
| 795 | 4x |
base_nodes_x <- get_base_nodes(e$x$opts$matrix$x$data) |
| 796 | 4x |
base_nodes_y <- get_base_nodes(e$x$opts$matrix$y$data) |
| 797 | ||
| 798 | 4x |
all_data <- list() |
| 799 | 4x |
for(i in seq_along(base_nodes_x)){
|
| 800 | 7x |
for(j in seq_along(base_nodes_y)){
|
| 801 | 13x |
center <- c(base_nodes_x[[i]], base_nodes_y[[j]]) |
| 802 | 13x |
data <- e$x$data[[1]] |> dplyr::filter(.data[[e$x$opts$matrix$x$name]] == center[[1]] & .data[[e$x$opts$matrix$y$name]] == center[[2]]) |> dplyr::select(dplyr::all_of(c(z_val))) |
| 803 | 13x |
serie <- list(center[[1]], center[[2]], data[[1]]) |
| 804 | 13x |
all_data <- append(all_data, list(serie)) |
| 805 |
} |
|
| 806 |
} |
|
| 807 | ||
| 808 | 4x |
serie <- list(type = "heatmap", coordinateSystem = "matrix", data = all_data, ...) |
| 809 | 4x |
e$x$opts$series <- append(e$x$opts$series, list(serie)) |
| 810 | ||
| 811 | 4x |
e |
| 812 |
} |
| 1 |
globalVariables(c("x", "e", ".", ".data", "acc", "epoch", "loss", "size", "val_acc", "val_loss", "flights", "airports", "buildings_sample", "buildings_sample_json", "population"))
|
|
| 2 | ||
| 3 |
.arrange_data_x <- function(data, x, reorder = TRUE) {
|
|
| 4 | 370x |
vect <- data[[x]] |
| 5 | ||
| 6 | 369x |
if (reorder) {
|
| 7 | 368x |
if (any(c(inherits(vect, "numeric"), inherits(vect, "integer")))) {
|
| 8 | 280x |
data <- data[order(data[[x]]), ] |
| 9 |
} |
|
| 10 |
} |
|
| 11 | ||
| 12 | 368x |
return(data) |
| 13 |
} |
|
| 14 | ||
| 15 |
.arrange_data_by_group <- function(data, x, reorder = TRUE) {
|
|
| 16 | 73x |
vect <- data[[1]][[x]] |
| 17 | ||
| 18 | 73x |
for (i in seq_along(data)) {
|
| 19 | 207x |
if (reorder) {
|
| 20 | 207x |
if (any(c(inherits(vect, "numeric"), inherits(vect, "integer")))) {
|
| 21 | 124x |
data[[i]] <- data[[i]][order(data[[i]][[x]]), ] |
| 22 |
} |
|
| 23 |
} |
|
| 24 |
} |
|
| 25 | ||
| 26 | 73x |
return(data) |
| 27 |
} |
|
| 28 | ||
| 29 |
.assign_axis <- function(x, data) {
|
|
| 30 | 434x |
x$mapping$include_x <- FALSE |
| 31 | 434x |
cl <- x$mapping$x_class |
| 32 | 434x |
if (any(c("factor", "character") %in% cl)) {
|
| 33 | 112x |
labs <- unique(data[[x$mapping$x]]) |
| 34 | ||
| 35 | 112x |
if (length(labs) == 1) {
|
| 36 | 1x |
labs <- list(labs) |
| 37 |
} |
|
| 38 | ||
| 39 | 112x |
x$opts$xAxis <- list(list(data = labs, type = "category", boundaryGap = TRUE)) |
| 40 | 322x |
} else if (any(c("POSIXct", "POSIXlt", "Date") %in% cl)) {
|
| 41 | 6x |
labs <- unique(data[[x$mapping$x]]) |
| 42 | ||
| 43 | 6x |
if (length(labs) == 1) {
|
| 44 | ! |
labs <- list(labs) |
| 45 |
} |
|
| 46 | ||
| 47 | 6x |
x$opts$xAxis <- list(list(data = labs, type = "time", boundaryGap = TRUE)) |
| 48 |
} else {
|
|
| 49 | 316x |
x$mapping$include_x <- TRUE |
| 50 | 316x |
x$opts$xAxis <- list(list(type = "value")) |
| 51 |
} |
|
| 52 | 434x |
x |
| 53 |
} |
|
| 54 | ||
| 55 |
.rm_axis <- function(e, rm_x, axis) {
|
|
| 56 | 378x |
if (isTRUE(rm_x)) {
|
| 57 | 378x |
axis <- .r2axis(axis) |
| 58 | ||
| 59 | 378x |
if (!e$x$tl) {
|
| 60 | 332x |
e$x$opts[[axis]] <- NULL |
| 61 |
} else {
|
|
| 62 | 46x |
e$x$opts$baseOption[[axis]] <- NULL |
| 63 |
} |
|
| 64 |
} |
|
| 65 | 378x |
e |
| 66 |
} |
|
| 67 | ||
| 68 |
.build_data <- function(e, ...) {
|
|
| 69 | 5x |
data <- e$x$data[[1]] |> |
| 70 | 5x |
dplyr::select(...) |
| 71 |
# data <- data[, c(...), drop = FALSE] |
|
| 72 | 5x |
data <- unname(data) |
| 73 | ||
| 74 | 5x |
apply(data, 1, function(x) {
|
| 75 | 241x |
list(value = unlist(x, use.names = FALSE)) |
| 76 |
}) |
|
| 77 |
} |
|
| 78 | ||
| 79 |
.jitter <- function(x, factor = 0, amount = NULL) {
|
|
| 80 | 354x |
jit <- tryCatch( |
| 81 | 354x |
jitter(x, factor, amount), |
| 82 | 354x |
error = function(e) e |
| 83 |
) |
|
| 84 | ||
| 85 | 354x |
if (inherits(jit, "error")) {
|
| 86 | 19x |
x |
| 87 |
} else {
|
|
| 88 | 335x |
jit |
| 89 |
} |
|
| 90 |
} |
|
| 91 | ||
| 92 |
.build_data_size <- function(data, x, y, size, scale, symbol_size, factor = 0, amount = NULL) {
|
|
| 93 | 8x |
row.names(data) <- NULL |
| 94 | ||
| 95 | 8x |
data[["sizeECHARTS"]] <- as.numeric(data[[size]]) |
| 96 | ||
| 97 | 8x |
if (!is.null(scale)) {
|
| 98 | 8x |
data[["sizeECHARTS"]] <- scale(data[["sizeECHARTS"]]) * symbol_size |
| 99 |
} |
|
| 100 | ||
| 101 | 8x |
data <- data |> |
| 102 | 8x |
dplyr::select(dplyr::all_of(c(x = x, y = y, size, "sizeECHARTS"))) |> |
| 103 | 8x |
dplyr::mutate( |
| 104 | 8x |
x = .jitter(x, factor, amount), |
| 105 | 8x |
y = .jitter(y, factor, amount) |
| 106 |
) |
|
| 107 | ||
| 108 | 8x |
data <- unname(data) |
| 109 | ||
| 110 | 8x |
apply(data, 1, function(x) {
|
| 111 | 142x |
list(value = unlist(x, use.names = FALSE)) |
| 112 |
}) |
|
| 113 |
} |
|
| 114 | ||
| 115 |
.build_data_jitter <- function(data, x, y, factor = 0, amount = NULL) {
|
|
| 116 | 168x |
row.names(data) <- NULL |
| 117 | ||
| 118 | 168x |
data <- data |> |
| 119 | 168x |
dplyr::select(dplyr::all_of(c(x = {{x}}, y = {{y}}))) |>
|
| 120 | 168x |
dplyr::mutate( |
| 121 | 168x |
x = .jitter(x, factor, amount), |
| 122 | 168x |
y = .jitter(y, factor, amount) |
| 123 |
) |
|
| 124 | ||
| 125 | 168x |
data <- unname(data) |
| 126 | ||
| 127 | 168x |
apply(data, 1, function(x) {
|
| 128 | 6300x |
list(value = unlist(x, use.names = FALSE)) |
| 129 |
}) |
|
| 130 |
} |
|
| 131 | ||
| 132 |
.build_data2 <- function(data, ...) {
|
|
| 133 | 393x |
row.names(data) <- NULL |
| 134 | ||
| 135 |
# Noticed an issue via unit test for e_line_3d proxy. |
|
| 136 |
# select() selects only 1 match of a col name while base R selects all col names and creates duplicates cols names (i.e. y, y.1, y.2). |
|
| 137 |
# So it was building the data incorrectly such that the z value in e_line_3d was not being replaced. |
|
| 138 | ||
| 139 |
# data <- data |> |
|
| 140 |
# dplyr::select(...) |
|
| 141 | 392x |
data <- data[, c(...), drop = FALSE] |
| 142 | 390x |
data <- unname(data) |
| 143 | ||
| 144 | 390x |
apply(data, 1, function(x) {
|
| 145 | 16641x |
list(value = unlist(x, use.names = FALSE)) |
| 146 |
}) |
|
| 147 |
} |
|
| 148 | ||
| 149 |
.add_bind2 <- function(e, l, bind, col = "name", i) {
|
|
| 150 | 49x |
bind <- e$x$data[[i]] |> |
| 151 | 49x |
dplyr::select(dplyr::all_of(bind)) |> |
| 152 | 49x |
unname() |> |
| 153 | 49x |
unlist() |
| 154 | ||
| 155 | 49x |
for (i in seq_along(l)) {
|
| 156 | 1045x |
l[[i]][[col]] <- bind[i] |
| 157 |
} |
|
| 158 | 49x |
l |
| 159 |
} |
|
| 160 | ||
| 161 |
.add_bind <- function(e, l, bind, col = "name") {
|
|
| 162 | 2x |
bind <- e$x$data[[1]] |> |
| 163 | 2x |
dplyr::select(dplyr::all_of(bind)) |> |
| 164 | 2x |
unname() |> |
| 165 | 2x |
unlist() |
| 166 | ||
| 167 | 2x |
for (i in seq_along(l)) {
|
| 168 | 31x |
l[[i]][[col]] <- bind[i] |
| 169 |
} |
|
| 170 | 2x |
l |
| 171 |
} |
|
| 172 | ||
| 173 |
# Not being used |
|
| 174 |
# .build_data_p <- function(data, ..., vector = FALSE, scale = NULL, symbol_size = 1) {
|
|
| 175 |
# data <- data |> |
|
| 176 |
# dplyr::select(...) |> |
|
| 177 |
# purrr::set_names(NULL) |
|
| 178 |
# |
|
| 179 |
# if (!is.null(scale)) {
|
|
| 180 |
# data[[4]] <- scale(data[[3]]) * symbol_size |
|
| 181 |
# } else {
|
|
| 182 |
# data[[4]] <- data[[3]] |
|
| 183 |
# } |
|
| 184 |
# |
|
| 185 |
# if (isTRUE(vector)) {
|
|
| 186 |
# unlist(data) |
|
| 187 |
# } else {
|
|
| 188 |
# apply(data, 1, function(x) {
|
|
| 189 |
# list(value = unlist(x, use.names = FALSE)) |
|
| 190 |
# }) |
|
| 191 |
# } |
|
| 192 |
# } |
|
| 193 | ||
| 194 |
.build_sankey_nodes <- function(data, source, target) {
|
|
| 195 | 6x |
nodes <- c( |
| 196 | 6x |
unlist( |
| 197 | 6x |
dplyr::select(data, source) |
| 198 |
), |
|
| 199 | 6x |
unlist( |
| 200 | 6x |
dplyr::select(data, target) |
| 201 |
) |
|
| 202 |
) |
|
| 203 | ||
| 204 | 6x |
nodes <- data.frame( |
| 205 | 6x |
name = unique(nodes), |
| 206 | 6x |
stringsAsFactors = FALSE |
| 207 |
) |
|
| 208 | ||
| 209 | 6x |
apply(nodes, 1, as.list) |
| 210 |
} |
|
| 211 | ||
| 212 |
.build_sankey_edges <- function(data, source, target, values) {
|
|
| 213 | 6x |
edges <- data |> |
| 214 | 6x |
dplyr::select(dplyr::all_of(c(source, target, values))) |
| 215 | ||
| 216 | 6x |
names(edges) <- c("source", "target", "value")
|
| 217 | ||
| 218 | 6x |
apply(edges, 1, as.list) |
| 219 |
} |
|
| 220 | ||
| 221 |
.build_graph_nodes <- function(nodes, names, value, symbolSize, category, symbol, xpos, ypos) {
|
|
| 222 | 7x |
row.names(nodes) <- NULL |
| 223 | ||
| 224 | 7x |
data <- nodes |> |
| 225 | 7x |
dplyr::select( |
| 226 | 7x |
c(name = !!names, |
| 227 | 7x |
value = !!value, |
| 228 | 7x |
symbolSize = !!symbolSize, |
| 229 | 7x |
category = !!category, |
| 230 | 7x |
symbol = !!symbol, |
| 231 | 7x |
x = !!xpos, |
| 232 | 7x |
y = !!ypos) |
| 233 |
) |
|
| 234 | ||
| 235 | 7x |
apply(data, 1, as.list) |
| 236 |
} |
|
| 237 | ||
| 238 |
.build_graph_nodes_no_size <- function(nodes, names, value, symbol, xpos, ypos) {
|
|
| 239 | 2x |
row.names(nodes) <- NULL |
| 240 | ||
| 241 | 2x |
data <- nodes |> |
| 242 | 2x |
dplyr::select( |
| 243 | 2x |
c(name = !!names, |
| 244 | 2x |
value = !!value, |
| 245 | 2x |
symbol = !!symbol, |
| 246 | 2x |
x = !!xpos, |
| 247 | 2x |
y = !!ypos) |
| 248 |
) |
|
| 249 | ||
| 250 | 2x |
apply(data, 1, as.list) |
| 251 |
} |
|
| 252 | ||
| 253 |
.build_graph_nodes_no_cat <- function(nodes, names, value, symbolSize, symbol, xpos, ypos) {
|
|
| 254 | 3x |
row.names(nodes) <- NULL |
| 255 | ||
| 256 | 3x |
data <- nodes |> |
| 257 | 3x |
dplyr::select( |
| 258 | 3x |
c(name = !!names, |
| 259 | 3x |
value = !!value, |
| 260 | 3x |
symbolSize = !!symbolSize, |
| 261 | 3x |
symbol = !!symbol, |
| 262 | 3x |
x = !!xpos, |
| 263 | 3x |
y = !!ypos) |
| 264 |
) |
|
| 265 | ||
| 266 | 3x |
apply(data, 1, as.list) |
| 267 |
} |
|
| 268 | ||
| 269 |
.build_graph_edges <- function(edges, source, target, value, size, color) {
|
|
| 270 | 7x |
row.names(edges) <- NULL |
| 271 | ||
| 272 | 7x |
if (rlang::quo_is_null(size) && rlang::quo_is_null(color)) {
|
| 273 | 3x |
data <- edges |> |
| 274 | 3x |
dplyr::select( |
| 275 | 3x |
c(source = !!source, |
| 276 | 3x |
target = !!target, |
| 277 | 3x |
value = !!value) |
| 278 |
) |
|
| 279 | ||
| 280 | 3x |
x <- apply(data, 1, as.list) |
| 281 |
} |
|
| 282 | ||
| 283 | 7x |
if (!rlang::quo_is_null(size) && rlang::quo_is_null(color)) {
|
| 284 | 2x |
data <- edges |> |
| 285 | 2x |
dplyr::select( |
| 286 | 2x |
c(source = !!source, |
| 287 | 2x |
target = !!target, |
| 288 | 2x |
value = !!value, |
| 289 | 2x |
size = !!size) |
| 290 |
) |
|
| 291 | ||
| 292 | 2x |
x <- apply(data, 1, function(x) {
|
| 293 | 45x |
list( |
| 294 | 45x |
source = unname(x["source"]), |
| 295 | 45x |
target = unname(x["target"]), |
| 296 | 45x |
value = {
|
| 297 | 5x |
if (rlang::quo_is_null(value)) "" else unname(x["value"]) |
| 298 |
}, |
|
| 299 | 45x |
symbolSize = c(5, 20), |
| 300 | 45x |
lineStyle = list(width = unname(x["size"])) |
| 301 |
) |
|
| 302 |
}) |
|
| 303 |
} |
|
| 304 | ||
| 305 | 7x |
if (!rlang::quo_is_null(color) && rlang::quo_is_null(size)) {
|
| 306 | 1x |
data <- edges |> |
| 307 | 1x |
dplyr::select( |
| 308 | 1x |
c(source = !!source, |
| 309 | 1x |
target = !!target, |
| 310 | 1x |
value = !!value, |
| 311 | 1x |
color = !!color) |
| 312 |
) |
|
| 313 | ||
| 314 | 1x |
x <- apply(data, 1, function(x) {
|
| 315 | 40x |
list( |
| 316 | 40x |
source = unname(x["source"]), |
| 317 | 40x |
target = unname(x["target"]), |
| 318 | 40x |
value = {
|
| 319 | ! |
if (rlang::quo_is_null(value)) "" else unname(x["value"]) |
| 320 |
}, |
|
| 321 | 40x |
lineStyle = list(color = unname(x["color"])) |
| 322 |
) |
|
| 323 |
}) |
|
| 324 |
} |
|
| 325 | ||
| 326 | 7x |
if (!rlang::quo_is_null(size) && !rlang::quo_is_null(color)) {
|
| 327 | 1x |
data <- edges |> |
| 328 | 1x |
dplyr::select( |
| 329 | 1x |
c(source = !!source, |
| 330 | 1x |
target = !!target, |
| 331 | 1x |
value = !!value, |
| 332 | 1x |
size = !!size, |
| 333 | 1x |
color = !!color) |
| 334 |
) |
|
| 335 | ||
| 336 | 1x |
x <- apply(data, 1, function(x) {
|
| 337 | 5x |
list( |
| 338 | 5x |
source = unname(x["source"]), |
| 339 | 5x |
target = unname(x["target"]), |
| 340 | 5x |
value = {
|
| 341 | ! |
if (rlang::quo_is_null(value)) "" else unname(x["value"]) |
| 342 |
}, |
|
| 343 | 5x |
symbolSize = c(5, 20), |
| 344 | 5x |
lineStyle = list( |
| 345 | 5x |
width = unname(x["size"]), |
| 346 | 5x |
color = unname(x["color"]) |
| 347 |
) |
|
| 348 |
) |
|
| 349 |
}) |
|
| 350 |
} |
|
| 351 | ||
| 352 | 7x |
x |
| 353 |
} |
|
| 354 | ||
| 355 |
.build_graph_category <- function(nodes, cat) {
|
|
| 356 | 7x |
data <- nodes |> |
| 357 | 7x |
dplyr::select( |
| 358 | 7x |
c(name = !!cat) |
| 359 |
) |> |
|
| 360 | 7x |
unique() |
| 361 | ||
| 362 | 7x |
x <- apply(data, 1, as.list) |
| 363 | 7x |
names(x) <- NULL |
| 364 | 7x |
x |
| 365 |
} |
|
| 366 | ||
| 367 |
.build_boxplot <- function(e, serie, i) {
|
|
| 368 | 7x |
x <- .get_data(e, serie, i) |
| 369 | ||
| 370 | 7x |
boxplot.stats(x)$stats |
| 371 |
} |
|
| 372 | ||
| 373 |
.get_outliers <- function(e, serie, i) {
|
|
| 374 | 7x |
x <- .get_data(e, serie, i) |
| 375 | ||
| 376 | 7x |
boxplot.stats(x)$out |
| 377 |
} |
|
| 378 | ||
| 379 |
.build_outliers <- function(e, out) {
|
|
| 380 | 7x |
x <- length(e$x$opts$series[[1]]$data) - 1 |
| 381 | 7x |
x <- rep(x, length(out)) |
| 382 | 7x |
matrix <- cbind(x, out) |
| 383 | 7x |
apply(unname(matrix), 1, as.list) |
| 384 |
} |
|
| 385 | ||
| 386 |
.add_outliers <- function(e, serie, i) {
|
|
| 387 | 7x |
outliers <- .get_outliers(e, serie, i) |
| 388 | 7x |
outliers <- .build_outliers(e, outliers) |
| 389 | ||
| 390 | 7x |
scatter <- list( |
| 391 | 7x |
type = "scatter", |
| 392 | 7x |
data = outliers |
| 393 |
) |
|
| 394 | ||
| 395 | 7x |
if (length(e$x$opts$series) == 2) {
|
| 396 | 4x |
e$x$opts$series[[2]]$data <- append(e$x$opts$series[[2]]$data, outliers) |
| 397 |
} else {
|
|
| 398 | 3x |
e$x$opts$series <- append(e$x$opts$series, list(scatter)) |
| 399 |
} |
|
| 400 | ||
| 401 | 7x |
e |
| 402 |
} |
|
| 403 | ||
| 404 |
.build_tree <- function(e, ...) {
|
|
| 405 | 3x |
jsonl <- e$x$data[[1]] # in case user's data is already in json-list format |
| 406 | 3x |
if ("data.frame" %in% class(jsonl)) {
|
| 407 | 3x |
jsonl <- jsonlite::toJSON(jsonl, auto_unbox = TRUE, pretty = FALSE) # convert nested tibble to JSON |
| 408 | 3x |
jsonl <- jsonlite::fromJSON(jsonl, simplifyDataFrame = FALSE) # JSON to json-list |
| 409 |
} |
|
| 410 | 3x |
if (!jsonlite::validate(jsonlite::toJSON(jsonl))) {
|
| 411 | ! |
stop("invalid input data", call. = FALSE)
|
| 412 |
} |
|
| 413 | ||
| 414 | 3x |
jsonl |
| 415 |
} |
|
| 416 | ||
| 417 |
#' Adding styles to hierarchical data by item names or levels |
|
| 418 |
#' @author helgasoft.com |
|
| 419 |
#' |
|
| 420 |
#' @inheritParams e_bar |
|
| 421 |
#' @param styles Style lists, expects a \code{vector}, defaults to \code{NULL}.
|
|
| 422 |
#' @param names Names of items to style, expects a \code{list}, defaults to \code{NULL}.
|
|
| 423 |
#' @param levels Hierarchical levels to style, expects a \code{list}, defaults to \code{NULL}.
|
|
| 424 |
#' @return updated hierarchy in json list format |
|
| 425 |
#' |
|
| 426 |
#' @noRd |
|
| 427 |
#' @keywords internal |
|
| 428 |
.build_sun <- function(e, styles = NULL, names = NULL, levels = NULL) {
|
|
| 429 |
#' recursive json-list traversal, append style on matching level and/or name |
|
| 430 | 7x |
recu <- function(chld, level) {
|
| 431 | 13x |
if (!is.null(levels)) {
|
| 432 | 13x |
idLevel <- unlist(lapply( |
| 433 | 13x |
seq_along(levels), |
| 434 | 13x |
function(x, i) {
|
| 435 | 10x |
if (level %in% x[[i]]) i |
| 436 |
}, |
|
| 437 | 13x |
x = levels |
| 438 |
)) |
|
| 439 |
} |
|
| 440 | 13x |
if (!is.null(names)) {
|
| 441 | 13x |
idName <- unlist(lapply( |
| 442 | 13x |
seq_along(names), |
| 443 | 13x |
function(x, i) {
|
| 444 | 3x |
if (chld$name %in% x[[i]]) i |
| 445 |
}, |
|
| 446 | 13x |
x = names |
| 447 |
)) |
|
| 448 |
} |
|
| 449 | 13x |
id <- if (length(idName) > 0) idName else idLevel # name supersedes level |
| 450 | 13x |
if (length(id) > 0) {
|
| 451 | 10x |
chld$itemStyle <- styles[id] |
| 452 |
} |
|
| 453 | 13x |
if (!is.null(chld$children)) {
|
| 454 | 7x |
chld$children <- lapply( |
| 455 | 7x |
chld$children, |
| 456 | 7x |
function(x) recu(x, level + 1) |
| 457 |
) |
|
| 458 | 7x |
level <- level - 1 |
| 459 |
} |
|
| 460 | 13x |
chld |
| 461 |
} |
|
| 462 | ||
| 463 | 7x |
jsonl <- e$x$data[[1]] # in case user's data is already in json-list format |
| 464 | 7x |
if ("data.frame" %in% class(jsonl)) {
|
| 465 | 7x |
jsonl <- jsonlite::toJSON(jsonl, auto_unbox = TRUE, pretty = FALSE) # convert nested tibble to JSON |
| 466 | 7x |
jsonl <- jsonlite::fromJSON(jsonl, simplifyDataFrame = FALSE) # JSON to json-list |
| 467 |
} |
|
| 468 | 7x |
if (!jsonlite::validate(jsonlite::toJSON(jsonl))) {
|
| 469 | ! |
stop("invalid input data", call. = FALSE)
|
| 470 |
} |
|
| 471 | 7x |
if (!is.null(styles)) {
|
| 472 | 1x |
jsonl <- lapply(jsonl, function(x) recu(x, level = 1)) |
| 473 |
} # recursively add styles |
|
| 474 | 7x |
jsonl |
| 475 |
} |
|
| 476 | ||
| 477 |
.build_river <- function(e, serie, label, i) {
|
|
| 478 | ||
| 479 | 5x |
if(is.null(e$x$mapping$x)){
|
| 480 | ! |
stop("e$x$mapping$x is NULL")
|
| 481 |
} |
|
| 482 | ||
| 483 | 5x |
x <- .get_data(e, e$x$mapping$x, i) |
| 484 | 5x |
label <- rep(label, length(x)) |
| 485 | ||
| 486 | 5x |
data <- e$x$data[[i]] |> |
| 487 | 5x |
dplyr::select(dplyr::all_of(serie)) |
| 488 | ||
| 489 | 5x |
data <- cbind(x, data, label) |
| 490 | 5x |
row.names(data) <- NULL |
| 491 | ||
| 492 | 5x |
apply(unname(data), 1, as.list) |
| 493 |
} |
|
| 494 | ||
| 495 |
.get_class <- function(e, serie) {
|
|
| 496 | 120x |
class(.get_data(e, serie)) |
| 497 |
} |
|
| 498 | ||
| 499 |
.get_type <- function(e, serie) {
|
|
| 500 | 120x |
cl <- .get_class(e, serie) |
| 501 | ||
| 502 | 120x |
if (any(c("character", "factor") %in% cl)) {
|
| 503 | 40x |
"category" |
| 504 | 80x |
} else if (any(c("POSIXct", "POSIXlt", "Date") %in% cl)) {
|
| 505 | 1x |
"time" |
| 506 |
} else {
|
|
| 507 | 79x |
"value" |
| 508 |
} |
|
| 509 |
} |
|
| 510 | ||
| 511 |
.get_data <- function(e, serie, i = 1) {
|
|
| 512 | 229x |
data <- e$x$data[[i]] |> |
| 513 | 229x |
dplyr::select(dplyr::all_of(serie)) |> |
| 514 | 229x |
unname() |
| 515 | ||
| 516 | 229x |
data[[1]] |
| 517 |
} |
|
| 518 | ||
| 519 |
.set_any_axis <- function(e, serie, index, axis = "x", i = 1) {
|
|
| 520 | 33x |
raxis <- .r2axis(axis) |
| 521 | ||
| 522 | 33x |
update <- length(e$x$opts[[raxis]]) - 1 < index || length(e$x$opts$baseOption[[raxis]]) - 1 < index |
| 523 | ||
| 524 | 33x |
if (update) {
|
| 525 | 33x |
type <- .get_type(e, serie) |
| 526 | ||
| 527 | 33x |
ax <- list(type = type) |
| 528 | ||
| 529 | 33x |
if (type != "value") {
|
| 530 | 10x |
axis_data <- .get_data(e, serie, i) |
| 531 | ||
| 532 | 10x |
if (length(axis_data) == 1) {
|
| 533 | ! |
axis_data <- list(axis_data) |
| 534 |
} |
|
| 535 | ||
| 536 | 10x |
ax$data <- axis_data |
| 537 |
} |
|
| 538 | ||
| 539 | 33x |
if (!e$x$tl) {
|
| 540 | 31x |
e$x$opts[[raxis]][[index + 1]] <- ax |
| 541 |
} else {
|
|
| 542 | 2x |
e$x$opts$baseOption[[raxis]][[index + 1]] <- ax |
| 543 |
} |
|
| 544 |
} |
|
| 545 | ||
| 546 | 33x |
e |
| 547 |
} |
|
| 548 | ||
| 549 |
.set_x_axis <- function(e, x_index, i) {
|
|
| 550 | ||
| 551 | 15x |
if(is.null(e$x$mapping$x)){
|
| 552 | ! |
stop("e$x$mapping$x is NULL")
|
| 553 |
} |
|
| 554 | ||
| 555 | 15x |
.set_any_axis(e, e$x$mapping$x, x_index, axis = "x", i) |
| 556 |
} |
|
| 557 | ||
| 558 |
.set_y_axis <- function(e, serie, y.index, i) {
|
|
| 559 | 18x |
.set_any_axis(e, serie, y.index, axis = "y", i) |
| 560 |
} |
|
| 561 | ||
| 562 |
.set_z_axis <- function(e, serie, z.index, i) {
|
|
| 563 | ! |
.set_any_axis(e, serie, z.index, axis = "z", i) |
| 564 |
} |
|
| 565 | ||
| 566 |
.set_axis_3D <- function(e, axis, serie, index) {
|
|
| 567 | 84x |
ax <- .r2axis3D(axis) |
| 568 | ||
| 569 | 84x |
if (length(e$x$opts[[ax]]) - 1 < index) {
|
| 570 | 78x |
type <- .get_type(e, serie) |
| 571 | ||
| 572 | 78x |
axis <- list(type = type) |
| 573 | ||
| 574 | 78x |
if (type != "value") {
|
| 575 | 23x |
axis$data <- purrr::map(e$x$data, serie) |> |
| 576 | 23x |
unlist() |> |
| 577 | 23x |
unique() |
| 578 |
} |
|
| 579 | ||
| 580 | 78x |
if (!e$x$tl) {
|
| 581 | 54x |
e$x$opts[[ax]][[index + 1]] <- axis |
| 582 |
} else {
|
|
| 583 | 24x |
e$x$opts$baseOption[[ax]][[index + 1]] <- axis |
| 584 |
} |
|
| 585 |
} |
|
| 586 | 84x |
e |
| 587 |
} |
|
| 588 | ||
| 589 |
.r2axis <- function(axis) {
|
|
| 590 | 497x |
paste0(axis, "Axis") |
| 591 |
} |
|
| 592 | ||
| 593 |
.r2axis3D <- function(axis) {
|
|
| 594 | 89x |
paste0(axis, "Axis3D") |
| 595 |
} |
|
| 596 | ||
| 597 |
.map_lines <- function(e, source.lon, source.lat, target.lon, target.lat, source.name, target.name, value, i) {
|
|
| 598 | 12x |
data <- e$x$data[[i]] |> |
| 599 | 12x |
dplyr::select(dplyr::all_of( |
| 600 | 12x |
c(source.lon, |
| 601 | 12x |
source.lat, |
| 602 | 12x |
target.lon, |
| 603 | 12x |
target.lat) |
| 604 |
)) |> |
|
| 605 | 12x |
apply(1, function(x) {
|
| 606 | 1261x |
x <- unname(x) |
| 607 | 1261x |
list( |
| 608 | 1261x |
coords = list( |
| 609 | 1261x |
c(x[1], x[2]), |
| 610 | 1261x |
c(x[3], x[4]) |
| 611 |
) |
|
| 612 |
) |
|
| 613 |
}) |
|
| 614 | 12x |
if (!is.null(source.name)) {
|
| 615 | 2x |
data <- .add_bind2(e, data, source.name, col = "source_name", i) |
| 616 |
} |
|
| 617 | 12x |
if (!is.null(target.name)) {
|
| 618 | 2x |
data <- .add_bind2(e, data, target.name, col = "target_name", i) |
| 619 |
} |
|
| 620 | 12x |
if (!is.null(value)) {
|
| 621 | 2x |
data <- .add_bind2(e, data, value, col = "value", i) |
| 622 |
} |
|
| 623 | 12x |
data |
| 624 |
} |
|
| 625 | ||
| 626 |
.build_cartesian3D <- function(e, ..., i = 1) {
|
|
| 627 |
# Don't use this bc it only selects unique cols. |
|
| 628 |
# ... could be the same col. |
|
| 629 |
# df <- e$x$data[[i]] |> |
|
| 630 |
# dplyr::select( |
|
| 631 |
# ... |
|
| 632 |
# ) |> |
|
| 633 |
# unname() |
|
| 634 | 18x |
df <- e$x$data[[i]][, c(...), drop = FALSE] |
| 635 | 18x |
row.names(df) <- NULL |
| 636 | 18x |
df <- unname(df) |
| 637 | ||
| 638 | 18x |
apply(df, 1, function(x) {
|
| 639 | 778x |
list(value = x) |
| 640 |
}) |
|
| 641 |
} |
|
| 642 | ||
| 643 | ||
| 644 |
.build_height <- function(e, serie, color, j) {
|
|
| 645 |
# data <- .build_data(e, e$x$mapping$x, serie, names = c("name", "height"))
|
|
| 646 | ||
| 647 | 1x |
if(is.null(e$x$mapping$x)){
|
| 648 | ! |
stop("e$x$mapping$x is NULL")
|
| 649 |
} |
|
| 650 | ||
| 651 | 1x |
data <- e$x$data[[j]] |> |
| 652 | 1x |
dplyr::select(dplyr::all_of( |
| 653 | 1x |
c(name = e$x$mapping$x, |
| 654 | 1x |
height = serie) |
| 655 |
)) |
|
| 656 | ||
| 657 | 1x |
names(data) <- c("name", "height")
|
| 658 | ||
| 659 | 1x |
l <- apply(data, 1, as.list) |
| 660 | ||
| 661 | 1x |
if (!missing(color)) {
|
| 662 | 1x |
color <- .get_data(e, color) |
| 663 | ||
| 664 | 1x |
for (i in seq_along(l)) {
|
| 665 | 9x |
is <- list( |
| 666 | 9x |
color = color[i] |
| 667 |
) |
|
| 668 | 9x |
l[[i]]$itemStyle <- is |
| 669 |
} |
|
| 670 |
} |
|
| 671 | 1x |
l |
| 672 |
} |
|
| 673 | ||
| 674 |
.correct_countries <- function(x) {
|
|
| 675 | 4x |
dplyr::recode( |
| 676 | 4x |
x, |
| 677 | 4x |
"United States of America" = "United States", |
| 678 | 4x |
"Viet Nam" = "Vietnam", |
| 679 | 4x |
"United Kingdom of Great Britain and Northern Ireland" = "United Kingdom", |
| 680 | 4x |
"Republic of Korea" = "Korea", |
| 681 | 4x |
"Russian Federation" = "Russia", |
| 682 | 4x |
"Congo - Kinshasa" = "Dem. Rep. Congo", |
| 683 | 4x |
"Congo - Brazzaville" = "Congo", |
| 684 | 4x |
"Central African Republic" = "Central African Rep.", |
| 685 | 4x |
"South Sudan" = "S. Sudan", |
| 686 | 4x |
"North Korea" = "Dem. Rep. Korea", |
| 687 | 4x |
"South Korea" = "Korea", |
| 688 | 4x |
"Western Sahara" = "W. Sahara", |
| 689 | 4x |
"Myanmar (Burma)" = "Myanmar", |
| 690 | 4x |
"Laos" = "Lao PDR", |
| 691 | 4x |
"C\\u00f4te d\\u2019Ivoire" = "C\\u00f4te d\\'Ivoire", |
| 692 | 4x |
"Czechia" = "Czech Rep.", |
| 693 | 4x |
"Equatorial Guinea" = "Eq. Guinea", |
| 694 | 4x |
"Eswatini" = "Swaziland", |
| 695 | 4x |
"Falkland Islands" = "Falkland Is.", |
| 696 | 4x |
"South Georgia & South Sandwich Islands" = "S. Geo. and S. Sandw. Is.", |
| 697 | 4x |
"French Southern Territories" = "Fr. S. Antarctic Lands", |
| 698 | 4x |
"British Indian Ocean Territory" = "Br. Indian Ocean Ter.", |
| 699 | 4x |
"Solomon Islands" = "Solomon Is.", |
| 700 | 4x |
"Dominican Republic" = "Dominican Rep.", |
| 701 | 4x |
"Bosnia & Herzegovina" = "Bosnia and Herz.", |
| 702 | 4x |
"North Macedonia" = "Macedonia", |
| 703 | 4x |
"Heard & McDonald Islands" = "Heard I. and McDonald Is.", |
| 704 | 4x |
"Micronesia (Federated States of)" = "Micronesia", |
| 705 | 4x |
"Trinidad & Tobago" = "Trinidad and Tobago", |
| 706 | 4x |
"St. Vincent & Grenadines" = "St. Vin. and Gren.", |
| 707 | 4x |
"St. Lucia" = "Saint Lucia", |
| 708 | 4x |
"Antigua & Barbuda" = "Antigua", |
| 709 | 4x |
"U.S. Virgin Islands" = "U.S. Virgin Is.", |
| 710 | 4x |
"Faroe Islands" = "Faeroe Is.", |
| 711 | 4x |
"\\u00c5lland Islands" = "Aland" |
| 712 |
) |
|
| 713 |
} |
|
| 714 | ||
| 715 |
.get_index <- function(e, serie) {
|
|
| 716 | 3x |
series <- if (e$x$tl) names(e$x$data) else purrr::map(e$x$opts$series, "name") |> unlist() |
| 717 | 3x |
purrr::map(serie, ~ which(series == .)) |> |
| 718 | 3x |
unlist() |> |
| 719 | 3x |
unique() |
| 720 |
} |
|
| 721 | ||
| 722 |
.add_indicators <- function(e, r.index, max, radar = list()) {
|
|
| 723 | 8x |
if (!length(e$x$opts$radar)) {
|
| 724 | 8x |
e$x$opts$radar <- list(list()) |
| 725 |
} |
|
| 726 | ||
| 727 | 8x |
if(is.null(e$x$mapping$x)){
|
| 728 | ! |
stop("e$x$mapping$x is NULL")
|
| 729 |
} |
|
| 730 | ||
| 731 | 8x |
x <- .get_data(e, e$x$mapping$x) |
| 732 | ||
| 733 | 8x |
indicators <- data.frame( |
| 734 | 8x |
name = x, |
| 735 | 8x |
max = rep(max, length(x)) |
| 736 |
) |
|
| 737 | ||
| 738 | 8x |
indicators <- apply(indicators, 1, as.list) |
| 739 | ||
| 740 | 8x |
e$x$opts$radar[[r.index + 1]] <- radar |
| 741 | 8x |
e$x$opts$radar[[r.index + 1]]$indicator <- indicators |
| 742 | 8x |
e |
| 743 |
} |
|
| 744 | ||
| 745 |
.name_it <- function(e, serie, name, i) {
|
|
| 746 | 347x |
if (is.null(name)) { # defaults to column name
|
| 747 | 307x |
if (!is.null(names(e$x$data)[i])) {
|
| 748 | 67x |
nm <- names(e$x$data)[i] |
| 749 |
} else {
|
|
| 750 | 240x |
nm <- serie |
| 751 |
} |
|
| 752 |
} else {
|
|
| 753 | 40x |
nm <- name |
| 754 |
} |
|
| 755 | ||
| 756 | 347x |
return(nm) |
| 757 |
} |
|
| 758 | ||
| 759 |
.list_depth <- function(this, thisdepth = 0) {
|
|
| 760 | 259x |
if (!is.list(this)) {
|
| 761 | 102x |
return(thisdepth) |
| 762 |
} else {
|
|
| 763 | 157x |
return(max(unlist(lapply(this, .list_depth, thisdepth = thisdepth + 1)))) |
| 764 |
} |
|
| 765 |
} |
|
| 766 | ||
| 767 |
.e_graphic_elem <- function(e, elem, ...) {
|
|
| 768 | 13x |
if (!e$x$tl) {
|
| 769 | 13x |
if (length(e$x$opts$graphic) == 0) {
|
| 770 | 13x |
e$x$opts$graphic <- list(...) |
| 771 |
} |
|
| 772 | ! |
} else if (length(e$x$opts$baseOption$graphic) == 0) {
|
| 773 | ! |
e$x$opts$baseOption$graphic <- list(...) |
| 774 |
} |
|
| 775 | ||
| 776 | 13x |
opts <- list(type = elem, ...) |
| 777 | ||
| 778 | 13x |
if (!e$x$tl) {
|
| 779 | 13x |
e$x$opts$graphic <- append(e$x$opts$graphic, opts) |
| 780 |
} else {
|
|
| 781 | ! |
e$x$opts$baseOption$graphic <- append(e$x$opts$baseOption$graphic, opts) |
| 782 |
} |
|
| 783 | ||
| 784 | 13x |
e |
| 785 |
} |
|
| 786 | ||
| 787 |
.get_locale <- function() {
|
|
| 788 | 12x |
"en" |
| 789 |
} |
|
| 790 | ||
| 791 |
#' Determines the extremum of a vector |
|
| 792 |
#' |
|
| 793 |
#' If the vector contains only 1 element, then one of the extremum is set to 0. |
|
| 794 |
#' |
|
| 795 |
#' @author Wei Su |
|
| 796 |
#' |
|
| 797 |
#' @param rng Numeric vector. |
|
| 798 |
#' @return A list contains maximum and minimum value. |
|
| 799 |
#' |
|
| 800 |
#' @noRd |
|
| 801 |
.get_validate_range <- function(rng) {
|
|
| 802 | 21x |
max <- max(rng, na.rm = TRUE) |
| 803 | 20x |
min <- min(rng, na.rm = TRUE) |
| 804 | 20x |
if (!(max > min)) {
|
| 805 | ! |
if (rng[1] >= 0) {
|
| 806 | ! |
max <- rng[1] |
| 807 | ! |
min <- 0 |
| 808 |
} else {
|
|
| 809 | ! |
max <- 0 |
| 810 | ! |
min <- rng[1] |
| 811 |
} |
|
| 812 |
} |
|
| 813 | 20x |
return(list(max = max, min = min)) |
| 814 |
} |
|
| 815 | ||
| 816 |
check_installed <- function(pkg) {
|
|
| 817 | ! |
has_it <- base::requireNamespace(pkg, quietly = TRUE) |
| 818 | ||
| 819 | ! |
if (!has_it) {
|
| 820 | ! |
stop(sprintf("This function requires the package {%s}", pkg), call. = FALSE)
|
| 821 |
} |
|
| 822 |
} |
|
| 823 | ||
| 824 |
.build_zigzags <- function(data, starts, ends, gap) {
|
|
| 825 | 6x |
zigzags <- data |> |
| 826 | 6x |
dplyr::select(dplyr::all_of(c(starts, ends, gap))) |
| 827 | ||
| 828 | 6x |
names(zigzags) <- c("start", "end", "gap")
|
| 829 | ||
| 830 | 6x |
apply(zigzags, 1, as.list) |
| 831 |
} |
|
| 832 | ||
| 833 |
# Not used |
|
| 834 |
# .get_base_nodes <- function(x) {
|
|
| 835 |
# found_values <- list() |
|
| 836 |
# |
|
| 837 |
# if (is.list(x) && !is.data.frame(x)) {
|
|
| 838 |
# current_names <- names(x) |
|
| 839 |
# |
|
| 840 |
# for (i in seq_along(x)) {
|
|
| 841 |
# element <- x[[i]] |
|
| 842 |
# |
|
| 843 |
# is_unnamed <- is.null(current_names) || current_names[i] == "" |
|
| 844 |
# |
|
| 845 |
# is_atomic_data <- !is.list(element) || is.data.frame(element) |
|
| 846 |
# |
|
| 847 |
# if (is_unnamed && is_atomic_data) {
|
|
| 848 |
# found_values <- c(found_values, list(element)) |
|
| 849 |
# } |
|
| 850 |
# |
|
| 851 |
# found_values <- c(found_values, get_base_nodes(element)) |
|
| 852 |
# } |
|
| 853 |
# } |
|
| 854 |
# return(found_values) |
|
| 855 |
# } |
|
| 856 | ||
| 857 |
.get_bind <- function(bind){
|
|
| 858 | 307x |
if (bind == "") {
|
| 859 | 306x |
bd <- NULL |
| 860 |
} else {
|
|
| 861 | 1x |
bd <- bind |
| 862 |
} |
|
| 863 | 307x |
return(bd) |
| 864 |
} |
|
| 865 | ||
| 866 |
.get_size <- function(size){
|
|
| 867 | 142x |
if (size == "") {
|
| 868 | 136x |
sz <- NULL |
| 869 |
} else {
|
|
| 870 | 6x |
sz <- size |
| 871 |
} |
|
| 872 | 142x |
return(sz) |
| 873 |
} |
|
| 874 | ||
| 875 |
.actions_btn <- function(e, opts, btn){
|
|
| 876 | 38x |
if (!is.null(btn)) {
|
| 877 | 26x |
if (!btn %in% names(e$x$buttons)) {
|
| 878 | 15x |
e$x$buttons[[btn]] <- list(opts) |
| 879 |
} else {
|
|
| 880 | 11x |
e$x$buttons[[btn]] <- append(e$x$buttons[[btn]], list(opts)) |
| 881 |
} |
|
| 882 |
} else {
|
|
| 883 | 12x |
e$x$events <- append(e$x$events, list(opts)) |
| 884 |
} |
|
| 885 | 38x |
return(e) |
| 886 |
} |
|
| 887 | ||
| 888 | ||
| 889 |
check_installed_installer <- function(pkg) {
|
|
| 890 | ! |
has_it <- base::requireNamespace(pkg, quietly = TRUE) |
| 891 | ||
| 892 | ! |
if (!has_it) {
|
| 893 | ! |
cat(paste("This function requires the package:", pkg, "\n"))
|
| 894 | ! |
install_pkg(pkg) |
| 895 |
} |
|
| 896 | ||
| 897 | ||
| 898 |
} |
|
| 899 | ||
| 900 | ||
| 901 |
install_pkg <- function(pkg) {
|
|
| 902 | ! |
input <- 1L |
| 903 | ||
| 904 | ! |
if (interactive()) {
|
| 905 | ! |
input <- utils::menu( |
| 906 | ! |
c("Yes", "No"),
|
| 907 | ! |
title = paste("Install the", pkg, "package?")
|
| 908 |
) |
|
| 909 |
} |
|
| 910 | ||
| 911 | ! |
if (input != 1L) {
|
| 912 | ! |
cli::cli_abort( |
| 913 | ! |
paste0("The {.pkg ", pkg,"} package is necessary for that method.\n Please try installing the package yourself with {.code install.packages(\"",pkg,"\")}")
|
| 914 |
) |
|
| 915 |
} |
|
| 916 | ||
| 917 | ! |
cli::cli_inform(paste0("Installing the {.pkg ", pkg,"} package."))
|
| 918 | ||
| 919 | ! |
tryCatch( |
| 920 | ! |
utils::install.packages(pkg), |
| 921 | ! |
error = function(e) {
|
| 922 | ! |
cli::cli_inform(conditionMessage(e)) |
| 923 |
} |
|
| 924 |
) |
|
| 925 | ||
| 926 | ! |
tryCatch( |
| 927 | ! |
require(pkg, character.only = TRUE), |
| 928 | ! |
error = function(e) {
|
| 929 | ! |
cli::cli_inform(conditionMessage(e)) |
| 930 |
} |
|
| 931 |
) |
|
| 932 |
} |
| 1 |
#' Timeline |
|
| 2 |
#' |
|
| 3 |
#' Set timeline options |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param axis_type Type of axis, \code{time}, \code{value}, or \code{category}.
|
|
| 7 |
#' @param index The index of the element to apply options to, see examples. |
|
| 8 |
#' @param serie_index The index of the serie to add elements to. |
|
| 9 |
#' @param ... Named options. |
|
| 10 |
#' |
|
| 11 |
#' @section Functions: |
|
| 12 |
#' \itemize{
|
|
| 13 |
#' \item{\code{e_timeline_opts}: Pass general timeline options, see \href{https://echarts.apache.org/en/option.html#timeline}{official documentation}.}
|
|
| 14 |
#' \item{\code{e_timeline_serie}: Pass options to each serie, each options \emph{must} be a vector or list the same length as their are steps, see examples.}
|
|
| 15 |
#' \item{\code{e_timeline_make}: Helper function that wraps your data and \code{e_timeline_serie} to dynamically add options to series.}
|
|
| 16 |
#' } |
|
| 17 |
#' |
|
| 18 |
#' @examples |
|
| 19 |
#' # general options |
|
| 20 |
#' iris |> |
|
| 21 |
#' group_by(Species) |> |
|
| 22 |
#' e_charts(Sepal.Length, timeline = TRUE) |> |
|
| 23 |
#' e_line(Sepal.Width) |> |
|
| 24 |
#' e_timeline_opts( |
|
| 25 |
#' autoPlay = TRUE, |
|
| 26 |
#' rewind = TRUE |
|
| 27 |
#' ) |
|
| 28 |
#' |
|
| 29 |
#' # serie options |
|
| 30 |
#' iris |> |
|
| 31 |
#' group_by(Species) |> |
|
| 32 |
#' e_charts(Sepal.Length, timeline = TRUE) |> |
|
| 33 |
#' e_line(Sepal.Width) |> |
|
| 34 |
#' e_timeline_serie( |
|
| 35 |
#' title = list( |
|
| 36 |
#' list(text = "setosa"), |
|
| 37 |
#' list(text = "versicolor"), |
|
| 38 |
#' list(text = "virginica") |
|
| 39 |
#' ) |
|
| 40 |
#' ) |
|
| 41 |
#' @name timeline-opts |
|
| 42 |
#' @export |
|
| 43 |
e_timeline_opts <- function(e, axis_type = "category", ...) {
|
|
| 44 | 3x |
if (missing(e)) {
|
| 45 | 1x |
stop("must pass e", call. = FALSE)
|
| 46 |
} |
|
| 47 | ||
| 48 | 2x |
if (!e$x$tl) {
|
| 49 | 1x |
warning("timeline not enabled in e_chart", call. = FALSE)
|
| 50 |
} |
|
| 51 | ||
| 52 | 2x |
e$x$opts$baseOption$timeline$axisType <- axis_type |
| 53 | ||
| 54 | 2x |
e$x$opts$baseOption$timeline <- append(e$x$opts$baseOption$timeline, list(...)) |
| 55 | ||
| 56 | 2x |
e |
| 57 |
} |
|
| 58 | ||
| 59 |
#' @name timeline-opts |
|
| 60 |
#' @export |
|
| 61 |
e_timeline_serie <- function(e, ..., index = 1) {
|
|
| 62 | 16x |
if (missing(e)) {
|
| 63 | 1x |
stop("must pass e", call. = FALSE)
|
| 64 |
} |
|
| 65 | ||
| 66 | 15x |
args <- list(...) |
| 67 | ||
| 68 | 15x |
if (length(args) == 0) {
|
| 69 | 1x |
stop("no arguments passed", call. = FALSE)
|
| 70 |
} |
|
| 71 | ||
| 72 | 14x |
for (i in seq_along(e$x$opts$options)) {
|
| 73 | 37x |
for (j in seq_along(args)) {
|
| 74 | 37x |
if (!length(e$x$opts$options[[i]][[names(args)[j]]]) < index) {
|
| 75 | ! |
e$x$opts$options[[i]][[names(args)[j]]][[index]] <- list() |
| 76 |
} |
|
| 77 | 37x |
e$x$opts$options[[i]][[names(args)[j]]][[index]] <- args[[j]][[i]] |
| 78 |
} |
|
| 79 |
} |
|
| 80 | ||
| 81 | 13x |
e |
| 82 |
} |
|
| 83 | ||
| 84 |
#' @name timeline-opts |
|
| 85 |
#' @export |
|
| 86 |
e_timeline_on_serie <- function(e, ..., serie_index) {
|
|
| 87 | 3x |
if (missing(e) || missing(serie_index)) {
|
| 88 | 1x |
stop("must pass e or serie_index", call. = FALSE)
|
| 89 |
} |
|
| 90 | ||
| 91 | 2x |
args <- list(...) |
| 92 | ||
| 93 | 2x |
if (length(args) == 0) {
|
| 94 | ! |
stop("no arguments passed", call. = FALSE)
|
| 95 |
} |
|
| 96 | ||
| 97 | 2x |
for (i in seq_along(e$x$opts$options)) {
|
| 98 | 6x |
for (j in seq_along(args)) {
|
| 99 | 9x |
if (!length(e$x$opts$options[[i]]$series[[serie_index]][[names(args)[j]]]) < serie_index) {
|
| 100 | ! |
e$x$opts$options[[i]]$series[[serie_index]][[names(args)[j]]] <- list() |
| 101 |
} |
|
| 102 | 9x |
e$x$opts$options[[i]]$series[[serie_index]][[names(args)[j]]] <- args[[j]][[i]] |
| 103 |
} |
|
| 104 |
} |
|
| 105 | ||
| 106 | 2x |
e |
| 107 |
} |
| 1 |
#' Leaflet |
|
| 2 |
#' |
|
| 3 |
#' Leaflet extension. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param roam Whether to allow the user to roam. |
|
| 7 |
#' @param template \code{urlTemplate}, should not be changed.
|
|
| 8 |
#' @param options List of options, including \code{attribution} and \code{label}.
|
|
| 9 |
#' |
|
| 10 |
#' @examples |
|
| 11 |
#' population |> |
|
| 12 |
#' dplyr::filter(value > 8) |> |
|
| 13 |
#' e_charts(lon) |> |
|
| 14 |
#' e_leaflet() |> |
|
| 15 |
#' e_leaflet_tile(options = list(maxZoom = 3)) |> |
|
| 16 |
#' e_scatter(lat, size = value, coord_system = "leaflet") |
|
| 17 |
#' |
|
| 18 |
#' @rdname leaflet |
|
| 19 |
#' @export |
|
| 20 |
e_leaflet <- function(e, roam = TRUE, ...) {
|
|
| 21 | 12x |
leaf <- list(...) |
| 22 | 12x |
leaf$roam <- roam |
| 23 | 12x |
leaf$tiles <- list() |
| 24 | ||
| 25 |
# remove axis |
|
| 26 | 12x |
e <- .rm_axis(e, TRUE, "x") |
| 27 | 12x |
e <- .rm_axis(e, TRUE, "y") |
| 28 | ||
| 29 | 12x |
e$x$opts$leaflet <- leaf |
| 30 | ||
| 31 |
# add dependency |
|
| 32 | 12x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0/plugins", package = "echarts4r")
|
| 33 | ||
| 34 | 12x |
dep <- htmltools::htmlDependency( |
| 35 | 12x |
name = "leaflet", |
| 36 | 12x |
version = "1.9.4", |
| 37 | 12x |
src = c(file = path), |
| 38 | 12x |
stylesheet = "leaflet.css", |
| 39 | 12x |
script = "leaflet.js" |
| 40 |
) |
|
| 41 | ||
| 42 | 12x |
dep2 <- htmltools::htmlDependency( |
| 43 | 12x |
name = "echarts-leaflet", |
| 44 | 12x |
version = "1.0.0", |
| 45 | 12x |
src = c(file = path), |
| 46 | 12x |
script = "echarts-leaflet.js" |
| 47 |
) |
|
| 48 | ||
| 49 | 12x |
e$dependencies <- append(e$dependencies, htmlwidgets::getDependency("leaflet"))
|
| 50 | 12x |
e$dependencies <- append(e$dependencies, list(dep, dep2)) |
| 51 | ||
| 52 | 12x |
e |
| 53 |
} |
|
| 54 | ||
| 55 |
#' @rdname leaflet |
|
| 56 |
#' @export |
|
| 57 |
e_leaflet_tile <- function( |
|
| 58 |
e, |
|
| 59 |
template = "https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png",
|
|
| 60 |
options = NULL, |
|
| 61 |
... |
|
| 62 |
) {
|
|
| 63 | 8x |
pkgs <- utils::installed.packages() |> rownames() |
| 64 | 8x |
if (!"leaflet" %in% pkgs) {
|
| 65 | 1x |
stop("Requires the `leaflet` package installed", call. = FALSE)
|
| 66 |
} |
|
| 67 | ||
| 68 | 7x |
if (!length(e$x$opts$leaflet$roam)) {
|
| 69 | 1x |
e <- e_leaflet(e) |
| 70 |
} |
|
| 71 | ||
| 72 | 7x |
tile <- list(...) |
| 73 | 7x |
tile$urlTemplate <- template |
| 74 | ||
| 75 | 7x |
if (!is.null(options)) {
|
| 76 | 2x |
tile$options <- options |
| 77 |
} |
|
| 78 | ||
| 79 | 7x |
e$x$opts$leaflet$tiles <- list(tile) |
| 80 | ||
| 81 | 7x |
e |
| 82 |
} |
| 1 |
#' Axis |
|
| 2 |
#' |
|
| 3 |
#' Customise axis. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param axis Axis to customise. |
|
| 7 |
#' @param serie Column name of serie to range the axis. If used the range of the serie is used as, |
|
| 8 |
#' \code{min} an \code{max}.
|
|
| 9 |
#' @param margin Margin to apply to \code{serie}: \eqn{min = serie - margin} and
|
|
| 10 |
#' \eqn{max = serie + margin}
|
|
| 11 |
#' @param index Index of axis to customise. |
|
| 12 |
#' @param formatter An axis formatter as returned by \code{\link{e_axis_formatter}}.
|
|
| 13 |
#' @param style Formatter style, one of \code{decimal}, \code{percent}, or \code{currency}.
|
|
| 14 |
#' @param currency Currency to to display. |
|
| 15 |
#' @param digits Number of decimals. |
|
| 16 |
#' @param locale Locale, if \code{NULL} then it is inferred from \code{Sys.getlocale}.
|
|
| 17 |
#' |
|
| 18 |
#' @section Functions: |
|
| 19 |
#' \itemize{
|
|
| 20 |
#' \item{\code{e_axis} to customise axis}
|
|
| 21 |
#' \item{\code{e_rm_axis} to remove axis}
|
|
| 22 |
#' } |
|
| 23 |
#' |
|
| 24 |
#' @details The \code{e_axis_formatter} may not work in RStudio,
|
|
| 25 |
#' open the plot in your browser. It will display just fine in |
|
| 26 |
#' Rmarkdown and Shiny. |
|
| 27 |
#' |
|
| 28 |
#' @examples |
|
| 29 |
#' # range axis based on serie |
|
| 30 |
#' cars |> |
|
| 31 |
#' e_charts(speed) |> |
|
| 32 |
#' e_line(dist) |> |
|
| 33 |
#' e_x_axis(speed) |> |
|
| 34 |
#' e_y_axis(dist) |
|
| 35 |
#' |
|
| 36 |
#' # use formatter |
|
| 37 |
#' cars |> |
|
| 38 |
#' dplyr::mutate( |
|
| 39 |
#' speed = speed / 25 |
|
| 40 |
#' ) |> |
|
| 41 |
#' e_charts(speed) |> |
|
| 42 |
#' e_scatter(dist) |> |
|
| 43 |
#' e_y_axis( |
|
| 44 |
#' formatter = e_axis_formatter("currency")
|
|
| 45 |
#' ) |> |
|
| 46 |
#' e_x_axis( |
|
| 47 |
#' formatter = e_axis_formatter("percent", digits = 0)
|
|
| 48 |
#' ) |
|
| 49 |
#' |
|
| 50 |
#' # plot all labels & rotate |
|
| 51 |
#' USArrests |> |
|
| 52 |
#' head(10) |> |
|
| 53 |
#' tibble::rownames_to_column(var = "State") |> |
|
| 54 |
#' e_charts(State) |> |
|
| 55 |
#' e_area(Murder) |> |
|
| 56 |
#' e_x_axis(axisLabel = list(interval = 0, rotate = 45)) # rotate |
|
| 57 |
#' @seealso \href{https://echarts.apache.org/en/option.html#xAxis}{Additional x arguments},
|
|
| 58 |
#' \href{https://echarts.apache.org/en/option.html#yAxis}{Additional y arguments}
|
|
| 59 |
#' |
|
| 60 |
#' @rdname axis |
|
| 61 |
#' @export |
|
| 62 |
e_axis <- function(e, serie, axis = c("x", "y", "z"), index = 0, formatter = NULL, margin = 0, ...) {
|
|
| 63 | 44x |
if (missing(serie)) {
|
| 64 | 43x |
serie <- NULL |
| 65 |
} else {
|
|
| 66 | 1x |
serie <- deparse(substitute(serie)) |
| 67 |
} |
|
| 68 | ||
| 69 | 44x |
e_axis_(e, serie = serie, axis = axis, index = index, formatter = formatter, margin = margin, ...) |
| 70 |
} |
|
| 71 | ||
| 72 |
#' @rdname axis |
|
| 73 |
#' @export |
|
| 74 |
e_axis_ <- function(e, serie = NULL, axis = c("x", "y", "z"), index = 0, formatter = NULL, margin = 0, ...) {
|
|
| 75 | 86x |
if (missing(e)) {
|
| 76 | 1x |
stop("must pass e", call. = FALSE)
|
| 77 |
} |
|
| 78 | ||
| 79 | 85x |
axis <- .r2axis(axis[1]) |
| 80 | ||
| 81 | 85x |
r.index <- index + 1 |
| 82 | 85x |
max <- length(e$x$opts[[axis]]) |
| 83 | ||
| 84 | 85x |
attrs <- list(...) |
| 85 | ||
| 86 | 85x |
if (!is.null(serie)) {
|
| 87 | 2x |
dat <- .get_data(e, serie) |
| 88 | 2x |
if (inherits(dat, "numeric") || inherits(dat, "integer")) {
|
| 89 | 2x |
rng <- range(dat) |
| 90 |
# If min or max is provided, that will replace the min/max in the data |
|
| 91 | 2x |
if(!is.null(attrs$min)){
|
| 92 | 1x |
rng[1] <- attrs$min |
| 93 |
} |
|
| 94 | 2x |
if(!is.null(attrs$max)){
|
| 95 | 1x |
rng[2] <- attrs$max |
| 96 |
} |
|
| 97 | ||
| 98 | 2x |
attrs$min <- rng[1] - margin |
| 99 | 2x |
attrs$max <- rng[2] + margin |
| 100 |
} |
|
| 101 |
} |
|
| 102 | ||
| 103 | 85x |
if (!is.null(formatter)) {
|
| 104 | 1x |
attrs$axisLabel$formatter <- formatter |
| 105 |
} |
|
| 106 | ||
| 107 | 85x |
if (!length(attrs)) {
|
| 108 | 1x |
stop("no attribute", call. = FALSE)
|
| 109 |
} |
|
| 110 | ||
| 111 |
# initiatlise if wrong index |
|
| 112 | 84x |
if (r.index > max) {
|
| 113 | 11x |
r.index <- 1 |
| 114 |
} |
|
| 115 | ||
| 116 | 84x |
if (!e$x$tl) {
|
| 117 | 78x |
dp <- .list_depth(e$x$opts[[axis]]) |
| 118 |
} else {
|
|
| 119 | 6x |
dp <- .list_depth(e$x$opts$baseOption[[axis]]) |
| 120 |
} |
|
| 121 | ||
| 122 | 84x |
if (dp >= 2) {
|
| 123 | 77x |
for (i in seq_along(attrs)) {
|
| 124 | 131x |
arg <- names(attrs)[i] |
| 125 | 131x |
if (!e$x$tl) {
|
| 126 | 125x |
e$x$opts[[axis]][[r.index]][[arg]] <- attrs[[i]] |
| 127 |
} else {
|
|
| 128 | 6x |
e$x$opts$baseOption[[axis]][[r.index]][[arg]] <- attrs[[i]] |
| 129 |
} |
|
| 130 |
} |
|
| 131 |
} else {
|
|
| 132 | 7x |
for (i in seq_along(attrs)) {
|
| 133 | 7x |
arg <- names(attrs)[i] |
| 134 | 7x |
if (!e$x$tl) {
|
| 135 | 6x |
e$x$opts[[axis]][[arg]] <- attrs[[i]] |
| 136 |
} else {
|
|
| 137 | 1x |
e$x$opts$baseOption[[axis]][[arg]] <- attrs[[i]] |
| 138 |
} |
|
| 139 |
} |
|
| 140 |
} |
|
| 141 | ||
| 142 | 84x |
e |
| 143 |
} |
|
| 144 | ||
| 145 |
#' @rdname axis |
|
| 146 |
#' @export |
|
| 147 |
e_x_axis_ <- function(e, serie = NULL, index = 0, formatter = NULL, margin = 0, ...) {
|
|
| 148 | 2x |
if (missing(e)) {
|
| 149 | 1x |
stop("must pass e", call. = FALSE)
|
| 150 |
} |
|
| 151 | 1x |
e_axis_(e, serie, "x", index, formatter, margin = margin, ...) |
| 152 |
} |
|
| 153 | ||
| 154 |
#' @rdname axis |
|
| 155 |
#' @export |
|
| 156 |
e_y_axis_ <- function(e, serie = NULL, index = 0, formatter = NULL, margin = 0, ...) {
|
|
| 157 | 2x |
if (missing(e)) {
|
| 158 | 1x |
stop("must pass e", call. = FALSE)
|
| 159 |
} |
|
| 160 | 1x |
e_axis_(e = e, serie = serie, axis = "y", index = index, formatter, margin = margin, ...) |
| 161 |
} |
|
| 162 | ||
| 163 |
#' @rdname axis |
|
| 164 |
#' @export |
|
| 165 |
e_z_axis_ <- function(e, serie = NULL, index = 0, margin = 0, ...) {
|
|
| 166 | 2x |
if (missing(e)) {
|
| 167 | 1x |
stop("must pass e", call. = FALSE)
|
| 168 |
} |
|
| 169 | 1x |
e_axis_(e = e, serie = serie, axis = "z", index = index, margin = margin, ...) |
| 170 |
} |
|
| 171 | ||
| 172 | ||
| 173 |
#' @rdname axis |
|
| 174 |
#' @export |
|
| 175 |
e_x_axis <- function(e, serie, index = 0, formatter = NULL, margin = 0, ...) {
|
|
| 176 | 18x |
if (missing(e)) {
|
| 177 | 1x |
stop("must pass e", call. = FALSE)
|
| 178 |
} |
|
| 179 | ||
| 180 | 17x |
if (missing(serie)) {
|
| 181 | 17x |
serie <- NULL |
| 182 |
} else {
|
|
| 183 | ! |
serie <- deparse(substitute(serie)) |
| 184 |
} |
|
| 185 | ||
| 186 | 17x |
e_axis_(e, serie, "x", index, formatter, margin = margin, ...) |
| 187 |
} |
|
| 188 | ||
| 189 |
#' @rdname axis |
|
| 190 |
#' @export |
|
| 191 |
e_y_axis <- function(e, serie, index = 0, formatter = NULL, margin = 0, ...) {
|
|
| 192 | 14x |
if (missing(e)) {
|
| 193 | 1x |
stop("must pass e", call. = FALSE)
|
| 194 |
} |
|
| 195 | ||
| 196 | 13x |
if (missing(serie)) {
|
| 197 | 13x |
serie <- NULL |
| 198 |
} else {
|
|
| 199 | ! |
serie <- deparse(substitute(serie)) |
| 200 |
} |
|
| 201 | ||
| 202 | 13x |
e_axis_(e = e, serie = serie, axis = "y", index = index, formatter, margin = margin, ...) |
| 203 |
} |
|
| 204 | ||
| 205 |
#' @rdname axis |
|
| 206 |
#' @export |
|
| 207 |
e_z_axis <- function(e, serie, index = 0, margin = 0, ...) {
|
|
| 208 | 2x |
if (missing(e)) {
|
| 209 | 1x |
stop("must pass e", call. = FALSE)
|
| 210 |
} |
|
| 211 | ||
| 212 | 1x |
if (missing(serie)) {
|
| 213 | 1x |
serie <- NULL |
| 214 |
} else {
|
|
| 215 | ! |
serie <- deparse(substitute(serie)) |
| 216 |
} |
|
| 217 | ||
| 218 | 1x |
e_axis_(e = e, serie = serie, axis = "z", index = index, margin = margin, ...) |
| 219 |
} |
|
| 220 | ||
| 221 |
#' @rdname axis |
|
| 222 |
#' @export |
|
| 223 |
e_rm_axis <- function(e, axis = c("x", "y", "z")) {
|
|
| 224 | 1x |
match.arg(axis, c("x", "y", "z"))
|
| 225 | 1x |
axis <- .r2axis(axis[1]) |
| 226 | ||
| 227 | 1x |
e$x$opts[[axis]] <- NULL |
| 228 | ||
| 229 | 1x |
return(e) |
| 230 |
} |
|
| 231 | ||
| 232 |
#' @rdname axis |
|
| 233 |
#' @export |
|
| 234 |
e_axis_formatter <- function( |
|
| 235 |
style = c("decimal", "percent", "currency"),
|
|
| 236 |
digits = 0, |
|
| 237 |
locale = NULL, |
|
| 238 |
currency = "USD" |
|
| 239 |
) {
|
|
| 240 | 8x |
if (rstudioapi::isAvailable()) {
|
| 241 | 1x |
warning("`e_axis_formatter` breaks the plot in RStudio, open it in your browser.", call. = FALSE)
|
| 242 |
} |
|
| 243 | ||
| 244 | 8x |
if (is.null(locale)) {
|
| 245 | 7x |
locale <- .get_locale() |
| 246 |
} |
|
| 247 | ||
| 248 | 8x |
style <- match.arg(style) |
| 249 | 8x |
opts <- list( |
| 250 | 8x |
style = style, |
| 251 | 8x |
minimumFractionDigits = digits, |
| 252 | 8x |
maximumFractionDigits = digits, |
| 253 | 8x |
currency = currency |
| 254 |
) |
|
| 255 | 8x |
htmlwidgets::JS(sprintf("function(value, index) {
|
| 256 | 8x |
var fmt = new Intl.NumberFormat('%s', %s);
|
| 257 | 8x |
return fmt.format(value); |
| 258 | 8x |
}", locale, jsonlite::toJSON(opts, auto_unbox = TRUE))) |
| 259 |
} |
|
| 260 | ||
| 261 |
#' Grid |
|
| 262 |
#' |
|
| 263 |
#' Customise grid. |
|
| 264 |
#' |
|
| 265 |
#' @inheritParams e_bar |
|
| 266 |
#' @inheritParams e_axis |
|
| 267 |
#' |
|
| 268 |
#' @examples |
|
| 269 |
#' USArrests |> |
|
| 270 |
#' e_charts(UrbanPop) |> |
|
| 271 |
#' e_line(Assault, smooth = TRUE) |> |
|
| 272 |
#' e_area(Murder, y_index = 1, x_index = 1) |> |
|
| 273 |
#' e_y_axis(gridIndex = 1) |> |
|
| 274 |
#' e_x_axis(gridIndex = 1) |> |
|
| 275 |
#' e_grid(height = "40%") |> |
|
| 276 |
#' e_grid(height = "40%", top = "55%") |
|
| 277 |
#' @seealso \href{https://echarts.apache.org/en/option.html#grid}{Additional arguments}
|
|
| 278 |
#' |
|
| 279 |
#' @export |
|
| 280 |
e_grid <- function(e, index = NULL, ...) {
|
|
| 281 | 28x |
if (missing(e)) {
|
| 282 | 1x |
stop("must pass e", call. = FALSE)
|
| 283 |
} |
|
| 284 | ||
| 285 | 27x |
if (is.null(index)) {
|
| 286 | 27x |
index <- length(e$x$opts[["grid"]]) + 1 |
| 287 |
} |
|
| 288 | ||
| 289 | 27x |
attrs <- list(...) |
| 290 | ||
| 291 |
# initialise of not existing |
|
| 292 | 27x |
if (!e$x$tl) {
|
| 293 | 27x |
if (!length(e$x$opts[["grid"]])) {
|
| 294 | 11x |
e$x$opts$grid <- list() |
| 295 |
} |
|
| 296 | ||
| 297 | 27x |
e$x$opts$grid <- append(e$x$opts$grid, list(attrs)) |
| 298 |
} else {
|
|
| 299 | ! |
if (!length(e$x$opts$baseOption[["grid"]])) {
|
| 300 | ! |
e$x$opts$baseOption$grid <- list() |
| 301 |
} |
|
| 302 | ||
| 303 | ! |
e$x$opts$baseOption$grid <- append(e$x$opts$baseOption$grid, list(attrs)) |
| 304 |
} |
|
| 305 | ||
| 306 | 27x |
e |
| 307 |
} |
|
| 308 | ||
| 309 |
#' Radius axis |
|
| 310 |
#' |
|
| 311 |
#' Customise radius axis. |
|
| 312 |
#' |
|
| 313 |
#' @inheritParams e_bar |
|
| 314 |
#' @param serie Serie to use as axis labels. |
|
| 315 |
#' @param show Whether to display the axis. |
|
| 316 |
#' |
|
| 317 |
#' @examples |
|
| 318 |
#' df <- data.frame(x = LETTERS[1:10], y = seq(1, 20, by = 2)) |
|
| 319 |
#' |
|
| 320 |
#' df |> |
|
| 321 |
#' e_charts(x) |> |
|
| 322 |
#' e_polar() |> |
|
| 323 |
#' e_angle_axis() |> |
|
| 324 |
#' e_radius_axis(x) |> |
|
| 325 |
#' e_bar(y, coord_system = "polar") |
|
| 326 |
#' @seealso \href{https://echarts.apache.org/en/option.html#radiusAxis}{Additional arguments}
|
|
| 327 |
#' |
|
| 328 |
#' @export |
|
| 329 |
#' @name radius_axis |
|
| 330 |
e_radius_axis <- function(e, serie, show = TRUE, ...) {
|
|
| 331 | 10x |
if (missing(e)) {
|
| 332 | 1x |
stop("must pass e", call. = FALSE)
|
| 333 |
} |
|
| 334 | ||
| 335 | 9x |
opts <- list(show = show, ...) |
| 336 | ||
| 337 | 9x |
if (!missing(serie)) {
|
| 338 | 1x |
sr <- deparse(substitute(serie)) |
| 339 |
} |
|
| 340 | ||
| 341 | 9x |
if (!missing(serie)) {
|
| 342 | 1x |
opts$data <- e$x$data |> |
| 343 | 1x |
purrr::map(sr) |> |
| 344 | 1x |
unlist() |> |
| 345 | 1x |
unname() |> |
| 346 | 1x |
unique() |> |
| 347 | 1x |
as.list() |
| 348 |
} |
|
| 349 | ||
| 350 | 9x |
if (!e$x$tl) {
|
| 351 | 8x |
e$x$opts$radiusAxis <- opts |
| 352 |
} else {
|
|
| 353 | 1x |
e$x$opts$baseOption$radiusAxis <- opts |
| 354 |
} |
|
| 355 | ||
| 356 | 9x |
e |
| 357 |
} |
|
| 358 | ||
| 359 |
#' @rdname radius_axis |
|
| 360 |
#' @export |
|
| 361 |
e_radius_axis_ <- function(e, serie = NULL, show = TRUE, ...) {
|
|
| 362 | 3x |
if (missing(e)) {
|
| 363 | 1x |
stop("must pass e", call. = FALSE)
|
| 364 |
} |
|
| 365 | ||
| 366 | 2x |
opts <- list(show = show, ...) |
| 367 | ||
| 368 | 2x |
if (!is.null(serie)) {
|
| 369 | 1x |
opts$data <- e$x$data |> |
| 370 | 1x |
purrr::map(serie) |> |
| 371 | 1x |
unlist() |> |
| 372 | 1x |
unname() |> |
| 373 | 1x |
unique() |> |
| 374 | 1x |
as.list() |
| 375 |
} |
|
| 376 | ||
| 377 | 2x |
if (!e$x$tl) {
|
| 378 | 2x |
e$x$opts$radiusAxis <- opts |
| 379 |
} else {
|
|
| 380 | ! |
e$x$opts$baseOption$radiusAxis <- opts |
| 381 |
} |
|
| 382 | ||
| 383 | 2x |
e |
| 384 |
} |
|
| 385 | ||
| 386 |
#' Angle axis |
|
| 387 |
#' |
|
| 388 |
#' Customise angle axis. |
|
| 389 |
#' |
|
| 390 |
#' @inheritParams e_bar |
|
| 391 |
#' @param serie Serie to use as axis labels. |
|
| 392 |
#' @param show Whether to display the axis. |
|
| 393 |
#' |
|
| 394 |
#' @examples |
|
| 395 |
#' df <- data.frame(x = 1:100, y = seq(1, 200, by = 2)) |
|
| 396 |
#' |
|
| 397 |
#' df |> |
|
| 398 |
#' e_charts(x) |> |
|
| 399 |
#' e_polar(FALSE) |> |
|
| 400 |
#' e_angle_axis(FALSE) |> |
|
| 401 |
#' e_radius_axis(FALSE) |> |
|
| 402 |
#' e_line(y, coord_system = "polar", smooth = TRUE) |> |
|
| 403 |
#' e_legend(show = FALSE) |
|
| 404 |
#' |
|
| 405 |
#' df <- data.frame(x = LETTERS[1:5], y = runif(5)) |
|
| 406 |
#' |
|
| 407 |
#' df |> |
|
| 408 |
#' e_charts(x) |> |
|
| 409 |
#' e_polar() |> |
|
| 410 |
#' e_angle_axis(x) |> |
|
| 411 |
#' e_radius_axis() |> |
|
| 412 |
#' e_line(y, coord_system = "polar", smooth = TRUE) |
|
| 413 |
#' @seealso \href{https://echarts.apache.org/en/option.html#angleAxis}{Additional arguments}
|
|
| 414 |
#' |
|
| 415 |
#' @name angle_axis |
|
| 416 |
#' @export |
|
| 417 |
e_angle_axis <- function(e, serie, show = TRUE, ...) {
|
|
| 418 | 10x |
if (missing(e)) {
|
| 419 | 1x |
stop("must pass e", call. = FALSE)
|
| 420 |
} |
|
| 421 | ||
| 422 | 9x |
opts <- list(show = show, ...) |
| 423 | ||
| 424 | 9x |
if (!missing(serie)) {
|
| 425 | 8x |
sr <- deparse(substitute(serie)) |
| 426 |
} |
|
| 427 | ||
| 428 | 9x |
if (!missing(serie)) {
|
| 429 | 8x |
opts$data <- e$x$data |> |
| 430 | 8x |
purrr::map(sr) |> |
| 431 | 8x |
unlist() |> |
| 432 | 8x |
unname() |> |
| 433 | 8x |
unique() |> |
| 434 | 8x |
as.list() |
| 435 |
} |
|
| 436 | ||
| 437 | 9x |
if (!e$x$tl) {
|
| 438 | 8x |
e$x$opts$angleAxis <- opts |
| 439 |
} else {
|
|
| 440 | 1x |
e$x$opts$baseOption$angleAxis <- opts |
| 441 |
} |
|
| 442 | ||
| 443 | 9x |
e |
| 444 |
} |
|
| 445 | ||
| 446 |
#' @rdname angle_axis |
|
| 447 |
#' @export |
|
| 448 |
e_angle_axis_ <- function(e, serie = NULL, show = TRUE, ...) {
|
|
| 449 | 3x |
if (missing(e)) {
|
| 450 | 1x |
stop("must pass e", call. = FALSE)
|
| 451 |
} |
|
| 452 | ||
| 453 | 2x |
opts <- list(show = show, ...) |
| 454 | ||
| 455 | 2x |
if (!is.null(serie)) {
|
| 456 | 1x |
opts$data <- e$x$data |> |
| 457 | 1x |
purrr::map(serie) |> |
| 458 | 1x |
unlist() |> |
| 459 | 1x |
unname() |> |
| 460 | 1x |
unique() |> |
| 461 | 1x |
as.list() |
| 462 |
} |
|
| 463 | ||
| 464 | 2x |
if (!e$x$tl) {
|
| 465 | 2x |
e$x$opts$angleAxis <- opts |
| 466 |
} else {
|
|
| 467 | ! |
e$x$opts$baseOption$angleAxis <- opts |
| 468 |
} |
|
| 469 | ||
| 470 | 2x |
e |
| 471 |
} |
|
| 472 | ||
| 473 |
#' Radar axis |
|
| 474 |
#' |
|
| 475 |
#' Radar axis setup and options. |
|
| 476 |
#' |
|
| 477 |
#' @inheritParams e_bar |
|
| 478 |
#' @param index Index of axis to customise. |
|
| 479 |
#' |
|
| 480 |
#' @examples |
|
| 481 |
#' df <- data.frame( |
|
| 482 |
#' x = LETTERS[1:5], |
|
| 483 |
#' y = runif(5, 1, 5), |
|
| 484 |
#' z = runif(5, 3, 7) |
|
| 485 |
#' ) |
|
| 486 |
#' |
|
| 487 |
#' df |> |
|
| 488 |
#' e_charts(x) |> |
|
| 489 |
#' e_radar(y, max = 7) |> |
|
| 490 |
#' e_radar(z) |> |
|
| 491 |
#' e_radar_opts(center = c("25%", "25%")) |>
|
|
| 492 |
#' e_tooltip(trigger = "item") |
|
| 493 |
#' @export |
|
| 494 |
e_radar_opts <- function(e, index = 0, ...) {
|
|
| 495 | 6x |
if (missing(e)) {
|
| 496 | 1x |
stop("must pass e", call. = FALSE)
|
| 497 |
} |
|
| 498 | ||
| 499 | 5x |
r.index <- index + 1 |
| 500 | 5x |
max <- length(e$x$opts$radar) |
| 501 | ||
| 502 | 5x |
attrs <- list(...) |
| 503 | ||
| 504 | 5x |
if (!length(attrs)) {
|
| 505 | 1x |
stop("no attribute", call. = FALSE)
|
| 506 |
} |
|
| 507 | ||
| 508 |
# initiatlise if wrong index |
|
| 509 | 4x |
if (r.index > max) {
|
| 510 | 1x |
r.index <- 1 |
| 511 |
# This part seemed to break the plot |
|
| 512 |
# if (!e$x$tl) {
|
|
| 513 |
# e$x$opts$radar <- list(list()) |
|
| 514 |
# } else {
|
|
| 515 |
# e$x$opts$baseOption$radar <- list(list()) |
|
| 516 |
# } |
|
| 517 |
} |
|
| 518 | ||
| 519 | 4x |
for (i in seq_along(attrs)) {
|
| 520 | 6x |
arg <- names(attrs)[i] |
| 521 | 6x |
if (!e$x$tl) {
|
| 522 | 6x |
e$x$opts$radar[[r.index]][[arg]] <- attrs[[i]] |
| 523 |
} else {
|
|
| 524 | ! |
e$x$opts$baseOption$radar[[r.index]][[arg]] <- attrs[[i]] |
| 525 |
} |
|
| 526 |
} |
|
| 527 | ||
| 528 | 4x |
e |
| 529 |
} |
|
| 530 | ||
| 531 |
#' Single Axis |
|
| 532 |
#' |
|
| 533 |
#' Setup single axis. |
|
| 534 |
#' |
|
| 535 |
#' @inheritParams e_bar |
|
| 536 |
#' @param index Index of axis to customise. |
|
| 537 |
#' |
|
| 538 |
#' @examples |
|
| 539 |
#' df <- data.frame( |
|
| 540 |
#' axis = LETTERS[1:10], |
|
| 541 |
#' value = runif(10, 3, 20), |
|
| 542 |
#' size = runif(10, 3, 20) |
|
| 543 |
#' ) |
|
| 544 |
#' |
|
| 545 |
#' df |> |
|
| 546 |
#' e_charts(axis) |> |
|
| 547 |
#' e_single_axis() |> # add the single axis |
|
| 548 |
#' e_scatter( |
|
| 549 |
#' value, |
|
| 550 |
#' size, |
|
| 551 |
#' coord_system = "singleAxis" |
|
| 552 |
#' ) |
|
| 553 |
#' @export |
|
| 554 |
e_single_axis <- function(e, index = 0, ...) {
|
|
| 555 | 11x |
if (missing(e)) {
|
| 556 | 1x |
stop("must pass e", call. = FALSE)
|
| 557 |
} |
|
| 558 | ||
| 559 | 10x |
r.index <- index + 1 |
| 560 | ||
| 561 | 10x |
e$x$opts$xAxis <- NULL |
| 562 | 10x |
e$x$opts$yAxis <- NULL |
| 563 | ||
| 564 | 10x |
if (!e$x$tl) {
|
| 565 | 9x |
if (!length(e$x$opts$singleAxis)) {
|
| 566 | 8x |
e$x$opts$singleAxis <- list(...) |
| 567 |
} else {
|
|
| 568 | 1x |
e$x$opts$singleAxis <- append(e$x$opts$singleAxis, list(...)) |
| 569 |
} |
|
| 570 |
} else {
|
|
| 571 | 1x |
if (!length(e$x$opts$baseOption$singleAxis)) {
|
| 572 | 1x |
e$x$opts$baseOption$singleAxis <- list(...) |
| 573 |
} else {
|
|
| 574 | ! |
e$x$opts$baseOption$singleAxis <- append(e$x$opts$baseOption$singleAxis, list(...)) |
| 575 |
} |
|
| 576 |
} |
|
| 577 | ||
| 578 | 10x |
if (!is.null(e$x$mapping$x)) {
|
| 579 | 9x |
type <- .get_type(e, e$x$mapping$x) |
| 580 | ||
| 581 | 9x |
if (!e$x$tl) {
|
| 582 | 8x |
e$x$opts$singleAxis$type <- type |
| 583 |
} else {
|
|
| 584 | 1x |
e$x$opts$baseOption$singleAxis$type <- type |
| 585 |
} |
|
| 586 | ||
| 587 | 9x |
if (type == "category" || type == "time") {
|
| 588 | 8x |
vect <- c() |
| 589 | 8x |
for (i in seq_along(e$x$data)) {
|
| 590 | 9x |
dat <- e$x$data[[i]] |> |
| 591 | 9x |
dplyr::select(e$x$mapping$x) |> |
| 592 | 9x |
unlist() |
| 593 | 9x |
dat <- unname(dat) |
| 594 | 9x |
dat <- as.character(dat) |
| 595 | ||
| 596 | 9x |
vect <- append(vect, dat) |
| 597 |
} |
|
| 598 | ||
| 599 | 8x |
vect <- unique(vect) |
| 600 | ||
| 601 | 8x |
if (!e$x$tl) {
|
| 602 | 7x |
e$x$opts$singleAxis$data <- vect |
| 603 |
} else {
|
|
| 604 | 1x |
e$x$opts$baseOption$singleAxis$data <- vect |
| 605 |
} |
|
| 606 |
} |
|
| 607 |
} else {
|
|
| 608 | 1x |
warning("x not pass to e_charts", call. = FALSE)
|
| 609 |
} |
|
| 610 | ||
| 611 | 10x |
e |
| 612 |
} |
|
| 613 | ||
| 614 |
#' Axis Labels |
|
| 615 |
#' |
|
| 616 |
#' Convenience function to add axis labels. |
|
| 617 |
#' |
|
| 618 |
#' @inheritParams e_bar |
|
| 619 |
#' @param x,y Labels of axes. |
|
| 620 |
#' |
|
| 621 |
#' @examples |
|
| 622 |
#' cars |> |
|
| 623 |
#' e_charts(speed) |> |
|
| 624 |
#' e_scatter(dist) |> |
|
| 625 |
#' e_axis_labels( |
|
| 626 |
#' x = "speed", |
|
| 627 |
#' y = "distance" |
|
| 628 |
#' ) |
|
| 629 |
#' @export |
|
| 630 |
e_axis_labels <- function(e, x = "", y = "") {
|
|
| 631 | 2x |
if (missing(e)) {
|
| 632 | 1x |
stop("must pass e", call. = FALSE)
|
| 633 |
} |
|
| 634 | 1x |
e |> |
| 635 | 1x |
e_x_axis(name = x) |> |
| 636 | 1x |
e_y_axis(name = y) |
| 637 |
} |
|
| 638 | ||
| 639 |
#'' Hide Grid Lines |
|
| 640 |
#' |
|
| 641 |
#' A convenience function to easily hide grid lines. |
|
| 642 |
#' |
|
| 643 |
#' @inheritParams e_bar |
|
| 644 |
#' @param which Which axis grid lines to hide. |
|
| 645 |
#' |
|
| 646 |
#' @examples |
|
| 647 |
#' cars |> |
|
| 648 |
#' e_charts(speed) |> |
|
| 649 |
#' e_scatter(dist) |> |
|
| 650 |
#' e_hide_grid_lines() |
|
| 651 |
#' @export |
|
| 652 |
e_hide_grid_lines <- function(e, which = c("x", "y")) {
|
|
| 653 | 6x |
if (missing(e)) {
|
| 654 | 1x |
stop("must pass e", call. = FALSE)
|
| 655 |
} |
|
| 656 | 5x |
if ("x" %in% which) {
|
| 657 | 4x |
if (!e$x$tl) {
|
| 658 | 3x |
e$x$opts[["xAxis"]][[1]]$splitLine$show <- FALSE |
| 659 |
} else {
|
|
| 660 | 1x |
e$x$opts$baseOption[["xAxis"]][[1]]$splitLine$show <- FALSE |
| 661 |
} |
|
| 662 |
} |
|
| 663 | ||
| 664 | 5x |
if ("y" %in% which) {
|
| 665 | 4x |
if (!e$x$tl) {
|
| 666 | 3x |
e$x$opts[["yAxis"]][[1]]$splitLine$show <- FALSE |
| 667 |
} else {
|
|
| 668 | 1x |
e$x$opts$baseOption[["yAxis"]][[1]]$splitLine$show <- FALSE |
| 669 |
} |
|
| 670 |
} |
|
| 671 | ||
| 672 | 5x |
return(e) |
| 673 |
} |
|
| 674 | ||
| 675 |
#' Stagger Axis Labels |
|
| 676 |
#' |
|
| 677 |
#' Stagger axis labels. |
|
| 678 |
#' |
|
| 679 |
#' @inheritParams e_bar |
|
| 680 |
#' |
|
| 681 |
#' @examples |
|
| 682 |
#' df <- data.frame( |
|
| 683 |
#' x = c("a very long label", "Another long label"),
|
|
| 684 |
#' y = 1:2 |
|
| 685 |
#' ) |
|
| 686 |
#' |
|
| 687 |
#' df |> |
|
| 688 |
#' e_charts(x, width = 150) |> |
|
| 689 |
#' e_bar(y) |> |
|
| 690 |
#' e_axis_stagger() |
|
| 691 |
#' @export |
|
| 692 |
e_axis_stagger <- function(e) {
|
|
| 693 | 4x |
if (missing(e)) {
|
| 694 | 1x |
stop("must pass e", call. = FALSE)
|
| 695 |
} |
|
| 696 | 3x |
form <- "function(value, index){
|
| 697 | 3x |
if(index % 2){
|
| 698 | 3x |
return('\\n' + value)
|
| 699 |
} |
|
| 700 | ||
| 701 | 3x |
return(value) |
| 702 |
}" |> |
|
| 703 | 3x |
htmlwidgets::JS() |
| 704 | ||
| 705 | 3x |
if (!e$x$tl) {
|
| 706 | 2x |
e$x$opts[["xAxis"]][[1]]$axisLabel$formatter <- form |
| 707 |
} else {
|
|
| 708 | 1x |
e$x$opts$baseOption[["xAxis"]][[1]]$axisLabel$formatter <- form |
| 709 |
} |
|
| 710 | ||
| 711 | 3x |
return(e) |
| 712 |
} |
| 1 |
#' Country names |
|
| 2 |
#' |
|
| 3 |
#' Convert country names to echarts format. |
|
| 4 |
#' |
|
| 5 |
#' @param data Data.frame in which to find column names. |
|
| 6 |
#' @param input,output Input and output columns. |
|
| 7 |
#' @param type Passed to \link[countrycode]{countrycode} \code{origin} parameter.
|
|
| 8 |
#' @param ... Any other parameter to pass to \link[countrycode]{countrycode}.
|
|
| 9 |
#' |
|
| 10 |
#' @details Taiwan and Hong Kong cannot be plotted. |
|
| 11 |
#' |
|
| 12 |
#' @examples |
|
| 13 |
#' cns <- data.frame(country = c("US", "BE"))
|
|
| 14 |
#' |
|
| 15 |
#' # replace |
|
| 16 |
#' e_country_names(cns, country) |
|
| 17 |
#' |
|
| 18 |
#' # specify output |
|
| 19 |
#' e_country_names(cns, country, country_name) |
|
| 20 |
#' @rdname e_country_names |
|
| 21 |
#' @export |
|
| 22 |
e_country_names <- function(data, input, output, type = "iso2c", ...) {
|
|
| 23 | 3x |
if (missing(data) || missing(input)) {
|
| 24 | 1x |
stop("must pass data and input", call. = FALSE)
|
| 25 |
} |
|
| 26 | ||
| 27 | 2x |
if (missing(output)) {
|
| 28 | 1x |
output <- NULL |
| 29 |
} else {
|
|
| 30 | 1x |
output <- deparse(substitute(output)) |
| 31 |
} |
|
| 32 | ||
| 33 | 2x |
e_country_names_(data, deparse(substitute(input)), output, type, ...) |
| 34 |
} |
|
| 35 | ||
| 36 |
#' @rdname e_country_names |
|
| 37 |
#' @export |
|
| 38 |
e_country_names_ <- function(data, input, output = NULL, type = "iso2c", ...) {
|
|
| 39 | 4x |
if (missing(data) || missing(input)) {
|
| 40 | 1x |
stop("must pass data and input", call. = FALSE)
|
| 41 |
} |
|
| 42 | ||
| 43 | 3x |
src <- input |
| 44 | 3x |
cn <- countrycode::countrycode(data[[src]], origin = type, destination = "country.name", ...) |
| 45 | ||
| 46 | 3x |
if (is.null(output)) {
|
| 47 | 2x |
output <- src |
| 48 |
} |
|
| 49 | ||
| 50 | 3x |
data[[output]] <- .correct_countries(cn) |
| 51 | 3x |
data |
| 52 |
} |
|
| 53 | ||
| 54 |
#' Color range |
|
| 55 |
#' |
|
| 56 |
#' Build manual color range |
|
| 57 |
#' |
|
| 58 |
#' @param data Data.frame in which to find column names. |
|
| 59 |
#' @param input,output Input and output columns. |
|
| 60 |
#' @param colors Colors to pass to \code{\link{colorRampPalette}}.
|
|
| 61 |
#' @param ... Any other argument to pass to \code{\link{colorRampPalette}}.
|
|
| 62 |
#' |
|
| 63 |
#' @examples |
|
| 64 |
#' df <- data.frame(val = 1:10) |
|
| 65 |
#' |
|
| 66 |
#' e_color_range(df, val, colors) |
|
| 67 |
#' @rdname e_color_range |
|
| 68 |
#' @export |
|
| 69 |
e_color_range <- function(data, input, output, colors = c("#bf444c", "#d88273", "#f6efa6"), ...) {
|
|
| 70 | 4x |
if (missing(data) || missing(input) || missing(output)) {
|
| 71 | 1x |
stop("must pass data, input and output", call. = FALSE)
|
| 72 |
} |
|
| 73 | ||
| 74 | 3x |
e_color_range_(data, deparse(substitute(input)), deparse(substitute(output)), colors, ...) |
| 75 |
} |
|
| 76 | ||
| 77 |
#' @rdname e_color_range |
|
| 78 |
#' @export |
|
| 79 |
e_color_range_ <- function(data, input, output, colors = c("#bf444c", "#d88273", "#f6efa6"), ...) {
|
|
| 80 | 4x |
if (missing(data) || missing(input) || missing(output)) {
|
| 81 | 1x |
stop("must pass data, input and output", call. = FALSE)
|
| 82 |
} |
|
| 83 | ||
| 84 | 3x |
serie <- data[[input]] |
| 85 | 3x |
if (inherits(serie, "factor") || inherits(serie, "character")) {
|
| 86 | 1x |
col <- scales::col_factor(colors, domain = (serie))(serie) |
| 87 |
} else {
|
|
| 88 | 2x |
col <- scales::col_numeric(colors, domain = range(serie))(serie) |
| 89 |
} |
|
| 90 | ||
| 91 | 3x |
data[[output]] <- col |
| 92 | 3x |
data |
| 93 |
} |
|
| 94 | ||
| 95 |
#' Get data |
|
| 96 |
#' |
|
| 97 |
#' Get data passed to \code{\link{e_charts}}.
|
|
| 98 |
#' |
|
| 99 |
#' @inheritParams e_bar |
|
| 100 |
#' |
|
| 101 |
#' @return A list of data.frames, one for each group. |
|
| 102 |
#' |
|
| 103 |
#' @examples |
|
| 104 |
#' echart <- cars |> |
|
| 105 |
#' e_charts(speed) |> |
|
| 106 |
#' e_scatter(dist) |> |
|
| 107 |
#' e_lm(dist ~ speed) |
|
| 108 |
#' |
|
| 109 |
#' echart |
|
| 110 |
#' |
|
| 111 |
#' e_get_data(echart)[[1]] |
|
| 112 |
#' @export |
|
| 113 |
e_get_data <- function(e) {
|
|
| 114 | 1x |
e$x$data |
| 115 |
} |
|
| 116 | ||
| 117 |
#' Formatters |
|
| 118 |
#' |
|
| 119 |
#' Simple formatters as helpers. |
|
| 120 |
#' |
|
| 121 |
#' @inheritParams e_bar |
|
| 122 |
#' @param axis Axis to apply formatter to. |
|
| 123 |
#' @param suffix,prefix Suffix and prefix of label. |
|
| 124 |
#' @param ... Any other arguments to pass to \code{\link{e_axis}}.
|
|
| 125 |
#' |
|
| 126 |
#' @examples |
|
| 127 |
#' # Y = % |
|
| 128 |
#' df <- data.frame( |
|
| 129 |
#' x = 1:10, |
|
| 130 |
#' y = round( |
|
| 131 |
#' runif(10, 1, 100), |
|
| 132 |
#' 2 |
|
| 133 |
#' ) |
|
| 134 |
#' ) |
|
| 135 |
#' |
|
| 136 |
#' df |> |
|
| 137 |
#' e_charts(x) |> |
|
| 138 |
#' e_line(y) |> |
|
| 139 |
#' e_format_y_axis(suffix = "%") |> |
|
| 140 |
#' e_format_x_axis(prefix = "A") |
|
| 141 |
#' @rdname formatters |
|
| 142 |
#' @export |
|
| 143 |
e_format_axis <- function(e, axis = "y", suffix = NULL, prefix = NULL, ...) {
|
|
| 144 | 4x |
if (missing(e)) {
|
| 145 | 1x |
stop("must pass e", call. = FALSE)
|
| 146 |
} |
|
| 147 | ||
| 148 | 3x |
if (is.null(suffix) && is.null(prefix)) {
|
| 149 | 1x |
stop("missing formatting")
|
| 150 |
} |
|
| 151 | ||
| 152 | 2x |
fmt <- paste(prefix, "{value}", suffix)
|
| 153 | ||
| 154 | 2x |
e <- e |> |
| 155 | 2x |
e_axis( |
| 156 | 2x |
axis = axis, |
| 157 | 2x |
axisLabel = list(formatter = fmt), |
| 158 |
... |
|
| 159 |
) |
|
| 160 | ||
| 161 | 2x |
e |
| 162 |
} |
|
| 163 | ||
| 164 |
#' @rdname formatters |
|
| 165 |
#' @export |
|
| 166 |
e_format_x_axis <- function(e, suffix = NULL, prefix = NULL, ...) {
|
|
| 167 | 1x |
e_format_axis(e, "x", suffix, prefix, ...) |
| 168 |
} |
|
| 169 | ||
| 170 |
#' @rdname formatters |
|
| 171 |
#' @export |
|
| 172 |
e_format_y_axis <- function(e, suffix = NULL, prefix = NULL, ...) {
|
|
| 173 | 1x |
e_format_axis(e, "y", suffix, prefix, ...) |
| 174 |
} |
|
| 175 | ||
| 176 |
#' Format labels |
|
| 177 |
#' |
|
| 178 |
#' @inheritParams e_bar |
|
| 179 |
#' @param show Set to \code{TRUE} to show the labels.
|
|
| 180 |
#' @param position Position of labels, see |
|
| 181 |
#' \href{https://echarts.apache.org/en/option.html#series-line.label.position}{official documentation}
|
|
| 182 |
#' for the full list of options. |
|
| 183 |
#' @param ... Any other options see |
|
| 184 |
#' \href{https://echarts.apache.org/en/option.html#series-line.label}{documentation} for other options.
|
|
| 185 |
#' |
|
| 186 |
#' @examples |
|
| 187 |
#' mtcars |> |
|
| 188 |
#' e_chart(wt) |> |
|
| 189 |
#' e_scatter(qsec, cyl) |> |
|
| 190 |
#' e_labels(fontSize = 9) |
|
| 191 |
#' |
|
| 192 |
#' mtcars |> |
|
| 193 |
#' group_by(cyl) |> |
|
| 194 |
#' e_chart(wt) |> |
|
| 195 |
#' e_scatter(qsec, mpg) |> |
|
| 196 |
#' e_labels(fontSize = 9) |
|
| 197 |
#' |
|
| 198 |
#' # timeline |
|
| 199 |
#' mtcars |> |
|
| 200 |
#' group_by(cyl) |> |
|
| 201 |
#' e_chart(wt, timeline = TRUE) |> |
|
| 202 |
#' e_scatter(qsec, mpg) |> |
|
| 203 |
#' e_labels(fontSize = 9) |
|
| 204 |
#' @export |
|
| 205 |
e_labels <- function(e, show = TRUE, position = "top", ...) {
|
|
| 206 | 3x |
if (missing(e)) {
|
| 207 | 1x |
stop("must pass e", call. = FALSE)
|
| 208 |
} |
|
| 209 | ||
| 210 | 2x |
opts <- list( |
| 211 | 2x |
show = show, |
| 212 | 2x |
position = position, |
| 213 |
... |
|
| 214 |
) |
|
| 215 | ||
| 216 | 2x |
if (!e$x$tl) {
|
| 217 | 1x |
for (i in seq_along(e$x$opts$series)) {
|
| 218 | 1x |
e$x$opts$series[[i]]$label <- opts |
| 219 |
} |
|
| 220 |
} else {
|
|
| 221 | 1x |
for (i in seq_along(e$x$opts$baseOption$series)) {
|
| 222 | 1x |
e$x$opts$baseOption$series[[i]]$label <- opts |
| 223 |
} |
|
| 224 |
} |
|
| 225 | ||
| 226 | 2x |
return(e) |
| 227 |
} |
|
| 228 | ||
| 229 |
#' List |
|
| 230 |
#' |
|
| 231 |
#' simply pass a list of options, similar to a \code{JSON}.
|
|
| 232 |
#' |
|
| 233 |
#' @inheritParams e_bar |
|
| 234 |
#' @param list A \code{list} of options passed to \code{setOptions}.
|
|
| 235 |
#' @param append if \code{TRUE} the \code{list} is appended to the options,
|
|
| 236 |
#' otherwise it \emph{overwrites} everything.
|
|
| 237 |
#' |
|
| 238 |
#' @examples |
|
| 239 |
#' N <- 20 # data points |
|
| 240 |
#' |
|
| 241 |
#' opts <- list( |
|
| 242 |
#' xAxis = list( |
|
| 243 |
#' type = "category", |
|
| 244 |
#' data = LETTERS[1:N] |
|
| 245 |
#' ), |
|
| 246 |
#' yAxis = list( |
|
| 247 |
#' type = "value" |
|
| 248 |
#' ), |
|
| 249 |
#' series = list( |
|
| 250 |
#' list( |
|
| 251 |
#' type = "line", |
|
| 252 |
#' data = round(runif(N, 5, 20)) |
|
| 253 |
#' ) |
|
| 254 |
#' ) |
|
| 255 |
#' ) |
|
| 256 |
#' |
|
| 257 |
#' e_charts() |> |
|
| 258 |
#' e_list(opts) |
|
| 259 |
#' @export |
|
| 260 |
e_list <- function(e, list, append = FALSE) {
|
|
| 261 | 3x |
if (missing(e)) {
|
| 262 | 1x |
stop("must pass e", call. = FALSE)
|
| 263 |
} |
|
| 264 | 2x |
if (missing(list)) {
|
| 265 | 1x |
stop("missing list", call. = FALSE)
|
| 266 |
} |
|
| 267 | ||
| 268 | 1x |
if (isTRUE(append)) {
|
| 269 | ! |
e$x$opts <- append(e$x$opts, list) |
| 270 |
} else {
|
|
| 271 | 1x |
e$x$opts <- list |
| 272 |
} |
|
| 273 | ||
| 274 | 1x |
e |
| 275 |
} |
|
| 276 | ||
| 277 |
#' Aria |
|
| 278 |
#' |
|
| 279 |
#' W3C defined the Accessible Rich Internet Applications Suite (WAI-ARIA) |
|
| 280 |
#' to make Web content and Web applications more accessible to the disabled. |
|
| 281 |
#' From ECharts 4.0, echarts4r supports ARIA by generating description for |
|
| 282 |
#' charts automatically. |
|
| 283 |
#' |
|
| 284 |
#' @details There should be an aria-label attribute on the chart DOM, which |
|
| 285 |
#' can help the disabled understand the content of charts with the help of certain devices. |
|
| 286 |
#' |
|
| 287 |
#' @inheritParams e_bar |
|
| 288 |
#' @param enabled Whether to enable aria helper text. |
|
| 289 |
#' |
|
| 290 |
#' @seealso \href{https://echarts.apache.org/en/option.html#aria}{official documentation}
|
|
| 291 |
#' |
|
| 292 |
#' @export |
|
| 293 |
e_aria <- function(e, enabled = TRUE, ...) {
|
|
| 294 | 1x |
e$x$opts$aria <- list( |
| 295 | 1x |
enabled = enabled, |
| 296 |
... |
|
| 297 |
) |
|
| 298 | ||
| 299 | 1x |
return(e) |
| 300 |
} |
|
| 301 | ||
| 302 |
#' To & From JSON |
|
| 303 |
#' |
|
| 304 |
#' Get JSON options from an echarts4r object and build one from JSON. |
|
| 305 |
#' |
|
| 306 |
#' @inheritParams e_bar |
|
| 307 |
#' @param json Whether to return the JSON, otherwise returns a \code{list}.
|
|
| 308 |
#' @param txt JSON character string, url, or file. |
|
| 309 |
#' @param jswrapper Whether to wrap pure JS functions in \code{htmlwidgets::JS}, Default is FALSE.
|
|
| 310 |
#' @param ... Additional options to pass to \link[jsonlite]{toJSON}.
|
|
| 311 |
#' |
|
| 312 |
#' @details \code{txt} should contain the full list of options required to build a chart.
|
|
| 313 |
#' This is subsequently passed to the \code{setOption} ECharts (JavaScript) function.
|
|
| 314 |
#' |
|
| 315 |
#' @examples |
|
| 316 |
#' p <- cars |> |
|
| 317 |
#' e_charts(dist) |> |
|
| 318 |
#' e_scatter(speed, symbol_size = 10) |
|
| 319 |
#' |
|
| 320 |
#' p # plot |
|
| 321 |
#' |
|
| 322 |
#' # extract the JSON |
|
| 323 |
#' json <- p |> |
|
| 324 |
#' e_inspect( |
|
| 325 |
#' json = TRUE, |
|
| 326 |
#' pretty = TRUE |
|
| 327 |
#' ) |
|
| 328 |
#' |
|
| 329 |
#' # print json |
|
| 330 |
#' json |
|
| 331 |
#' |
|
| 332 |
#' # rebuild plot |
|
| 333 |
#' echarts_from_json(json) |> |
|
| 334 |
#' e_theme("dark") # modify
|
|
| 335 |
#' @return \code{e_inspect} Returns a \code{list} if \code{json} is \code{FALSE} and a
|
|
| 336 |
#' JSON string otherwise. \code{echarts_from_json} returns an object of class \code{echarts4r}.
|
|
| 337 |
#' |
|
| 338 |
#' @note Must be passed as last option. |
|
| 339 |
#' |
|
| 340 |
#' @rdname echartsNJSON |
|
| 341 |
#' @export |
|
| 342 |
e_inspect <- function(e, json = FALSE, ...) {
|
|
| 343 | 2x |
opts <- e$x$opts |
| 344 | ||
| 345 | 2x |
if (isTRUE(json)) {
|
| 346 | 2x |
opts <- jsonlite::toJSON(opts, force = TRUE, auto_unbox = TRUE, null = "null", ...) |
| 347 |
} |
|
| 348 | ||
| 349 | 2x |
return(opts) |
| 350 |
} |
|
| 351 | ||
| 352 |
#' @rdname echartsNJSON |
|
| 353 |
#' @export |
|
| 354 |
echarts_from_json <- function(txt, jswrapper = FALSE) {
|
|
| 355 | 1x |
json <- jsonlite::fromJSON(txt, simplifyVector = FALSE) |
| 356 | ||
| 357 | 1x |
e <- e_charts() |
| 358 | 1x |
e$x$opts <- json |
| 359 | 1x |
if (isTRUE(jswrapper)) {
|
| 360 | ! |
for (i in seq_along(e$x$opts$tooltip)) {
|
| 361 | ||
| 362 | ! |
if (is.character(e$x$opts$tooltip[[i]]) && startsWith(e$x$opts$tooltip[[i]], "function")) {
|
| 363 |
# Wrap pure JS functions in htmlwidgets::JS |
|
| 364 | ! |
e$x$opts$tooltip[[i]] <- htmlwidgets::JS(e$x$opts$tooltip[[i]]) |
| 365 |
} |
|
| 366 | ||
| 367 |
} |
|
| 368 |
} |
|
| 369 | ||
| 370 | 1x |
e |
| 371 |
} |
|
| 372 | ||
| 373 |
#' Axis ZigZags |
|
| 374 |
#' |
|
| 375 |
#' helper function for generating axis break zigzags in chart |
|
| 376 |
#' |
|
| 377 |
#' @inheritParams e_bar |
|
| 378 |
#' @param axis Axis to apply formatter to. Supports x and y axis |
|
| 379 |
#' @param start,end Start and End point for boundary of zigzag. Also supports vectors for generating multiple breaks. Can also support time values. |
|
| 380 |
#' @param gap Determines the visual size of the axis break area. |
|
| 381 |
#' Supports Percentage(String) as proportional value relative to axis. Supports Absolute value(numeric) which refers to literal values in the axis similar to start,end (Not a pixel value). |
|
| 382 |
#' @param zigzagAmplitude Amplitude of zigzag. Unit is pixels. |
|
| 383 |
#' @param ... Any other arguments to pass to breakArea argument. |
|
| 384 |
#' |
|
| 385 |
#' @examples |
|
| 386 |
#' |
|
| 387 |
#' df <- data.frame( |
|
| 388 |
#' x = c("a", "b", "c", "d", "c"),
|
|
| 389 |
#' y = c(100, 200, 200, 700, 300) |
|
| 390 |
#' ) |
|
| 391 |
#' |
|
| 392 |
#' df |> |
|
| 393 |
#' e_charts(x) |> |
|
| 394 |
#' e_bar(y) |> |
|
| 395 |
#' e_zigzag(axis = 'y', start = 400, end = 500) |
|
| 396 |
#' |
|
| 397 |
#' df |> |
|
| 398 |
#' e_charts(x) |> |
|
| 399 |
#' e_bar(y) |> |
|
| 400 |
#' e_zigzag(axis = 'y', start = c(125,400), end = c(150,500)) |
|
| 401 |
#' @seealso \href{https://echarts.apache.org/en/option.html#series-bar}{Additional arguments}
|
|
| 402 |
#' |
|
| 403 |
#' @rdname e_zigzag |
|
| 404 |
#' @export |
|
| 405 |
e_zigzag <- function(e, axis = 'y', start, end, gap = "3%", zigzagAmplitude = 10, ...){
|
|
| 406 | ||
| 407 | 10x |
if (missing(e)) {
|
| 408 | 1x |
stop("must pass e", call. = FALSE)
|
| 409 |
} |
|
| 410 | ||
| 411 | 9x |
if (missing(axis)) {
|
| 412 | 1x |
stop("must indicate which axis to zigzag. e.g. 'x' or 'y'", call. = FALSE)
|
| 413 |
} |
|
| 414 | ||
| 415 | 8x |
if (missing(start) | missing(end)) {
|
| 416 | 2x |
stop("must provide start and end values")
|
| 417 |
} |
|
| 418 | ||
| 419 | 6x |
df <- data.frame("starts" = start, "ends" = end, "gap" = gap)
|
| 420 | 6x |
b <- .build_zigzags(df, starts = "starts", ends = "ends", gap = "gap") |
| 421 | ||
| 422 | 6x |
bA <- list( |
| 423 | 6x |
zigzagAmplitude = zigzagAmplitude, |
| 424 |
... |
|
| 425 |
) |
|
| 426 | ||
| 427 | 6x |
if(axis == 'y'){
|
| 428 | 5x |
e <- e |> e_y_axis(breaks = b, breakArea = bA) |
| 429 |
} |
|
| 430 | ||
| 431 | 6x |
if(axis == 'x'){
|
| 432 | 1x |
e <- e |> e_x_axis(breaks = b, breakArea = bA) |
| 433 |
} |
|
| 434 | 6x |
e |
| 435 |
} |
|
| 436 | ||
| 437 | ||
| 438 |
#' Axis Jitter |
|
| 439 |
#' |
|
| 440 |
#' helper function for generating jitter between points in a scatter plot. This is only applicable to e_scatter(). |
|
| 441 |
#' |
|
| 442 |
#' @inheritParams e_bar |
|
| 443 |
#' @param axis Axis to apply formatter to. Supports x and y axis |
|
| 444 |
#' @param jitter Pixel units indicating the amount of random noise to add to each data point position. |
|
| 445 |
#' @param jitterOverlap Boolean allowing overlap between data points. If false, overlap will not be allowed. For some cases, scatters may still overlap if there is no reasonable way to avoid. |
|
| 446 |
#' @param jitterMargin When you have jitter and jiterOverlap is FALSE, this is the minimum distance in pixels between two data points. |
|
| 447 |
#' |
|
| 448 |
#' @examples |
|
| 449 |
#' |
|
| 450 |
#' df <- data.frame( |
|
| 451 |
#' value = c(rnorm(50, mean = 5, sd = 1), |
|
| 452 |
#' rnorm(50, mean = 10, sd = 1), |
|
| 453 |
#' rnorm(50, mean = 15, sd = 1)), |
|
| 454 |
#' group = rep(c("Group A", "Group B", "Group C"), each = 50)
|
|
| 455 |
#' ) |
|
| 456 |
#' |
|
| 457 |
#' df |> e_charts(group) |> e_scatter(value) |> e_jitter() |
|
| 458 |
#' @seealso \href{https://echarts.apache.org/en/option.html#yAxis.jitter}{Additional arguments}
|
|
| 459 |
#' |
|
| 460 |
#' @rdname e_jitter |
|
| 461 |
#' @export |
|
| 462 |
e_jitter <- function(e, axis = 'x', jitter = 20, jitterOverlap = FALSE, jitterMargin = 5){
|
|
| 463 | 5x |
if(missing(e)) {
|
| 464 | 1x |
stop("must pass e", call. = FALSE)
|
| 465 |
} |
|
| 466 | ||
| 467 | 4x |
if(is.null(axis)) {
|
| 468 | 1x |
stop("must indicate which axis to apply jitter. e.g. 'x' or 'y'", call. = FALSE)
|
| 469 |
} |
|
| 470 | ||
| 471 | 3x |
if(e$x$opts$series[[1]]$type != 'scatter' || is.null(e$x$opts$series[[1]]$type)){
|
| 472 | 1x |
stop("jitter is only supported with scatter plots")
|
| 473 |
} |
|
| 474 | ||
| 475 | 2x |
if(axis == 'x'){
|
| 476 | 1x |
e <- e |> e_x_axis(jitter = jitter, jitterOverlap = jitterOverlap, jitterMargin = jitterMargin) |
| 477 |
} |
|
| 478 | ||
| 479 | 2x |
if(axis == 'y'){
|
| 480 | 1x |
e <- e |> e_y_axis(jitter = jitter, jitterOverlap = jitterOverlap, jitterMargin = jitterMargin) |
| 481 |
} |
|
| 482 | 2x |
e |
| 483 |
} |
| 1 |
#' Append Proxy |
|
| 2 |
#' |
|
| 3 |
#' Append data dynamically. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_highlight_p |
|
| 6 |
#' @param series_index Index of serie to append to (starts from 0). |
|
| 7 |
#' @param data Data.frame containing data to append. |
|
| 8 |
#' @param x,y,z Columns names to plot. |
|
| 9 |
#' @param name if using `bind` with e.g `e_scatter` this can be used to supply the colname for |
|
| 10 |
#' the name attribute bind is mapping to |
|
| 11 |
#' @param scale A scaling function as passed to \code{\link{e_scatter}}.
|
|
| 12 |
#' @param symbol_size Multiplier of scaling function as in \code{\link{e_scatter}}.
|
|
| 13 |
#' |
|
| 14 |
#' @details Currently not all types of series supported incremental rendering when using appendData. |
|
| 15 |
#' Only these types of series support it: \code{\link{e_scatter}} and \code{\link{e_line}} of pure echarts, and
|
|
| 16 |
#' \code{\link{e_scatter_3d}}, and \code{\link{e_line_3d}} of echarts-gl.
|
|
| 17 |
#' |
|
| 18 |
#' @examples |
|
| 19 |
#' \dontrun{
|
|
| 20 |
#' library(shiny) |
|
| 21 |
#' |
|
| 22 |
#' ui <- fluidPage( |
|
| 23 |
#' actionButton("add", "Add Data to y"),
|
|
| 24 |
#' echarts4rOutput("plot"),
|
|
| 25 |
#' h4("Brush"),
|
|
| 26 |
#' verbatimTextOutput("selected"),
|
|
| 27 |
#' h4("Legend select change"),
|
|
| 28 |
#' verbatimTextOutput("legend")
|
|
| 29 |
#' ) |
|
| 30 |
#' |
|
| 31 |
#' server <- function(input, output, session) {
|
|
| 32 |
#' data <- data.frame(x = rnorm(10, 5, 3), y = rnorm(10, 50, 12), z = rnorm(10, 5, 20)) |
|
| 33 |
#' |
|
| 34 |
#' react <- eventReactive(input$add, {
|
|
| 35 |
#' set.seed(sample(1:1000, 1)) |
|
| 36 |
#' data.frame(x = rnorm(10, 5, 2), y = rnorm(10, 50, 10), z = rnorm(10, 5, 20)) |
|
| 37 |
#' }) |
|
| 38 |
#' |
|
| 39 |
#' output$plot <- renderEcharts4r({
|
|
| 40 |
#' data |> |
|
| 41 |
#' e_charts(x) |> |
|
| 42 |
#' e_scatter(y, z, scale = NULL) |> |
|
| 43 |
#' e_scatter(z) |> |
|
| 44 |
#' e_brush() |
|
| 45 |
#' }) |
|
| 46 |
#' |
|
| 47 |
#' observeEvent(input$add, {
|
|
| 48 |
#' |
|
| 49 |
#' echarts4rProxy("plot") |>
|
|
| 50 |
#' e_append2_p(0, react(), x, y, z) |
|
| 51 |
#' }) |
|
| 52 |
#' |
|
| 53 |
#' output$selected <- renderPrint({
|
|
| 54 |
#' input$plot_brush |
|
| 55 |
#' }) |
|
| 56 |
#' |
|
| 57 |
#' output$legend <- renderPrint({
|
|
| 58 |
#' input$plot_legend_change |
|
| 59 |
#' }) |
|
| 60 |
#' } |
|
| 61 |
#' |
|
| 62 |
#' shinyApp(ui, server) |
|
| 63 |
#' } |
|
| 64 |
#' |
|
| 65 |
#' @rdname append |
|
| 66 |
#' @export |
|
| 67 |
e_append1_p <- function(proxy, series_index = NULL, data, x, y, name = NULL) {
|
|
| 68 | 2x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 69 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 70 |
} |
|
| 71 | ||
| 72 | 1x |
e_append1_p_(proxy, series_index, data, deparse(substitute(x)), deparse(substitute(y)), deparse(substitute(name))) |
| 73 |
} |
|
| 74 | ||
| 75 |
#' @rdname append |
|
| 76 |
#' @export |
|
| 77 |
e_append1_p_ <- function(proxy, series_index = NULL, data, x, y, name = NULL) {
|
|
| 78 | 3x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 79 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 80 |
} |
|
| 81 | ||
| 82 | 2x |
dlist <- data |> |
| 83 | 2x |
dplyr::select(dplyr::all_of(c(x, y, name))) |> |
| 84 | 2x |
unname() |> |
| 85 | 2x |
apply(1, function(row){
|
| 86 | 20x |
if(!is.null(name)){
|
| 87 | 20x |
list(value=as.numeric(row[1:2]),name=row[3]) |
| 88 |
} else {
|
|
| 89 | ! |
list(value=as.numeric(row[1:2])) |
| 90 |
} |
|
| 91 |
}) |
|
| 92 | ||
| 93 | 2x |
opts <- list(id = proxy$id, seriesIndex = series_index, data = dlist) |
| 94 | ||
| 95 | 2x |
proxy$session$sendCustomMessage("e_append_p", opts)
|
| 96 | ||
| 97 | 2x |
return(proxy) |
| 98 |
} |
|
| 99 | ||
| 100 |
#' @rdname append |
|
| 101 |
#' @export |
|
| 102 |
e_append2_p <- function(proxy, series_index = NULL, data, x, y, z, scale = NULL, symbol_size = 1) {
|
|
| 103 | 2x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 104 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 105 |
} |
|
| 106 | ||
| 107 | 1x |
e_append2_p_( |
| 108 | 1x |
proxy = proxy, |
| 109 | 1x |
series_index = series_index, |
| 110 | 1x |
data = data, |
| 111 | 1x |
x = deparse(substitute(x)), |
| 112 | 1x |
y = deparse(substitute(y)), |
| 113 | 1x |
z = deparse(substitute(z)), |
| 114 | 1x |
scale = scale, |
| 115 | 1x |
symbol_size = symbol_size |
| 116 |
) |
|
| 117 |
} |
|
| 118 | ||
| 119 |
#' @rdname append |
|
| 120 |
#' @export |
|
| 121 |
e_append2_p_ <- function(proxy, series_index = NULL, data, x, y, z, scale = NULL, symbol_size = 1) {
|
|
| 122 | 3x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 123 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 124 |
} |
|
| 125 | ||
| 126 | 2x |
data <- data |> |
| 127 | 2x |
dplyr::select(dplyr::all_of(c(x, y, z))) |
| 128 | ||
| 129 | 2x |
if (!is.null(scale)) {
|
| 130 | ! |
data[[4]] <- scale(data[[3]]) * symbol_size |
| 131 |
} else {
|
|
| 132 | 2x |
data[[4]] <- data[[3]] |
| 133 |
} |
|
| 134 | ||
| 135 | 2x |
data <- unname(data) |
| 136 | ||
| 137 | 2x |
dlist <- apply(data, 1, function(x) {
|
| 138 | 20x |
list(value = unlist(x, use.names = FALSE)) |
| 139 |
}) |
|
| 140 | ||
| 141 | 2x |
opts <- list(id = proxy$id, seriesIndex = series_index, data = dlist) |
| 142 | ||
| 143 | 2x |
proxy$session$sendCustomMessage("e_append_p", opts)
|
| 144 | ||
| 145 | 2x |
return(proxy) |
| 146 |
} |
|
| 147 | ||
| 148 |
#' Remove Serie |
|
| 149 |
#' |
|
| 150 |
#' Remove a serie by name or precising its index. |
|
| 151 |
#' |
|
| 152 |
#' @inheritParams e_highlight_p |
|
| 153 |
#' @param serie_index Index of serie to append to (starts from 0). |
|
| 154 |
#' @param serie_name Name of serie to remove. |
|
| 155 |
#' |
|
| 156 |
#' @examples |
|
| 157 |
#' library(shiny) |
|
| 158 |
#' |
|
| 159 |
#' ui <- fluidPage( |
|
| 160 |
#' actionButton("rm", "Remove z serie"),
|
|
| 161 |
#' echarts4rOutput("plot")
|
|
| 162 |
#' ) |
|
| 163 |
#' |
|
| 164 |
#' server <- function(input, output, session) {
|
|
| 165 |
#' data <- data.frame( |
|
| 166 |
#' x = rnorm(10, 5, 3), |
|
| 167 |
#' y = rnorm(10, 50, 12), |
|
| 168 |
#' z = rnorm(10, 50, 5) |
|
| 169 |
#' ) |
|
| 170 |
#' |
|
| 171 |
#' output$plot <- renderEcharts4r({
|
|
| 172 |
#' data |> |
|
| 173 |
#' e_charts(x) |> |
|
| 174 |
#' e_scatter(y) |> |
|
| 175 |
#' e_scatter(z) |
|
| 176 |
#' }) |
|
| 177 |
#' |
|
| 178 |
#' observeEvent(input$rm, {
|
|
| 179 |
#' echarts4rProxy("plot") |>
|
|
| 180 |
#' e_remove_serie_p(serie_name = "z") |
|
| 181 |
#' }) |
|
| 182 |
#' } |
|
| 183 |
#' \dontrun{
|
|
| 184 |
#' shinyApp(ui, server) |
|
| 185 |
#' } |
|
| 186 |
#' |
|
| 187 |
#' @name e_remove |
|
| 188 |
#' @export |
|
| 189 |
e_remove_serie_p <- function(proxy, serie_name = NULL, serie_index = NULL) {
|
|
| 190 | 2x |
if (is.null(serie_index) && is.null(serie_name)) {
|
| 191 | 1x |
stop("Must define `serie_index` or `serie_name`")
|
| 192 |
} |
|
| 193 | ||
| 194 | 1x |
opts <- list(id = proxy$id, serie_index = serie_index, serie_name = serie_name) |
| 195 | ||
| 196 | 1x |
proxy$session$sendCustomMessage("e_remove_serie_p", opts)
|
| 197 | ||
| 198 | 1x |
return(proxy) |
| 199 |
} |
|
| 200 | ||
| 201 |
#' @rdname e_remove |
|
| 202 |
#' @export |
|
| 203 |
e_remove_serie <- e_remove_serie_p |
|
| 204 | ||
| 205 |
#' Send |
|
| 206 |
#' |
|
| 207 |
#' Send new series to chart. |
|
| 208 |
#' |
|
| 209 |
#' @inheritParams e_highlight_p |
|
| 210 |
#' |
|
| 211 |
#' @name e_execute |
|
| 212 |
#' @export |
|
| 213 |
e_execute <- function(proxy) {
|
|
| 214 | 44x |
if (missing(proxy)) {
|
| 215 | 1x |
stop("missing proxy", call. = FALSE)
|
| 216 |
} |
|
| 217 | ||
| 218 | 43x |
proxy$session$sendCustomMessage( |
| 219 | 43x |
"e_send_p", |
| 220 | 43x |
list(id = proxy$id, opts = proxy$chart$x$opts) |
| 221 |
) |
|
| 222 | 43x |
return(proxy) |
| 223 |
} |
|
| 224 | ||
| 225 |
#' @rdname e_execute |
|
| 226 |
#' @export |
|
| 227 |
e_execute_p <- e_execute |
|
| 228 | ||
| 229 |
#' Merge options in chart, used in e_mark |
|
| 230 |
#' |
|
| 231 |
#' @inheritParams e_highlight_p |
|
| 232 |
#' |
|
| 233 |
#' @name e_merge |
|
| 234 |
#' @export |
|
| 235 |
e_merge <- function(proxy) {
|
|
| 236 | 1x |
if (missing(proxy)) stop("missing proxy", call. = FALSE)
|
| 237 | ||
| 238 | 1x |
proxy$session$sendCustomMessage( |
| 239 | 1x |
"e_merge_p", |
| 240 | 1x |
list(id = proxy$id, opts = proxy$chart$x$opts) |
| 241 |
) |
|
| 242 | 1x |
return(proxy) |
| 243 |
} |
| 1 |
#' Highlight & Downplay |
|
| 2 |
#' |
|
| 3 |
#' Highlight series |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param series_index,series_name Index or name of serie to highlight or |
|
| 7 |
#' \code{list} or \code{vector} of series.
|
|
| 8 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 9 |
#' |
|
| 10 |
#' @examples |
|
| 11 |
#' iris |> |
|
| 12 |
#' group_by(Species) |> |
|
| 13 |
#' e_charts(Sepal.Length) |> |
|
| 14 |
#' e_line(Sepal.Width) |> |
|
| 15 |
#' e_line(Petal.Length) |> |
|
| 16 |
#' e_highlight(series_name = "setosa") # highlight group |
|
| 17 |
#' @name highlight_action |
|
| 18 |
#' @export |
|
| 19 |
e_highlight <- function(e, series_index = NULL, series_name = NULL, btn = NULL) {
|
|
| 20 | 8x |
if (is.null(series_index) && is.null(series_name)) {
|
| 21 | 1x |
stop("must set index or name", call. = FALSE)
|
| 22 |
} |
|
| 23 | ||
| 24 | 7x |
opts <- list() |
| 25 | ||
| 26 | 7x |
data <- list(type = "highlight") |
| 27 | 5x |
if (!is.null(series_index)) data$seriesIndex <- as.list(series_index) |
| 28 | 2x |
if (!is.null(series_name)) data$seriesName <- as.list(series_name) |
| 29 | 7x |
opts$data <- data |
| 30 | ||
| 31 | 7x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 32 | ||
| 33 | 7x |
return(e) |
| 34 |
} |
|
| 35 | ||
| 36 |
#' @name highlight_action |
|
| 37 |
#' @export |
|
| 38 |
e_downplay <- function(e, series_index = NULL, series_name = NULL, btn = NULL) {
|
|
| 39 | 8x |
if (is.null(series_index) && is.null(series_name)) {
|
| 40 | 1x |
stop("must set index or name", call. = FALSE)
|
| 41 |
} |
|
| 42 | ||
| 43 | 7x |
opts <- list() |
| 44 | ||
| 45 | 7x |
data <- list(type = "downplay") |
| 46 | 5x |
if (!is.null(series_index)) data$seriesIndex <- as.list(series_index) |
| 47 | 2x |
if (!is.null(series_name)) data$seriesName <- as.list(series_name) |
| 48 | 7x |
opts$data <- data |
| 49 | ||
| 50 | 7x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 51 | ||
| 52 | 7x |
return(e) |
| 53 |
} |
|
| 54 | ||
| 55 |
#' Show & Hide Tooltip |
|
| 56 |
#' |
|
| 57 |
#' Show or hide tooltip. |
|
| 58 |
#' |
|
| 59 |
#' @inheritParams e_bar |
|
| 60 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 61 |
#' @param ... Any options, see |
|
| 62 |
#' \href{https://echarts.apache.org/en/api.html#action.tooltip}{official
|
|
| 63 |
#' documentation} |
|
| 64 |
#' |
|
| 65 |
#' @note The tooltip must be initialised with \code{\link{e_tooltip}} for this
|
|
| 66 |
#' to work. |
|
| 67 |
#' |
|
| 68 |
#' @examples |
|
| 69 |
#' cars |> |
|
| 70 |
#' e_charts(dist) |> |
|
| 71 |
#' e_scatter(speed) |> |
|
| 72 |
#' e_tooltip() |> |
|
| 73 |
#' e_hidetip(btn = "btn") |> |
|
| 74 |
#' e_button("btn", "Hide tooltip")
|
|
| 75 |
#' @name tooltip_action |
|
| 76 |
#' @export |
|
| 77 |
e_showtip <- function(e, ..., btn = NULL) {
|
|
| 78 | 5x |
opts <- list() |
| 79 | 5x |
data <- list(type = "showTip", ...) |
| 80 | 5x |
opts$data <- data |
| 81 | ||
| 82 | 5x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 83 | ||
| 84 | 5x |
return(e) |
| 85 |
} |
|
| 86 | ||
| 87 |
#' @name tooltip_action |
|
| 88 |
#' @export |
|
| 89 |
e_hidetip <- function(e, ..., btn = NULL) {
|
|
| 90 | 5x |
opts <- list() |
| 91 | 5x |
data <- list(type = "hideTip", ...) |
| 92 | 5x |
opts$data <- data |
| 93 | ||
| 94 | 5x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 95 | ||
| 96 | 5x |
return(e) |
| 97 |
} |
|
| 98 | ||
| 99 |
#' Zoom |
|
| 100 |
#' |
|
| 101 |
#' Zoom on a region. |
|
| 102 |
#' |
|
| 103 |
#' @inheritParams e_bar |
|
| 104 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 105 |
#' @param ... Any options, see |
|
| 106 |
#' \href{https://echarts.apache.org/en/api.html#action.dataZoom.dataZoom}{official
|
|
| 107 |
#' documentation} |
|
| 108 |
#' |
|
| 109 |
#' @examples |
|
| 110 |
#' cars |> |
|
| 111 |
#' e_charts(dist) |> |
|
| 112 |
#' e_scatter(speed) |> |
|
| 113 |
#' e_datazoom() |> |
|
| 114 |
#' e_zoom( |
|
| 115 |
#' dataZoomIndex = 0, |
|
| 116 |
#' start = 20, |
|
| 117 |
#' end = 40, |
|
| 118 |
#' btn = "BUTTON" |
|
| 119 |
#' ) |> |
|
| 120 |
#' e_button("BUTTON", "Zoom in")
|
|
| 121 |
#' @export |
|
| 122 |
e_zoom <- function(e, ..., btn = NULL) {
|
|
| 123 | 1x |
opts <- list() |
| 124 | 1x |
opts$data <- list(type = "dataZoom", ...) |
| 125 | ||
| 126 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 127 | ||
| 128 | 1x |
return(e) |
| 129 |
} |
|
| 130 | ||
| 131 |
#' Select Visual Map |
|
| 132 |
#' |
|
| 133 |
#' Selects data range of visual mapping. |
|
| 134 |
#' |
|
| 135 |
#' @inheritParams e_bar |
|
| 136 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 137 |
#' @param ... Any options, see |
|
| 138 |
#' \href{https://echarts.apache.org/en/api.html#action.visualMap}{official
|
|
| 139 |
#' documentation} |
|
| 140 |
#' |
|
| 141 |
#' @examples |
|
| 142 |
#' data("state")
|
|
| 143 |
#' |
|
| 144 |
#' as.data.frame(state.x77) |> |
|
| 145 |
#' e_charts(Population) |> |
|
| 146 |
#' e_scatter(Income, Frost) |> |
|
| 147 |
#' e_visual_map(Frost, scale = e_scale) |> |
|
| 148 |
#' e_legend(FALSE) |> |
|
| 149 |
#' e_visual_map_range( |
|
| 150 |
#' selected = list(60, 120) |
|
| 151 |
#' ) |
|
| 152 |
#' @export |
|
| 153 |
e_visual_map_range <- function(e, ..., btn = NULL) {
|
|
| 154 | 1x |
opts <- list() |
| 155 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 156 | 1x |
opts$data <- list(type = "selectDataRange", ...) |
| 157 | ||
| 158 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 159 | ||
| 160 | 1x |
return(e) |
| 161 |
} |
|
| 162 | ||
| 163 |
# TODO this works but I see nothing changing in the examples. |
|
| 164 |
# There's also pieToggleSelect |
|
| 165 | ||
| 166 |
#' Select & Unselect Pie |
|
| 167 |
#' |
|
| 168 |
#' Actions related to \code{\link{e_pie}}.
|
|
| 169 |
#' |
|
| 170 |
#' @inheritParams e_bar |
|
| 171 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 172 |
#' @param ... Any options, see |
|
| 173 |
#' \href{https://echarts.apache.org/en/api.html#action.pie}{official
|
|
| 174 |
#' documentation} |
|
| 175 |
#' |
|
| 176 |
#' @examples |
|
| 177 |
#' mtcars |> |
|
| 178 |
#' head() |> |
|
| 179 |
#' tibble::rownames_to_column("model") |>
|
|
| 180 |
#' e_charts(model) |> |
|
| 181 |
#' e_pie(carb) |> |
|
| 182 |
#' e_pie_select(dataIndex = 0) |
|
| 183 |
#' @name pie_action |
|
| 184 |
#' @export |
|
| 185 |
e_pie_select <- function(e, ..., btn = NULL) {
|
|
| 186 | 1x |
opts <- list() |
| 187 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 188 | 1x |
opts$data <- list(type = "pieSelect", ...) |
| 189 | ||
| 190 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 191 | ||
| 192 | 1x |
return(e) |
| 193 |
} |
|
| 194 | ||
| 195 |
#' @rdname pie_action |
|
| 196 |
#' @export |
|
| 197 |
e_pie_unselect <- function(e, ..., btn = NULL) {
|
|
| 198 | 1x |
opts <- list() |
| 199 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 200 | 1x |
opts$data <- list(type = "pieUnSelect", ...) |
| 201 | ||
| 202 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 203 | ||
| 204 | 1x |
return(e) |
| 205 |
} |
|
| 206 | ||
| 207 |
#' Nodes Adjacency |
|
| 208 |
#' |
|
| 209 |
#' Actions related to \code{\link{e_graph}}.
|
|
| 210 |
#' |
|
| 211 |
#' @inheritParams e_bar |
|
| 212 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 213 |
#' @param ... Any options, see |
|
| 214 |
#' \href{https://echarts.apache.org/en/api.html#action.graph}{official
|
|
| 215 |
#' documentation} |
|
| 216 |
#' |
|
| 217 |
#' @examples |
|
| 218 |
#' value <- rnorm(10, 10, 2) |
|
| 219 |
#' |
|
| 220 |
#' nodes <- data.frame( |
|
| 221 |
#' name = sample(LETTERS, 10), |
|
| 222 |
#' value = value, |
|
| 223 |
#' size = value, |
|
| 224 |
#' grp = rep(c("grp1", "grp2"), 5),
|
|
| 225 |
#' stringsAsFactors = FALSE |
|
| 226 |
#' ) |
|
| 227 |
#' |
|
| 228 |
#' edges <- data.frame( |
|
| 229 |
#' source = sample(nodes$name, 20, replace = TRUE), |
|
| 230 |
#' target = sample(nodes$name, 20, replace = TRUE), |
|
| 231 |
#' stringsAsFactors = FALSE |
|
| 232 |
#' ) |
|
| 233 |
#' |
|
| 234 |
#' e_charts() |> |
|
| 235 |
#' e_graph() |> |
|
| 236 |
#' e_graph_nodes(nodes, name, value, size, grp) |> |
|
| 237 |
#' e_graph_edges(edges, source, target) |> |
|
| 238 |
#' e_focus_adjacency( |
|
| 239 |
#' seriesIndex = 0, |
|
| 240 |
#' dataIndex = 4 |
|
| 241 |
#' ) |
|
| 242 |
#' @name graph_action |
|
| 243 |
#' @export |
|
| 244 |
e_focus_adjacency <- function(e, ..., btn = NULL) {
|
|
| 245 | 1x |
opts <- list() |
| 246 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 247 | 1x |
opts$data <- list(type = "focusNodeAdjacency", ...) |
| 248 | ||
| 249 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 250 | ||
| 251 | 1x |
return(e) |
| 252 |
} |
|
| 253 | ||
| 254 |
#' @rdname graph_action |
|
| 255 |
#' @export |
|
| 256 |
e_unfocus_adjacency <- function(e, ..., btn = NULL) {
|
|
| 257 | 1x |
opts <- list() |
| 258 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 259 | 1x |
opts$data <- list(type = "unfocusNodeAdjacency", ...) |
| 260 | ||
| 261 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 262 | ||
| 263 | 1x |
return(e) |
| 264 |
} |
|
| 265 | ||
| 266 |
#' Legend |
|
| 267 |
#' |
|
| 268 |
#' @inheritParams e_bar |
|
| 269 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 270 |
#' @param name Legend name. |
|
| 271 |
#' @param scroll_index Controle the scrolling of legend when \code{type =
|
|
| 272 |
#' "scroll"} in \code{e_legend}.
|
|
| 273 |
#' @param legend_id Id of legend. |
|
| 274 |
#' |
|
| 275 |
#' @examples |
|
| 276 |
#' e <- CO2 |> |
|
| 277 |
#' group_by(Type) |> |
|
| 278 |
#' e_charts(conc) |> |
|
| 279 |
#' e_scatter(uptake) |
|
| 280 |
#' |
|
| 281 |
#' e |> |
|
| 282 |
#' e_legend_unselect("Quebec")
|
|
| 283 |
#' |
|
| 284 |
#' e |> |
|
| 285 |
#' e_legend_unselect("Quebec", btn = "btn") |>
|
|
| 286 |
#' e_button("btn", "Quebec")
|
|
| 287 |
#' @name legend_action |
|
| 288 |
#' @export |
|
| 289 |
e_legend_select <- function(e, name, btn = NULL) {
|
|
| 290 | 2x |
if (missing(name)) {
|
| 291 | 1x |
stop("missing name", call. = FALSE)
|
| 292 |
} |
|
| 293 | ||
| 294 | 1x |
opts <- list() |
| 295 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 296 | 1x |
opts$data <- list(type = "legendSelect") |
| 297 | ||
| 298 | 1x |
if (!is.null(name)) {
|
| 299 | 1x |
opts$data$name <- name |
| 300 |
} |
|
| 301 | ||
| 302 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 303 | ||
| 304 | 1x |
return(e) |
| 305 |
} |
|
| 306 | ||
| 307 |
#' @rdname legend_action |
|
| 308 |
#' @export |
|
| 309 |
e_legend_unselect <- function(e, name, btn = NULL) {
|
|
| 310 | 3x |
if (missing(name)) {
|
| 311 | 2x |
stop("missing name", call. = FALSE)
|
| 312 |
} |
|
| 313 | ||
| 314 | 1x |
opts <- list() |
| 315 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 316 | 1x |
opts$data <- list(type = "legendUnSelect") |
| 317 | ||
| 318 | 1x |
if (!is.null(name)) {
|
| 319 | 1x |
opts$data$name <- name |
| 320 |
} |
|
| 321 | ||
| 322 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 323 | ||
| 324 | 1x |
return(e) |
| 325 |
} |
|
| 326 | ||
| 327 |
#' @rdname legend_action |
|
| 328 |
#' @export |
|
| 329 |
e_legend_toggle_select <- function(e, name, btn = NULL) {
|
|
| 330 | 2x |
if (missing(name)) {
|
| 331 | 1x |
stop("missing name", call. = FALSE)
|
| 332 |
} |
|
| 333 | ||
| 334 | 1x |
opts <- list() |
| 335 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 336 | 1x |
opts$data <- list(type = "legendToggleSelect") |
| 337 | ||
| 338 | 1x |
if (!is.null(name)) {
|
| 339 | 1x |
opts$data$name <- name |
| 340 |
} |
|
| 341 | ||
| 342 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 343 | ||
| 344 | 1x |
return(e) |
| 345 |
} |
|
| 346 | ||
| 347 |
#' @rdname legend_action |
|
| 348 |
#' @export |
|
| 349 |
e_legend_scroll <- function(e, |
|
| 350 |
scroll_index = NULL, |
|
| 351 |
legend_id = NULL, |
|
| 352 |
btn = NULL |
|
| 353 |
) {
|
|
| 354 | 1x |
opts <- list() |
| 355 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 356 | 1x |
opts$data <- list(type = "legendScroll") |
| 357 | ||
| 358 | 1x |
if (!is.null(scroll_index)) {
|
| 359 | 1x |
opts$data$scrollDataIndex <- scroll_index |
| 360 |
} |
|
| 361 | ||
| 362 | 1x |
if (!is.null(legend_id)) {
|
| 363 | 1x |
opts$data$legendId <- legend_id |
| 364 |
} |
|
| 365 | ||
| 366 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 367 | ||
| 368 | 1x |
return(e) |
| 369 |
} |
|
| 370 | ||
| 371 |
#' Restore Toolbox |
|
| 372 |
#' |
|
| 373 |
#' Restore Toolbox. |
|
| 374 |
#' |
|
| 375 |
#' @inheritParams e_bar |
|
| 376 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 377 |
#' |
|
| 378 |
#' @examples |
|
| 379 |
#' cars |> |
|
| 380 |
#' e_charts(speed) |> |
|
| 381 |
#' e_scatter(dist) |> |
|
| 382 |
#' e_datazoom() |> |
|
| 383 |
#' e_restore("btn") |>
|
|
| 384 |
#' e_button("btn", "Reset")
|
|
| 385 |
#' @export |
|
| 386 |
e_restore <- function(e, btn = NULL) {
|
|
| 387 | 1x |
opts <- list() |
| 388 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 389 | 1x |
opts$data <- list(type = "restore") |
| 390 | ||
| 391 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 392 | ||
| 393 | 1x |
return(e) |
| 394 |
} |
|
| 395 | ||
| 396 |
#' Map Actions |
|
| 397 |
#' |
|
| 398 |
#' Map-related actions. |
|
| 399 |
#' |
|
| 400 |
#' @inheritParams e_bar |
|
| 401 |
#' @param btn A \code{\link{e_button}} id.
|
|
| 402 |
#' @param ... Any options, see |
|
| 403 |
#' \href{https://echarts.apache.org/en/api.html#action.map}{official
|
|
| 404 |
#' documentation} |
|
| 405 |
#' |
|
| 406 |
#' @examples |
|
| 407 |
#' choropleth <- data.frame( |
|
| 408 |
#' countries = c( |
|
| 409 |
#' "France", |
|
| 410 |
#' "Brazil", |
|
| 411 |
#' "China", |
|
| 412 |
#' "Russia", |
|
| 413 |
#' "Canada", |
|
| 414 |
#' "India", |
|
| 415 |
#' "United States", |
|
| 416 |
#' "Argentina", |
|
| 417 |
#' "Australia" |
|
| 418 |
#' ), |
|
| 419 |
#' values = round(runif(9, 10, 25)) |
|
| 420 |
#' ) |
|
| 421 |
#' |
|
| 422 |
#' choropleth |> |
|
| 423 |
#' e_charts(countries) |> |
|
| 424 |
#' e_map(values) |> |
|
| 425 |
#' e_visual_map(min = 10, max = 25) |> |
|
| 426 |
#' e_map_toggle_select(name = "China", btn = "btn") |> |
|
| 427 |
#' e_button("btn", "Select China")
|
|
| 428 |
#' @seealso \code{\link{e_map_register}}
|
|
| 429 |
#' |
|
| 430 |
#' @name map_actions |
|
| 431 |
#' @export |
|
| 432 |
e_map_select <- function(e, ..., btn = NULL) {
|
|
| 433 | 1x |
opts <- list() |
| 434 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 435 | 1x |
opts$data <- list(type = "mapSelect", ...) |
| 436 | ||
| 437 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 438 | ||
| 439 | 1x |
return(e) |
| 440 |
} |
|
| 441 | ||
| 442 |
#' @rdname map_actions |
|
| 443 |
#' @export |
|
| 444 |
e_map_unselect <- function(e, ..., btn = NULL) {
|
|
| 445 | 1x |
opts <- list() |
| 446 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 447 | 1x |
opts$data <- list(type = "mapUnSelect", ...) |
| 448 | ||
| 449 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 450 | ||
| 451 | 1x |
return(e) |
| 452 |
} |
|
| 453 | ||
| 454 |
#' @rdname map_actions |
|
| 455 |
#' @export |
|
| 456 |
e_map_toggle_select <- function(e, ..., btn = NULL) {
|
|
| 457 | 1x |
opts <- list() |
| 458 | 1x |
if (!is.null(btn)) opts$id <- btn |
| 459 | 1x |
opts$data <- list(type = "mapToggleSelect", ...) |
| 460 | ||
| 461 | 1x |
e <- .actions_btn(e = e, opts = opts, btn = btn) |
| 462 | ||
| 463 | 1x |
return(e) |
| 464 |
} |
| 1 |
#' Add nested data |
|
| 2 |
#' |
|
| 3 |
#' Utility function to add data where the original JavaScript library expects nested data. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param param The nested parameter to add data to. |
|
| 7 |
#' @param .serie Serie's index to add the data to, if |
|
| 8 |
#' `NULL` then it is added to all. |
|
| 9 |
#' @param .data A dataset to use, if none are specified than |
|
| 10 |
#' the original dataset passed to `e_charts` is used. |
|
| 11 |
#' @param value The column to map to the parameter. |
|
| 12 |
#' |
|
| 13 |
#' @section Functions: |
|
| 14 |
#' |
|
| 15 |
#' - `e_add_nested`: Adds nested data, e.g.: |
|
| 16 |
#' `e_add_nested("itemStyle", color, fontBold)` creates
|
|
| 17 |
#' `\{itemStyle: \{color: 'red', fontBold: 'bold'\}\}`.
|
|
| 18 |
#' - `e_add_unnested`: Adds unnested data, e.g.: |
|
| 19 |
#' `e_add_unnested("symbolSize", size)` creates
|
|
| 20 |
#' `\{symbolSize: 4\}`.
|
|
| 21 |
#' |
|
| 22 |
#' @details For instance, \code{\link{e_funnel}} lets you pass \code{values} and \code{labels}
|
|
| 23 |
#' (from your initial data.frame) which corresponds to \code{name} and \code{value} in the
|
|
| 24 |
#' \href{https://echarts.apache.org/en/option.html#series-heatmap.data}{original library}.
|
|
| 25 |
#' However the latter also takes, \code{label}, \code{itemStyle}, and \code{emphasis} but being JSON arrays
|
|
| 26 |
#' they translate to lists in R and dealing with nested data.frames is not ideal. \code{e_add} remedies to that.
|
|
| 27 |
#' It allows adding those nested data points, see the examples below. |
|
| 28 |
#' |
|
| 29 |
#' @examples |
|
| 30 |
#' # funnel can take nested itemStyle |
|
| 31 |
#' # https://echarts.apache.org/en/option.html#series-funnel.data |
|
| 32 |
#' funnel <- data.frame( |
|
| 33 |
#' stage = c("View", "Click", "Purchase"),
|
|
| 34 |
#' value = c(80, 30, 20), |
|
| 35 |
#' color = c("blue", "red", "green")
|
|
| 36 |
#' ) |
|
| 37 |
#' |
|
| 38 |
#' funnel |> |
|
| 39 |
#' e_charts() |> |
|
| 40 |
#' e_funnel(value, stage) |> |
|
| 41 |
#' e_add_nested("itemStyle", color)
|
|
| 42 |
#' |
|
| 43 |
#' # Heatmap can take nested label |
|
| 44 |
#' # https://echarts.apache.org/en/option.html#series-heatmap.data |
|
| 45 |
#' v <- LETTERS[1:10] |
|
| 46 |
#' matrix <- data.frame( |
|
| 47 |
#' x = sample(v, 300, replace = TRUE), |
|
| 48 |
#' y = sample(v, 300, replace = TRUE), |
|
| 49 |
#' z = rnorm(300, 10, 1), |
|
| 50 |
#' stringsAsFactors = FALSE |
|
| 51 |
#' ) |> |
|
| 52 |
#' dplyr::group_by(x, y) |> |
|
| 53 |
#' dplyr::summarise(z = sum(z)) |> |
|
| 54 |
#' dplyr::ungroup() |> |
|
| 55 |
#' dplyr::mutate( |
|
| 56 |
#' show = TRUE, |
|
| 57 |
#' fontStyle = round(runif(dplyr::n(), 5, 12)) |
|
| 58 |
#' ) |
|
| 59 |
#' |
|
| 60 |
#' matrix |> |
|
| 61 |
#' e_charts(x) |> |
|
| 62 |
#' e_heatmap(y, z) |> |
|
| 63 |
#' e_visual_map(z) |> |
|
| 64 |
#' e_add_nested( |
|
| 65 |
#' "label", |
|
| 66 |
#' show, |
|
| 67 |
#' fontStyle |
|
| 68 |
#' ) |
|
| 69 |
#' |
|
| 70 |
#' @name nesting |
|
| 71 |
#' @export |
|
| 72 |
e_add <- function(e, param, ..., .serie = NULL, .data = NULL) {
|
|
| 73 | 1x |
.Deprecated("e_add_nested", "echarts4r")
|
| 74 | 1x |
e_add_nested(e, param, ..., .serie = NULL, .data = NULL) |
| 75 |
} |
|
| 76 | ||
| 77 |
#' @rdname nesting |
|
| 78 |
#' @export |
|
| 79 |
e_add_nested <- function(e, param, ..., .serie = NULL, .data = NULL) {
|
|
| 80 | 12x |
if (missing(e) || missing(param)) {
|
| 81 | 3x |
stop("missing e or param", call. = FALSE)
|
| 82 |
} |
|
| 83 | ||
| 84 | 9x |
if(is.null(.data)) |
| 85 | 9x |
ds <- e$x$data |
| 86 |
else |
|
| 87 | ! |
ds <- map_grps_(.data, e$x$tl) |
| 88 | ||
| 89 | 9x |
for (i in seq_along(ds)) {
|
| 90 | 10x |
data <- ds[[i]] |> |
| 91 | 10x |
dplyr::select(...) |
| 92 | ||
| 93 | 10x |
data <- apply(data, 1, as.list) |
| 94 | ||
| 95 | 10x |
for (j in seq_along(data)) {
|
| 96 | ||
| 97 | 24x |
if (!e$x$tl) {
|
| 98 | 20x |
for(k in seq_along(e$x$opts$series)){
|
| 99 | 23x |
if(!is.null(.serie) && .serie != k) |
| 100 | 3x |
next |
| 101 | 20x |
if(!is.numeric(e$x$opts$series[[k]]$data[[j]])){
|
| 102 | 17x |
e$x$opts$series[[k]]$data[[j]][[param]] <- data[[j]] |
| 103 |
} else {
|
|
| 104 |
# If polar, 'itemStyle' may be in a different location - this fixes that. |
|
| 105 | 3x |
e$x$opts$series[[k]]$data[[j]] <- c( |
| 106 | 3x |
list(value = e$x$opts$series[[k]]$data[[j]]), |
| 107 | 3x |
stats::setNames(list(data[[j]]), param) |
| 108 |
) |
|
| 109 |
} |
|
| 110 |
} |
|
| 111 |
} else {
|
|
| 112 | 4x |
for(k in seq_along(e$x$opts$options[[i]]$series)){
|
| 113 | 4x |
if(!is.null(.serie) && .serie != k) |
| 114 | ! |
next |
| 115 | 4x |
e$x$opts$options[[i]]$series[[k]]$data[[j]][[param]] <- data[[j]] |
| 116 |
} |
|
| 117 |
} |
|
| 118 |
} |
|
| 119 |
} |
|
| 120 | 9x |
e |
| 121 |
} |
|
| 122 | ||
| 123 |
#' @rdname nesting |
|
| 124 |
#' @export |
|
| 125 |
e_add_unnested <- function(e, param, value, .serie = NULL, .data = NULL) {
|
|
| 126 | 8x |
if (missing(e) || missing(param) || missing(value)) {
|
| 127 | 3x |
stop("missing e, param, or value", call. = FALSE)
|
| 128 |
} |
|
| 129 | ||
| 130 | 5x |
if(is.null(.data)) |
| 131 | 5x |
ds <- e$x$data |
| 132 |
else |
|
| 133 | ! |
ds <- map_grps_(.data, e$x$tl) |
| 134 | ||
| 135 | 5x |
for (i in seq_along(ds)) {
|
| 136 | 6x |
data <- ds[[i]] |> |
| 137 | 6x |
dplyr::pull({{ value }}) |>
|
| 138 | 6x |
unname() |
| 139 | ||
| 140 | 6x |
for (j in seq_along(data)) {
|
| 141 | 15x |
if(!is.null(.serie) && .serie != j) |
| 142 | 2x |
next |
| 143 | ||
| 144 | 13x |
if (!e$x$tl) {
|
| 145 | 9x |
if(!is.numeric(e$x$opts$series[[i]]$data[[j]])){
|
| 146 | 6x |
e$x$opts$series[[i]]$data[[j]][[param]] <- data[j] |
| 147 |
} else {
|
|
| 148 |
# If polar, 'itemStyle' may be in a different location - this fixes that.N |
|
| 149 | 3x |
e$x$opts$series[[i]]$data[[j]] <- c( |
| 150 | 3x |
list(value = e$x$opts$series[[i]]$data[[j]]), |
| 151 | 3x |
stats::setNames(list(data[[j]]), param) |
| 152 |
) |
|
| 153 |
} |
|
| 154 |
} else {
|
|
| 155 | 4x |
for(k in seq_along(e$x$opts$options[[i]]$series)){
|
| 156 | 4x |
e$x$opts$options[[i]]$series[[k]]$data[[j]][[param]] <- data[j] |
| 157 |
} |
|
| 158 |
} |
|
| 159 |
} |
|
| 160 |
} |
|
| 161 | 5x |
e |
| 162 |
} |
|
| 163 | ||
| 164 | ||
| 165 |
#' Insert binded data |
|
| 166 |
#' |
|
| 167 |
#' Utility function to attach an existing column(s) from your data to an existing series |
|
| 168 |
#' |
|
| 169 |
#' @inheritParams e_bar |
|
| 170 |
#' @param .serie Serie's index to add the data. |
|
| 171 |
#' @param .data Column names for the new data. |
|
| 172 |
#' |
|
| 173 |
#' @details This inserts new data into a series's values which allows for use in mapping things like e_visual_map |
|
| 174 |
#' or tooltip parameters. This function also works when using timelines. |
|
| 175 |
#' |
|
| 176 |
#' @examples |
|
| 177 |
#' mtcars |> |
|
| 178 |
#' e_charts(mpg) |> |
|
| 179 |
#' e_scatter(wt, scale = e_scale) |> |
|
| 180 |
#' e_insert_data(.serie = 1, .data = "qsec") |> |
|
| 181 |
#' e_visual_map(qsec, scale = e_scale, dimension = 2) |> |
|
| 182 |
#' e_tooltip(trigger = "item", |
|
| 183 |
#' formatter = htmlwidgets::JS("function(params) {
|
|
| 184 |
#' return 'mpg: ' + params.value[0] + |
|
| 185 |
#' '<br />wt: ' + params.value[1] + |
|
| 186 |
#' '<br />qsec: ' + params.value[2]; |
|
| 187 |
#' }") |
|
| 188 |
#' ) |
|
| 189 |
#' |
|
| 190 |
#' @name e_insert_data |
|
| 191 |
#' @export |
|
| 192 |
e_insert_data <- function(e, .serie, .data){
|
|
| 193 | ! |
if(!e$x$tl){
|
| 194 | ! |
for(i in seq_along(e$x$data)) {
|
| 195 | ! |
bind <- e$x$data[[i]] |> |
| 196 | ! |
dplyr::select(dplyr::all_of(.data)) |> |
| 197 | ! |
unname() |> |
| 198 | ! |
unlist() |
| 199 | ! |
for(j in seq_along(e$x$opts$series[[.serie]]$data)){
|
| 200 | ! |
e$x$opts$series[[.serie]]$data[[j]]$value <- append(e$x$opts$series[[.serie]]$data[[j]]$value, bind[j]) |
| 201 |
} |
|
| 202 |
} |
|
| 203 |
} else {
|
|
| 204 | ! |
for(i in seq_along(e$x$data)) {
|
| 205 | ! |
bind <- e$x$data[[i]] |> |
| 206 | ! |
dplyr::select(dplyr::all_of(.data)) |> |
| 207 | ! |
unname() |> |
| 208 | ! |
unlist() |
| 209 | ! |
for(j in seq_along(e$x$opts$options[[i]]$series[[.serie]]$data)){
|
| 210 | ! |
e$x$opts$options[[i]]$series[[.serie]]$data[[j]]$value <- append(e$x$opts$options[[i]]$series[[.serie]]$data[[j]]$value, bind[j]) |
| 211 |
} |
|
| 212 |
} |
|
| 213 |
} |
|
| 214 | ! |
e |
| 215 |
} |
| 1 | 549x |
`%||%` <- function(a, b) if (is.null(a)) b else a |
| 2 | ||
| 3 |
#' Add annotations to a chart |
|
| 4 |
#' |
|
| 5 |
#' @description |
|
| 6 |
#' |
|
| 7 |
#' Apache ECharts does not include a native annotation system. This is a custom |
|
| 8 |
#' function that creates annotations using SVG. This currently only works in a |
|
| 9 |
#' 'cartesian2d' coordinate system. |
|
| 10 |
#' |
|
| 11 |
#' Each annotation must be in a list with an x, y, and text. Styling can be |
|
| 12 |
#' added - see @details. Because they are SVG, it takes SVG arguments, not |
|
| 13 |
#' ECharts arguments for styling. |
|
| 14 |
#' |
|
| 15 |
#' In Shiny, to output an annotation position after dragging the box, use |
|
| 16 |
#' \code{input$id_dragged_annotation} or see \link{echarts4r-shiny}. This
|
|
| 17 |
#' captures the annotation parameters - not any of the styles. |
|
| 18 |
#' |
|
| 19 |
#' @details annotations can take the following styles to change the defaults. To |
|
| 20 |
#' remove any element use "none". |
|
| 21 |
#' |
|
| 22 |
#' - \strong{rectStyle} Styles the annotation box. It also takes \strong{shape} that uses \href{https://echarts.apache.org/en/option.html#graphic.elements-rect.shape}{echarts graphic.elements-rect.shape attributes} that controls rectangle width, height and corner radius.
|
|
| 23 |
#' |
|
| 24 |
#' - \strong{textStyle}, Styles the annotation text. This supports limited HTML tags using tspan. Supported tags are: \code{b, strong, i, em, u, br, span}. Styling inside \code{span} can use \code{color, fontSize, fontWeight, fontStyle}. \code{x} and \code{y} attributes are automatically determined based on box size, unless specified. \code{padding_trbl} was also added to add padding - this must be a list of exactly 4 integers - in the order of top, right, bottom, and left padding. This padding just pushes the text in that direction.
|
|
| 25 |
#' |
|
| 26 |
#' - \strong{lineStyle} Styles the line that connects the annotation box to the arrow using \href{https://www.w3schools.com/graphics/svg_stroking.asp}{SVG stroke attributes}
|
|
| 27 |
#' |
|
| 28 |
#' - \strong{arrowStyle}: Styles the arrow. size was added that automatically adjusts the vertices of the triangle.
|
|
| 29 |
#' |
|
| 30 |
#' @param e An echarts4r object |
|
| 31 |
#' @param annotations list of annotations to plot |
|
| 32 |
#' @param .facet integer; facet's index. Only needed for faceted plots. Defaults to first plot. |
|
| 33 |
#' panel. |
|
| 34 |
#' @param legend Whether to add annotations to legend. |
|
| 35 |
#' @param name name of the legend |
|
| 36 |
#' @param legend_color color of the legend box |
|
| 37 |
#' @param default_color color of all SVG elements, unless specified in each |
|
| 38 |
#' style. It colors text, line, arrow, and rectable outline. |
|
| 39 |
#' @param draggable booleon; if TRUE, annotations can be dragged by the user |
|
| 40 |
#' @note HTML tags may not apply in RStudio viewer, open in browser. |
|
| 41 |
#' |
|
| 42 |
#' @examples |
|
| 43 |
#' # May not work render HTML correctly in RStudio viewer |
|
| 44 |
#' # Open in browser |
|
| 45 |
#' mtcars |> |
|
| 46 |
#' e_charts(mpg) |> |
|
| 47 |
#' e_scatter(wt) |> |
|
| 48 |
#' e_annotations( |
|
| 49 |
#' annotations = list( |
|
| 50 |
#' list( |
|
| 51 |
#' id = 0, |
|
| 52 |
#' x = 15, |
|
| 53 |
#' y = 3, |
|
| 54 |
#' text = 'An annotation<br>with <i>style</i>', |
|
| 55 |
#' offsetX = 0, |
|
| 56 |
#' offsetY = -50, |
|
| 57 |
#' textStyle = list( |
|
| 58 |
#' "font-size" = 14, |
|
| 59 |
#' "font-weight" = 'bold', |
|
| 60 |
#' color = "#394", |
|
| 61 |
#' padding_trbl = list(0, 0, 5, 0) |
|
| 62 |
#' ), |
|
| 63 |
#' rectStyle = list( |
|
| 64 |
#' `stroke-dasharray` = c(35, 10), |
|
| 65 |
#' `stroke-width` = 2, |
|
| 66 |
#' # Shadow can be added! |
|
| 67 |
#' shadow = list( |
|
| 68 |
#' dx= 0, |
|
| 69 |
#' dy= 2, |
|
| 70 |
#' blur= 4, |
|
| 71 |
#' color= '#000', |
|
| 72 |
#' opacity= 0.3 |
|
| 73 |
#' ), |
|
| 74 |
#' shape = list(width = 105, height = 50, r = 0) |
|
| 75 |
#' ), |
|
| 76 |
#' lineStyle = list(`stroke-width` = 2), |
|
| 77 |
#' arrowStyle = list(size = 8) |
|
| 78 |
#' ), |
|
| 79 |
#' # No styles, will just show text |
|
| 80 |
#' list( |
|
| 81 |
#' id = 0, |
|
| 82 |
#' x = 25, |
|
| 83 |
#' y = 1, |
|
| 84 |
#' text = 'No styles', |
|
| 85 |
#' lineStyle = "none", |
|
| 86 |
#' rectStyle = "none", |
|
| 87 |
#' arrowStyle = "none" |
|
| 88 |
#' ), |
|
| 89 |
#' # Default style |
|
| 90 |
#' list( |
|
| 91 |
#' id = 2, |
|
| 92 |
#' x = 25, |
|
| 93 |
#' y = 4.5, |
|
| 94 |
#' text = "Default style", |
|
| 95 |
#' offsetX = 0, |
|
| 96 |
#' offsetY = -40 |
|
| 97 |
#' ), |
|
| 98 |
#' # left-aligned annotation |
|
| 99 |
#' list( |
|
| 100 |
#' id = 2, |
|
| 101 |
#' x = 0, |
|
| 102 |
#' y = 0.5, |
|
| 103 |
#' text = "I'm left<br>aligned!", |
|
| 104 |
#' offsetX = 60, |
|
| 105 |
#' offsetY = -40, |
|
| 106 |
#' textStyle = list( |
|
| 107 |
#' "text-anchor" = "start", |
|
| 108 |
#' padding_trbl = list(0, 0, 6, 10) |
|
| 109 |
#' ) |
|
| 110 |
#' ) |
|
| 111 |
#' )) |
|
| 112 |
#' @seealso |
|
| 113 |
#' |
|
| 114 |
#' - \href{https://www.w3schools.com/graphics/svg_text.asp}{Additional arguments for textStyle}
|
|
| 115 |
#' |
|
| 116 |
#' - \href{https://www.w3schools.com/graphics/svg_stroking.asp}{Additional arguments for rectStyle}
|
|
| 117 |
#' |
|
| 118 |
#' - \href{https://www.w3schools.com/graphics/svg_stroking.asp}{Additional
|
|
| 119 |
#' arguments for lineStyle} |
|
| 120 |
#' |
|
| 121 |
#' - \href{https://www.w3schools.com/graphics/svg_fill.asp}{Additional
|
|
| 122 |
#' arguments for arrowStyle} |
|
| 123 |
#' |
|
| 124 |
#' - \href{https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute}{Even more arguments for SVG attributes}
|
|
| 125 |
#' |
|
| 126 |
#' @rdname e_annotations |
|
| 127 |
#' @export |
|
| 128 |
e_annotations <- function( |
|
| 129 |
e, |
|
| 130 |
annotations, |
|
| 131 |
.facet = NULL, |
|
| 132 |
legend = TRUE, |
|
| 133 |
name = "Annotations", |
|
| 134 |
legend_color = '#738DE4', |
|
| 135 |
default_color = '#738DE4', |
|
| 136 |
draggable = TRUE |
|
| 137 |
) {
|
|
| 138 | 8x |
if (missing(e)) {
|
| 139 | 1x |
stop("must pass e", call. = FALSE)
|
| 140 |
} |
|
| 141 | ||
| 142 | 7x |
if (missing(annotations)) {
|
| 143 | 1x |
stop("must provide list of annotations")
|
| 144 |
} |
|
| 145 | ||
| 146 | 6x |
if (!is.list(annotations) || !is.list(annotations[[1]])) {
|
| 147 | 2x |
stop("annotations must be a list")
|
| 148 |
} |
|
| 149 | ||
| 150 | 4x |
if(!is.null(.facet) && !is.numeric(.facet)){
|
| 151 | ! |
stop(".facet must be an integer, The first facet (.facet = 1) is the default when facet is NULL.")
|
| 152 |
} |
|
| 153 | ||
| 154 | ||
| 155 | 4x |
if (!is.null(.facet)) {
|
| 156 |
# facet number (1-based R indexing) |
|
| 157 | ! |
grid_idx <- .facet - 1 |
| 158 |
} else {
|
|
| 159 | 4x |
grid_idx <- 0 |
| 160 |
} |
|
| 161 | ||
| 162 | ||
| 163 | ||
| 164 | 4x |
new_annos <- lapply(annotations, function(ann) {
|
| 165 | 4x |
processed <- process_single_annotation(ann, default_color, draggable) |
| 166 | 4x |
processed$gridIndex <- ann$gridIndex %||% grid_idx |
| 167 | 4x |
processed$legend_name <- ann$legend_name %||% name |
| 168 | 4x |
processed |
| 169 |
}) |
|
| 170 | ||
| 171 |
# Get existing annotations from chart object |
|
| 172 | 4x |
existing_annos <- e$x$annotations %||% list() |
| 173 | ||
| 174 |
# Add new annotation to list |
|
| 175 | 4x |
if (is.null(existing_annos) || length(existing_annos) == 0) {
|
| 176 | 3x |
all_annos <- new_annos |
| 177 |
} else {
|
|
| 178 | 1x |
all_annos <- existing_annos |
| 179 | 1x |
for (new_anno in new_annos) {
|
| 180 | 1x |
all_annos[[length(all_annos) + 1]] <- new_anno |
| 181 |
} |
|
| 182 |
} |
|
| 183 | ||
| 184 |
# Store back in chart object |
|
| 185 | 4x |
e$x$annotations <- all_annos |
| 186 | ||
| 187 |
# ADD DUMMY SERIES FOR LEGEND |
|
| 188 | 4x |
if (legend) {
|
| 189 |
# Extract unique legend groups |
|
| 190 | 4x |
legend_info <- list() |
| 191 | ||
| 192 | 4x |
for (i in seq_along(all_annos)) {
|
| 193 | 5x |
ann <- all_annos[[i]] |
| 194 | ||
| 195 |
# Get color |
|
| 196 | 5x |
color <- legend_color |
| 197 | ||
| 198 |
# Get grid |
|
| 199 | 5x |
grid_idx <- if (!is.null(ann$gridIndex)) {
|
| 200 | 5x |
ann$gridIndex |
| 201 |
} else {
|
|
| 202 | ! |
0 |
| 203 |
} |
|
| 204 |
# Store first occurrence |
|
| 205 | 5x |
if (is.null(legend_info[[name]])) {
|
| 206 | 4x |
legend_info[[name]] <- list( |
| 207 | 4x |
color = color, |
| 208 | 4x |
gridIndex = grid_idx |
| 209 |
) |
|
| 210 |
} |
|
| 211 |
} |
|
| 212 | ||
| 213 |
# Add a dummy series for each unique legend group |
|
| 214 | 4x |
if (is.null(e$x$opts$series)) {
|
| 215 | ! |
e$x$opts$series <- list() |
| 216 |
} |
|
| 217 | ||
| 218 | 4x |
for (group_name in names(legend_info)) {
|
| 219 | 4x |
info <- legend_info[[group_name]] |
| 220 | ||
| 221 | 4x |
dummy_series <- list( |
| 222 | 4x |
type = "scatter", |
| 223 | 4x |
name = group_name, |
| 224 | 4x |
data = list(), # Empty data |
| 225 | 4x |
symbol = "rect", |
| 226 | 4x |
symbolSize = 8, |
| 227 | 4x |
itemStyle = list( |
| 228 | 4x |
color = info$color |
| 229 |
), |
|
| 230 | 4x |
gridIndex = info$gridIndex, |
| 231 | 4x |
xAxisIndex = info$gridIndex, |
| 232 | 4x |
yAxisIndex = info$gridIndex, |
| 233 | 4x |
tooltip = list(show = FALSE), |
| 234 | 4x |
silent = TRUE, |
| 235 | 4x |
animation = FALSE, |
| 236 | 4x |
isAnnotationLegend = TRUE |
| 237 |
) |
|
| 238 | ||
| 239 | 4x |
if (isTRUE(e$x$tl)) {
|
| 240 | ! |
e$x$opts$baseOption$series[[ |
| 241 | ! |
length(e$x$opts$baseOption$series) + 1 |
| 242 | ! |
]] <- dummy_series |
| 243 |
} else {
|
|
| 244 |
# Append to series |
|
| 245 | 4x |
e$x$opts$series[[length(e$x$opts$series) + 1]] <- dummy_series |
| 246 |
} |
|
| 247 |
} |
|
| 248 |
} |
|
| 249 | ||
| 250 |
# Adding this solves the issue for the SVG line to get messed up when |
|
| 251 |
# `shiny::fluidRow(echarts4r::echarts4rOutput("chart"))` with no e_tooltip().
|
|
| 252 |
# I don't understand why this fixes it. |
|
| 253 | 4x |
if (!e$x$tl & is.null(e$x$opts$tooltip)) {
|
| 254 | 3x |
e$x$opts$tooltip <- list(trigger = 'fake') |
| 255 | 1x |
} else if ((e$x$tl & is.null(e$x$opts$tooltip))) {
|
| 256 | ! |
e$x$opts$baseOption$tooltip <- list(trigger = 'fake') |
| 257 |
} |
|
| 258 | ||
| 259 | 4x |
dep <- |
| 260 | 4x |
htmltools::htmlDependency( |
| 261 | 4x |
name = "echarts-annotations", |
| 262 | 4x |
version = "1.0.0", |
| 263 | 4x |
src = system.file("htmlwidgets/lib/echarts-6.1.0/plugins/", package = "echarts4r"),
|
| 264 | 4x |
script = "echarts-annotations.js" |
| 265 |
) |
|
| 266 | ||
| 267 | 4x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 268 | ||
| 269 | 4x |
e |> |
| 270 | 4x |
htmlwidgets::onRender( |
| 271 | 4x |
"function(el, x, data) {
|
| 272 | 4x |
function initWhenReady() {
|
| 273 | 4x |
if (typeof EChartsAnnotations === 'undefined') {
|
| 274 | 4x |
setTimeout(initWhenReady, 50); |
| 275 | 4x |
return; |
| 276 |
} |
|
| 277 | ||
| 278 | 4x |
EChartsAnnotations.initialize(el, x, data); |
| 279 |
} |
|
| 280 | ||
| 281 | 4x |
initWhenReady(); |
| 282 |
}" |
|
| 283 |
) |
|
| 284 | ||
| 285 |
} |
|
| 286 |
# R function with multiple positioning options |
|
| 287 |
#' @keywords internal |
|
| 288 |
find_text_position <- function( |
|
| 289 |
box_shape, |
|
| 290 |
position = "middle" |
|
| 291 |
) {
|
|
| 292 | ||
| 293 |
# Calculate base positions |
|
| 294 | 23x |
x_left <- box_shape$x |
| 295 | 23x |
x_middle <- box_shape$x + (box_shape$width / 2) |
| 296 | 23x |
x_right <- box_shape$x + box_shape$width |
| 297 | ||
| 298 | 23x |
y_top <- box_shape$y |
| 299 | 23x |
y_middle <- box_shape$y + (box_shape$height / 2) |
| 300 | 23x |
y_bottom <- box_shape$y + box_shape$height |
| 301 | ||
| 302 |
# Choose position |
|
| 303 | 23x |
pos <- switch( |
| 304 | 23x |
position, |
| 305 | 23x |
"middle" = list( |
| 306 | 23x |
x = x_middle, |
| 307 | 23x |
y = y_middle |
| 308 |
), |
|
| 309 | 23x |
"start" = list(x = x_left, |
| 310 | 23x |
y = y_middle), |
| 311 | 23x |
"end" = list( |
| 312 | 23x |
x = x_right, |
| 313 | 23x |
y = y_middle |
| 314 |
), |
|
| 315 |
# default to middle |
|
| 316 | 23x |
list(x = x_middle, y = y_middle) |
| 317 |
) |
|
| 318 | 23x |
pos |
| 319 |
} |
|
| 320 | ||
| 321 |
# Process a single annotation with all styles so js can read it. |
|
| 322 |
#' @keywords internal |
|
| 323 |
process_single_annotation <- function( |
|
| 324 |
ann, |
|
| 325 |
default_color = "#738DE4", |
|
| 326 |
draggable = TRUE |
|
| 327 |
) {
|
|
| 328 |
# Required fields |
|
| 329 | 26x |
if (is.null(ann$x) || is.null(ann$y) || is.null(ann$text)) {
|
| 330 | 3x |
stop("Annotation must have x, y, and text fields")
|
| 331 |
} |
|
| 332 | ||
| 333 |
# Default Properties -- |
|
| 334 |
# For these, better to use ann$arrowStyle[["fill"]] evaluation as opposed to ann$arrowStyle$fill so it does do a partial match. |
|
| 335 | ||
| 336 | 23x |
default_text_style <- list( |
| 337 | 23x |
"font-size" = 11, |
| 338 | 23x |
"font-weight" = 'bold', |
| 339 | 23x |
"text-anchor" = ann$textStyle[["text-anchor"]] %||% "middle", |
| 340 | 23x |
"dominant-baseline" = ann$textStyle[["dominant-baseline"]] %||% "central" |
| 341 |
) |
|
| 342 | ||
| 343 | 23x |
if (if_style_is_not_none(ann$lineStyle)) {
|
| 344 | 22x |
default_line_style <- list( |
| 345 | 22x |
`stroke-width` = 2, |
| 346 | 22x |
stroke = ann$lineStyle[["stroke"]] %||% default_color |
| 347 |
) |
|
| 348 |
} else {
|
|
| 349 | 1x |
default_line_style <- list() |
| 350 | 1x |
ann$lineStyle <- NULL |
| 351 |
} |
|
| 352 | ||
| 353 | 23x |
if (if_style_is_not_none(ann$arrowStyle)) {
|
| 354 | 22x |
default_arrow_style <- list( |
| 355 | 22x |
fill = ann$arrowStyle[["fill"]] %||% default_color, |
| 356 | 22x |
size = ann$arrowStyle[["size"]] %||% 8 |
| 357 |
) |
|
| 358 |
} else {
|
|
| 359 | 1x |
default_arrow_style <- list(size = 0) |
| 360 | 1x |
ann$arrowStyle <- NULL |
| 361 |
} |
|
| 362 | ||
| 363 | 23x |
if (if_style_is_not_none(ann$rectStyle)) {
|
| 364 | 22x |
default_box_style <- list( |
| 365 | 22x |
stroke = ann$rectStyle[["stroke"]] %||% default_color, |
| 366 | 22x |
fill = '#ffffff', |
| 367 | 22x |
"stroke-width" = 2 |
| 368 |
) |
|
| 369 |
} else {
|
|
| 370 |
# Fully transparent |
|
| 371 | 1x |
default_box_style <- list(fill = "rgba(255, 255, 255, 0)") |
| 372 | 1x |
ann$rectStyle <- NULL |
| 373 |
} |
|
| 374 | ||
| 375 | 23x |
default_box_width <- 80 |
| 376 | 23x |
default_box_height <- 40 |
| 377 | 23x |
default_box_radius <- 3 |
| 378 | ||
| 379 | 23x |
padding <- ann$textStyle$padding_trbl |
| 380 | ||
| 381 | 23x |
if(!is.null(padding) && length(padding) > 0){
|
| 382 | 2x |
isPadNumber <- is.numeric(unlist(padding)) |
| 383 | ||
| 384 | 2x |
if(length(padding) != 4 || isPadNumber == FALSE){
|
| 385 | 2x |
stop("padding_trbl must be a list with 4 numbers")
|
| 386 |
} |
|
| 387 |
} |
|
| 388 | ||
| 389 | 21x |
pad_top <- padding[[1]] %||% 0 |
| 390 | 21x |
pad_right <- padding[[2]] %||% 0 |
| 391 | 21x |
pad_bot <- padding[[3]] %||% 0 |
| 392 | 21x |
pad_left <- padding[[4]] %||% 0 |
| 393 | ||
| 394 |
# Find box position -- |
|
| 395 | ||
| 396 |
# None of these should ever be NA |
|
| 397 | 21x |
box_width <- ann$rectStyle$shape[["width"]] %||% default_box_width |
| 398 | 21x |
box_height <- ann$rectStyle$shape[["height"]] %||% default_box_height |
| 399 | 21x |
box_radius <- ann$rectStyle$shape[["r"]] %||% default_box_radius |
| 400 | 21x |
box_x <- ann$rectStyle$shape[["x"]] %||% -box_width / 2 |
| 401 | 21x |
box_y <- ann$rectStyle$shape[["y"]] %||% -box_height / 2 |
| 402 | ||
| 403 |
# Box shape. |
|
| 404 |
# x and y make the line connect in the middle. |
|
| 405 | 21x |
box_shape <- list( |
| 406 | 21x |
x = box_x, |
| 407 | 21x |
y = box_y, |
| 408 | 21x |
width = box_width, |
| 409 | 21x |
height = box_height, |
| 410 | 21x |
r = box_radius |
| 411 |
) |
|
| 412 | ||
| 413 |
# Calculate text position (start, end, middle) |
|
| 414 | 21x |
text_pos <- find_text_position( |
| 415 | 21x |
box_shape, |
| 416 | 21x |
position = default_text_style[["text-anchor"]] |
| 417 |
) |
|
| 418 | ||
| 419 | 21x |
final_text_style <- utils::modifyList( |
| 420 | 21x |
default_text_style, |
| 421 | 21x |
c( |
| 422 | 21x |
ann$textStyle %||% list(), |
| 423 | 21x |
list( |
| 424 | 21x |
x = (ann$textStyle$x %||% text_pos$x) + pad_left - pad_right, |
| 425 | 21x |
y = (ann$textStyle$y %||% text_pos$y) + pad_top - pad_bot, |
| 426 | 21x |
text = as.character(ann$text), |
| 427 | 21x |
fill = ann$textStyle[["color"]] %||% default_color |
| 428 |
) |
|
| 429 |
) |
|
| 430 |
) |
|
| 431 | ||
| 432 | 21x |
final_box_style <- utils::modifyList( |
| 433 | 21x |
default_box_style, |
| 434 | 21x |
c( |
| 435 | 21x |
ann$rectStyle %||% list() |
| 436 |
) |
|
| 437 |
) |
|
| 438 | ||
| 439 | 21x |
final_line_style <- utils::modifyList( |
| 440 | 21x |
default_line_style, |
| 441 | 21x |
c( |
| 442 | 21x |
ann$lineStyle %||% list() |
| 443 |
) |
|
| 444 |
) |
|
| 445 | ||
| 446 | 21x |
final_arrow_style <- utils::modifyList( |
| 447 | 21x |
default_arrow_style, |
| 448 | 21x |
c( |
| 449 | 21x |
ann$arrowStyle %||% list() |
| 450 |
) |
|
| 451 |
) |
|
| 452 | ||
| 453 | 21x |
arrow_size <- final_arrow_style$size |
| 454 | ||
| 455 | 21x |
arrow_vertices <- list( |
| 456 | 21x |
c(-arrow_size / 2, -arrow_size), |
| 457 | 21x |
c(arrow_size / 2, -arrow_size), |
| 458 | 21x |
c(0, 0) |
| 459 |
) |
|
| 460 | ||
| 461 | 21x |
list( |
| 462 | 21x |
id = ann$id %||% 0, |
| 463 | 21x |
x = ann$x, |
| 464 | 21x |
y = ann$y, |
| 465 | 21x |
text = ann$text, |
| 466 | 21x |
offsetX = ann$offsetX %||% 0, |
| 467 | 21x |
offsetY = ann$offsetY %||% 0, |
| 468 | 21x |
rectShape = box_shape, |
| 469 | 21x |
rectStyle = final_box_style, |
| 470 | 21x |
textStyle = final_text_style, |
| 471 | 21x |
lineStyle = final_line_style, |
| 472 | 21x |
arrowStyle = final_arrow_style, |
| 473 | 21x |
arrowVertices = arrow_vertices, |
| 474 | 21x |
arrowTip = -arrow_size, |
| 475 | 21x |
draggable = draggable |
| 476 |
) |
|
| 477 |
} |
|
| 478 | ||
| 479 |
#' Determine if first element is none, then no style |
|
| 480 |
#'The first element could be NULL, a style, or a vector so we must account for all these. |
|
| 481 |
#' style = NULL |
|
| 482 |
#' style = list(lineWidth = 5) |
|
| 483 |
#' style = list(shape = list(width = 100)) |
|
| 484 |
#' @keywords internal |
|
| 485 |
if_style_is_not_none <- function(style) {
|
|
| 486 | 73x |
is.null(style) || |
| 487 | 73x |
!(is.character(style[[1]]) && style[[1]] == "none") |
| 488 |
} |
| 1 |
echarts_build <- function(e) {
|
|
| 2 | 63x |
e$x$data <- NULL |
| 3 | 63x |
e$x$mapping <- NULL |
| 4 | ||
| 5 | 63x |
ff <- getOption("ECHARTS4R_FONT_FAMILY")
|
| 6 | ||
| 7 | 63x |
if (!is.null(ff)) {
|
| 8 | 1x |
e <- e_text_style(e, fontFamily = ff) |
| 9 |
} |
|
| 10 | ||
| 11 | 63x |
e |
| 12 |
} |
|
| 13 | ||
| 14 |
#' Initialise |
|
| 15 |
#' |
|
| 16 |
#' Initialise a chart. |
|
| 17 |
#' |
|
| 18 |
#' @param data A \code{data.frame} or a crosstalk \code{SharedData} object
|
|
| 19 |
#' @param e An object of class \code{echarts4r} as returned by \code{e_charts}.
|
|
| 20 |
#' @param x Column name containing x axis. |
|
| 21 |
#' @param draw Whether to draw the chart, intended to be used with |
|
| 22 |
#' \code{\link{e_draw_p}}.
|
|
| 23 |
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
|
|
| 24 |
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
|
|
| 25 |
#' string and have \code{'px'} appended.
|
|
| 26 |
#' @param elementId Id of element. |
|
| 27 |
#' @param dispose Set to \code{TRUE} to force redraw of chart, set to
|
|
| 28 |
#' \code{FALSE} to update.
|
|
| 29 |
#' @param renderer Renderer, takes \code{canvas} (default) or \code{svg}.
|
|
| 30 |
#' @param timeline Set to \code{TRUE} to build a timeline, see timeline section.
|
|
| 31 |
#' @param ... Any other argument. |
|
| 32 |
#' @param reorder Set to \code{FALSE} to not reorder numeric x axis values.
|
|
| 33 |
#' |
|
| 34 |
#' @section Details: The chart is created inside a parent \code{'<div>'}
|
|
| 35 |
#' element, the dimensions of which are controlled by the \code{'width'} and
|
|
| 36 |
#' \code{'height'} arguments. When these dimensions are small, it is possible
|
|
| 37 |
#' that the chart \code{'grid'} resizes to a size larger than the parent,
|
|
| 38 |
#' which might result in unexpected size given the input arguments. To disable |
|
| 39 |
#' this automatic readjustment, define a static \code{\link{e_grid}} like the
|
|
| 40 |
#' following: \code{'e_grid(e = current_chart, top = 0, left = 20, right = 0,
|
|
| 41 |
#' bottom = 20)'}. |
|
| 42 |
#' |
|
| 43 |
#' @section Timeline: The timeline feature currently supports the following |
|
| 44 |
#' chart types. |
|
| 45 |
#' \itemize{
|
|
| 46 |
#' \item{\code{\link{e_bar}}}
|
|
| 47 |
#' \item{\code{\link{e_line}}}
|
|
| 48 |
#' \item{\code{\link{e_step}}}
|
|
| 49 |
#' \item{\code{\link{e_area}}}
|
|
| 50 |
#' \item{\code{\link{e_scatter}}}
|
|
| 51 |
#' \item{\code{\link{e_effect_scatter}}}
|
|
| 52 |
#' \item{\code{\link{e_candle}}}
|
|
| 53 |
#' \item{\code{\link{e_heatmap}}}
|
|
| 54 |
#' \item{\code{\link{e_pie}}}
|
|
| 55 |
#' \item{\code{\link{e_line_3d}}}
|
|
| 56 |
#' \item{\code{\link{e_lines_3d}}}
|
|
| 57 |
#' \item{\code{\link{e_bar_3d}}}
|
|
| 58 |
#' \item{\code{\link{e_lines}}}
|
|
| 59 |
#' \item{\code{\link{e_scatter_3d}}}
|
|
| 60 |
#' \item{\code{\link{e_scatter_gl}}}
|
|
| 61 |
#' \item{\code{\link{e_histogram}}}
|
|
| 62 |
#' \item{\code{\link{e_lm}}}
|
|
| 63 |
#' \item{\code{\link{e_loess}}}
|
|
| 64 |
#' \item{\code{\link{e_glm}}}
|
|
| 65 |
#' \item{\code{\link{e_density}}}
|
|
| 66 |
#' \item{\code{\link{e_pictorial}}}
|
|
| 67 |
#' \item{\code{\link{e_boxplot}}}
|
|
| 68 |
#' \item{\code{\link{e_map}}}
|
|
| 69 |
#' \item{\code{\link{e_map_3d}}}
|
|
| 70 |
#' \item{\code{\link{e_line_3d}}}
|
|
| 71 |
#' \item{\code{\link{e_gauge}}}
|
|
| 72 |
#' } |
|
| 73 |
#' @section Crosstalk: TO use crosstalk, pass \code{selectedMode = TRUE}) into
|
|
| 74 |
#' the chart type argument. crosstalk functionality currently supports the |
|
| 75 |
#' following chart types (but only cartesion2d): |
|
| 76 |
#' \itemize{
|
|
| 77 |
#' \item{\code{\link{e_area}}}
|
|
| 78 |
#' \item{\code{\link{e_bar}}}
|
|
| 79 |
#' \item{\code{\link{e_candle}}}
|
|
| 80 |
#' \item{\code{\link{e_density}}}
|
|
| 81 |
#' \item{\code{\link{e_effect_scatter}}}
|
|
| 82 |
#' \item{\code{\link{e_funnel}}}
|
|
| 83 |
#' \item{\code{\link{e_heatmap}}}
|
|
| 84 |
#' \item{\code{\link{e_histogram}}}
|
|
| 85 |
#' \item{\code{\link{e_line}}}
|
|
| 86 |
#' \item{\code{\link{e_pictorial}}}
|
|
| 87 |
#' \item{\code{\link{e_pie}}}
|
|
| 88 |
#' \item{\code{\link{e_scatter}}}
|
|
| 89 |
#' \item{\code{\link{e_step}}}
|
|
| 90 |
#' } |
|
| 91 |
#' |
|
| 92 |
#' @examples |
|
| 93 |
#' mtcars |> |
|
| 94 |
#' e_charts(qsec) |> |
|
| 95 |
#' e_line(mpg) |
|
| 96 |
#' |
|
| 97 |
#' sd <- crosstalk::SharedData$new(mtcars) |
|
| 98 |
#' mtcars |> |
|
| 99 |
#' e_charts(qsec) |> |
|
| 100 |
#' e_line(mpg, selectedMode = TRUE) |
|
| 101 |
#' |
|
| 102 |
#' @import htmlwidgets |
|
| 103 |
#' @importFrom grDevices boxplot.stats |
|
| 104 |
#' @importFrom grDevices colorRampPalette |
|
| 105 |
#' @importFrom stats as.formula lm glm loess predict |
|
| 106 |
#' @importFrom graphics hist |
|
| 107 |
#' |
|
| 108 |
#' @name init |
|
| 109 |
#' @export |
|
| 110 |
e_charts <- function( |
|
| 111 |
data, |
|
| 112 |
x, |
|
| 113 |
width = NULL, |
|
| 114 |
height = NULL, |
|
| 115 |
elementId = NULL, |
|
| 116 |
dispose = TRUE, |
|
| 117 |
draw = TRUE, |
|
| 118 |
renderer = "canvas", |
|
| 119 |
timeline = FALSE, |
|
| 120 |
..., |
|
| 121 |
reorder = TRUE |
|
| 122 |
) {
|
|
| 123 | 555x |
UseMethod("e_charts")
|
| 124 |
} |
|
| 125 | ||
| 126 |
#' @rdname init |
|
| 127 |
#' @export |
|
| 128 |
e_charts.default <- function( |
|
| 129 |
data, |
|
| 130 |
x, |
|
| 131 |
width = NULL, |
|
| 132 |
height = NULL, |
|
| 133 |
elementId = NULL, |
|
| 134 |
dispose = TRUE, |
|
| 135 |
draw = TRUE, |
|
| 136 |
renderer = "canvas", |
|
| 137 |
timeline = FALSE, |
|
| 138 |
..., |
|
| 139 |
reorder = TRUE |
|
| 140 |
) {
|
|
| 141 | 555x |
xmap <- NULL |
| 142 | ||
| 143 | 555x |
if (!missing(x)) {
|
| 144 | 397x |
xmap <- deparse(substitute(x)) |
| 145 |
} |
|
| 146 | ||
| 147 |
# Crosstalk |
|
| 148 | 555x |
ct_key <- NULL |
| 149 | 555x |
ct_group <- NULL |
| 150 | 555x |
isGroupedData <- NULL |
| 151 | 555x |
isCrosstalk <- FALSE |
| 152 | 555x |
if (!missing(data) && crosstalk::is.SharedData(data)) {
|
| 153 | 2x |
ct_key <- data$key() |> as.character() |
| 154 | 2x |
ct_group <- data$groupName() |
| 155 | ||
| 156 | 2x |
isCrosstalk = TRUE |
| 157 | ||
| 158 |
# origData() returns wrong thing on grouped SharedData |
|
| 159 | 2x |
data <- (data$origData()) |
| 160 | 2x |
data$XkeyX <- ct_key # add key column |
| 161 | 2x |
isGroupedData <- dplyr::is.grouped_df(data) |
| 162 |
} |
|
| 163 | ||
| 164 |
# forward options using x |
|
| 165 | 555x |
x <- list( |
| 166 | 555x |
theme = "", |
| 167 | 555x |
tl = timeline, |
| 168 | 555x |
draw = draw, |
| 169 | 555x |
renderer = tolower(renderer), |
| 170 | 555x |
mapping = list(), |
| 171 | 555x |
events = list(), |
| 172 | 555x |
buttons = list(), |
| 173 |
# ── 2. Store crosstalk metadata on x so JS + serie functions can read it ─ |
|
| 174 | 555x |
settings = list( |
| 175 | 555x |
crosstalk_key = ct_key, |
| 176 | 555x |
crosstalk_group = ct_group |
| 177 |
), |
|
| 178 | 555x |
opts = list( |
| 179 |
..., |
|
| 180 | 555x |
yAxis = list( |
| 181 | 555x |
list(show = TRUE) |
| 182 |
) |
|
| 183 |
) |
|
| 184 |
) |
|
| 185 | ||
| 186 | 555x |
if (!missing(data)) {
|
| 187 | 501x |
row.names(data) <- NULL |
| 188 | ||
| 189 | 501x |
if (!is.null(xmap) && timeline == FALSE) {
|
| 190 | 334x |
data <- .arrange_data_x(data, xmap, reorder = reorder) |
| 191 |
} |
|
| 192 | ||
| 193 | 501x |
x$data <- map_grps_(data, timeline) |
| 194 |
} |
|
| 195 | ||
| 196 |
# Start crosstalk ------------------------------------------------------- |
|
| 197 |
# First, I'll acknowledge {echarty} for guidance on how to implement
|
|
| 198 |
# this. https://github.com/helgasoft/echarty/ |
|
| 199 | ||
| 200 |
# Crosstalk works by adding a column called 'XkeyX' so each row (i.e. data |
|
| 201 |
# point) will have a unique key. e$x$settings contains crosstalk_key and |
|
| 202 |
# crosstalk_group - assigned by crosstalk, unless specified. |
|
| 203 |
# e$x$crosstalk_grpvar contains name of group (if any) |
|
| 204 | ||
| 205 |
# When data is constructed (i.e. in e_line()), each data point will have an |
|
| 206 |
# 'XkeyX'. This key is used to identify which data was selected. This gets |
|
| 207 |
# matched using this id and datasetId. These values are found: |
|
| 208 | ||
| 209 |
# e$x$opts$dataset[[1]]$source[[1]]$XkeyX |
|
| 210 |
# e$x$opts$series[[1]]$datasetId |
|
| 211 | ||
| 212 |
# This will attach dimensions (i.e. colnames) and e$opts$dataset (i.e.) |
|
| 213 |
# js / crosstalk and the js looks for this column to grab |
|
| 214 | 555x |
if (!is.null(ct_group) & isTRUE(isGroupedData)) {
|
| 215 | ||
| 216 | 1x |
flat_data <- dplyr::ungroup(data) |
| 217 | ||
| 218 |
# Just the name of the group_by variable |
|
| 219 | 1x |
grp_var <- if (dplyr::is_grouped_df(data)) dplyr::group_vars(data)[1] else NULL |
| 220 | ||
| 221 | 1x |
source_data <- lapply(seq_len(nrow(flat_data)), function(i) {
|
| 222 | 32x |
as.list(flat_data[i, , drop = FALSE]) |
| 223 |
}) |
|
| 224 | 1x |
x$crosstalk_grpvar <- grp_var |
| 225 | 1x |
grp_vals <- as.character(unique(flat_data[[grp_var]])) |
| 226 | ||
| 227 | 1x |
grp_transforms <- lapply(grp_vals, function(g) {
|
| 228 | 3x |
list( |
| 229 | 3x |
id = paste0("Xtalk_", g),
|
| 230 | 3x |
fromDatasetIndex = 0, |
| 231 | 3x |
transform = list( |
| 232 |
# filter by group first, then by crosstalk key |
|
| 233 | 3x |
list(type = "filter", config = list(dimension = grp_var, `=` = g)), |
| 234 | 3x |
list(type = "filter", config = list(dimension = "XkeyX", reg = "^")) |
| 235 |
) |
|
| 236 |
) |
|
| 237 |
}) |
|
| 238 | ||
| 239 | 1x |
x$opts$dataset <- c( |
| 240 | 1x |
list(list(id = "source", dimensions = colnames(flat_data), source = source_data)), |
| 241 | 1x |
grp_transforms |
| 242 |
) |
|
| 243 |
} |
|
| 244 | ||
| 245 |
# after building x$opts, add the dataset + Xtalk transform |
|
| 246 | 555x |
if (!is.null(ct_group) & isFALSE(isGroupedData)) {
|
| 247 |
# convert data frame to list of rows |
|
| 248 | 1x |
source_data <- lapply(seq_len(nrow(data)), function(i) {
|
| 249 | 32x |
as.list(data[i, , drop = FALSE]) |
| 250 |
}) |
|
| 251 | ||
| 252 | 1x |
x$opts$dataset <- list( |
| 253 | 1x |
list( |
| 254 | 1x |
id = "source", |
| 255 | 1x |
dimensions = colnames(data), |
| 256 | 1x |
source = source_data |
| 257 |
), |
|
| 258 | 1x |
list( |
| 259 | 1x |
id = "Xtalk", |
| 260 | 1x |
fromDatasetId = "source", |
| 261 | 1x |
transform = list( |
| 262 | 1x |
type = "filter", |
| 263 | 1x |
config = list(dimension = "XkeyX", reg = "^") |
| 264 |
) |
|
| 265 |
) |
|
| 266 |
) |
|
| 267 |
} |
|
| 268 | ||
| 269 |
# End crosstalk ----------------------------------------------------------- |
|
| 270 | ||
| 271 | 555x |
if (!is.null(xmap)) {
|
| 272 | 397x |
x$mapping$x <- xmap[1] |
| 273 | 397x |
x$mapping$x_class <- class(data[[xmap]]) |
| 274 | 397x |
x <- .assign_axis(x, data) |
| 275 |
} |
|
| 276 | ||
| 277 | 555x |
if (isTRUE(timeline)) {
|
| 278 | 85x |
if (missing(data)) {
|
| 279 | 2x |
stop("timeline expects data", call. = FALSE)
|
| 280 |
} |
|
| 281 | ||
| 282 | 83x |
if (!dplyr::is_grouped_df(data)) {
|
| 283 | 2x |
stop("must pass grouped data when timeline = TRUE", call. = FALSE)
|
| 284 |
} |
|
| 285 | ||
| 286 | 81x |
if (!is.null(xmap)) {
|
| 287 | 62x |
x$data <- .arrange_data_by_group(x$data, xmap, reorder = reorder) |
| 288 |
} |
|
| 289 | ||
| 290 | 81x |
tl <- list( |
| 291 | 81x |
baseOption = list( |
| 292 | 81x |
yAxis = list( |
| 293 | 81x |
list(show = TRUE) |
| 294 |
) |
|
| 295 |
), |
|
| 296 | 81x |
options = purrr::map(1:dplyr::n_groups(data), function(x) list()) |
| 297 |
) |
|
| 298 | ||
| 299 | 81x |
x$opts <- tl |
| 300 | ||
| 301 | 81x |
x$opts$baseOption$timeline <- list( |
| 302 | 81x |
data = as.list(names(x$data)), |
| 303 | 81x |
axisType = "category", |
| 304 |
... |
|
| 305 |
) |
|
| 306 | ||
| 307 | 81x |
if (!is.null(xmap)) {
|
| 308 | 62x |
x$mapping$x <- xmap[1] |
| 309 | 62x |
x$mapping$x_class <- class(data[[xmap]]) |
| 310 | ||
| 311 | 62x |
x$mapping$include_x <- FALSE |
| 312 | 62x |
cl <- x$mapping$x_class |
| 313 | 62x |
if (any(c("character", "factor") %in% cl)) {
|
| 314 | 26x |
labs <- unique(data[[x$mapping$x]]) |
| 315 | ||
| 316 | 26x |
if (length(labs) == 1) {
|
| 317 | ! |
labs <- list(labs) |
| 318 |
} |
|
| 319 | ||
| 320 | 26x |
x$opts$baseOption$xAxis <- list(list(data = labs, type = "category", boundaryGap = TRUE)) |
| 321 | 36x |
} else if (any(c("POSIXct", "POSIXlt", "Date") %in% cl)) {
|
| 322 | 2x |
labs <- unique(data[[x$mapping$x]]) |
| 323 | ||
| 324 | 2x |
if (length(labs) == 1) {
|
| 325 | ! |
labs <- list(labs) |
| 326 |
} |
|
| 327 | ||
| 328 | 2x |
x$opts$baseOption$xAxis <- list(list(data = labs, type = "time", boundaryGap = TRUE)) |
| 329 |
} else {
|
|
| 330 | 34x |
x$mapping$include_x <- TRUE |
| 331 | 34x |
x$opts$baseOption$xAxis <- list(list(type = "value")) |
| 332 |
} |
|
| 333 |
} |
|
| 334 |
} |
|
| 335 | ||
| 336 | 551x |
x$dispose <- dispose |
| 337 | ||
| 338 |
# create widget |
|
| 339 | 551x |
widget <- htmlwidgets::createWidget( |
| 340 | 551x |
name = "echarts4r", |
| 341 | 551x |
x, |
| 342 | 551x |
width = width, |
| 343 | 551x |
height = height, |
| 344 | 551x |
package = "echarts4r", |
| 345 | 551x |
elementId = elementId, |
| 346 | 551x |
preRenderHook = echarts_build, |
| 347 | 551x |
dependencies = if (!is.null(ct_group)) crosstalk::crosstalkLibs() else NULL, |
| 348 | 551x |
sizingPolicy = htmlwidgets::sizingPolicy( |
| 349 | 551x |
defaultWidth = "100%", |
| 350 | 551x |
knitr.figure = FALSE, |
| 351 | 551x |
browser.fill = TRUE, |
| 352 | 551x |
padding = 0 |
| 353 |
) |
|
| 354 |
) |
|
| 355 | ||
| 356 |
# Â check for theme |
|
| 357 | 551x |
theme <- getOption("ECHARTS4R_THEME") # Â default theme
|
| 358 | 551x |
if (!is.null(theme)) {
|
| 359 | 1x |
widget <- e_theme(widget, theme) |
| 360 |
} |
|
| 361 | ||
| 362 | 551x |
return(widget) |
| 363 |
} |
|
| 364 | ||
| 365 |
# TODO is this being used? Remove it? |
|
| 366 |
#' @rdname init |
|
| 367 |
#' @method e_charts Node |
|
| 368 |
#' @export |
|
| 369 |
e_charts.Node <- function( |
|
| 370 |
data, |
|
| 371 |
x, |
|
| 372 |
width = NULL, |
|
| 373 |
height = NULL, |
|
| 374 |
elementId = NULL, |
|
| 375 |
dispose = TRUE, |
|
| 376 |
draw = TRUE, |
|
| 377 |
renderer = "canvas", |
|
| 378 |
timeline = FALSE, |
|
| 379 |
..., |
|
| 380 |
reorder = TRUE |
|
| 381 |
) {
|
|
| 382 | ! |
check_installed("data.tree")
|
| 383 | ||
| 384 | ! |
json <- data.tree::ToListExplicit(data, unname = TRUE) |
| 385 | ! |
data <- json$children[[1]]$children |
| 386 | ||
| 387 | ! |
e_charts( |
| 388 | ! |
data, |
| 389 | ! |
x, |
| 390 | ! |
width = width, |
| 391 | ! |
height = height, |
| 392 | ! |
elementId = elementId, |
| 393 | ! |
dispose = dispose, |
| 394 | ! |
draw = draw, |
| 395 | ! |
renderer = renderer, |
| 396 | ! |
timeline = timeline, |
| 397 |
..., |
|
| 398 | ! |
reorder = reorder |
| 399 |
) |
|
| 400 |
} |
|
| 401 | ||
| 402 |
#' @rdname init |
|
| 403 |
#' @export |
|
| 404 |
e_charts_ <- function( |
|
| 405 |
data, |
|
| 406 |
x = NULL, |
|
| 407 |
width = NULL, |
|
| 408 |
height = NULL, |
|
| 409 |
elementId = NULL, |
|
| 410 |
dispose = TRUE, |
|
| 411 |
draw = TRUE, |
|
| 412 |
renderer = "canvas", |
|
| 413 |
timeline = FALSE, |
|
| 414 |
..., |
|
| 415 |
reorder = TRUE |
|
| 416 |
) {
|
|
| 417 | 23x |
xmap <- x |
| 418 | ||
| 419 |
# forward options using x |
|
| 420 | 23x |
x <- list( |
| 421 | 23x |
theme = "", |
| 422 | 23x |
tl = timeline, |
| 423 | 23x |
draw = draw, |
| 424 | 23x |
renderer = tolower(renderer), |
| 425 | 23x |
mapping = list(), |
| 426 | 23x |
events = list(), |
| 427 | 23x |
buttons = list(), |
| 428 | 23x |
opts = list( |
| 429 |
..., |
|
| 430 | 23x |
yAxis = list( |
| 431 | 23x |
list(show = TRUE) |
| 432 |
) |
|
| 433 |
) |
|
| 434 |
) |
|
| 435 | ||
| 436 |
# Crosstalk |
|
| 437 | 23x |
ct_key <- NULL |
| 438 | 23x |
ct_group <- NULL |
| 439 | 23x |
isGroupedData <- NULL |
| 440 | 23x |
isCrosstalk <- FALSE |
| 441 | 23x |
if (!missing(data) && crosstalk::is.SharedData(data)) {
|
| 442 | ! |
ct_key <- data$key() |> as.character() |
| 443 | ! |
ct_group <- data$groupName() |
| 444 | ||
| 445 | ! |
isCrosstalk = TRUE |
| 446 | ||
| 447 |
# origData() returns wrong thing on grouped SharedData |
|
| 448 | ! |
data <- (data$origData()) |
| 449 | ! |
data$XkeyX <- ct_key # add key column |
| 450 | ! |
isGroupedData <- dplyr::is.grouped_df(data) |
| 451 |
} |
|
| 452 | ||
| 453 |
# forward options using x |
|
| 454 | 23x |
x <- list( |
| 455 | 23x |
theme = "", |
| 456 | 23x |
tl = timeline, |
| 457 | 23x |
draw = draw, |
| 458 | 23x |
renderer = tolower(renderer), |
| 459 | 23x |
mapping = list(), |
| 460 | 23x |
events = list(), |
| 461 | 23x |
buttons = list(), |
| 462 |
# ── 2. Store crosstalk metadata on x so JS + serie functions can read it ─ |
|
| 463 | 23x |
settings = list( |
| 464 | 23x |
crosstalk_key = ct_key, |
| 465 | 23x |
crosstalk_group = ct_group |
| 466 |
), |
|
| 467 | 23x |
opts = list( |
| 468 |
..., |
|
| 469 | 23x |
yAxis = list( |
| 470 | 23x |
list(show = TRUE) |
| 471 |
) |
|
| 472 |
) |
|
| 473 |
) |
|
| 474 | ||
| 475 | 23x |
if (!missing(data)) {
|
| 476 | 21x |
row.names(data) <- NULL |
| 477 | ||
| 478 | 21x |
if (!is.null(xmap) && timeline == FALSE) {
|
| 479 | 10x |
data <- .arrange_data_x(data, xmap, reorder = reorder) |
| 480 |
} |
|
| 481 | ||
| 482 | 21x |
x$data <- map_grps_(data, timeline) |
| 483 |
} |
|
| 484 | ||
| 485 | ||
| 486 |
# Start crosstalk ------------------------------------------------------- |
|
| 487 |
# First, I'll acknowledge {echarty} for guidance on how to implement
|
|
| 488 |
# this. https://github.com/helgasoft/echarty/ |
|
| 489 | ||
| 490 |
# Crosstalk works by adding a column called 'XkeyX' so each row (i.e. data |
|
| 491 |
# point) will have a unique key. e$x$settings contains crosstalk_key and |
|
| 492 |
# crosstalk_group - assigned by crosstalk, unless specified. |
|
| 493 |
# e$x$crosstalk_grpvar contains name of group (if any) |
|
| 494 | ||
| 495 |
# When data is constructed (i.e. in e_line()), each data point will have an |
|
| 496 |
# 'XkeyX'. This key is used to identify which data was selected. This gets |
|
| 497 |
# matched using this id and datasetId. These values are found: |
|
| 498 | ||
| 499 |
# e$x$opts$dataset[[1]]$source[[1]]$XkeyX |
|
| 500 |
# e$x$opts$series[[1]]$datasetId |
|
| 501 | ||
| 502 |
# This will attach dimensions (i.e. colnames) and e$opts$dataset (i.e.) |
|
| 503 |
# js / crosstalk and the js looks for this column to grab |
|
| 504 | 23x |
if (!is.null(ct_group) & isTRUE(isGroupedData)) {
|
| 505 | ||
| 506 | ! |
flat_data <- dplyr::ungroup(data) |
| 507 | ||
| 508 |
# Just the name of the group_by variable |
|
| 509 | ! |
grp_var <- if (dplyr::is_grouped_df(data)) dplyr::group_vars(data)[1] else NULL |
| 510 | ||
| 511 | ! |
source_data <- lapply(seq_len(nrow(flat_data)), function(i) {
|
| 512 | ! |
as.list(flat_data[i, , drop = FALSE]) |
| 513 |
}) |
|
| 514 | ! |
x$crosstalk_grpvar <- grp_var |
| 515 | ! |
grp_vals <- as.character(unique(flat_data[[grp_var]])) |
| 516 | ||
| 517 | ! |
grp_transforms <- lapply(grp_vals, function(g) {
|
| 518 | ! |
list( |
| 519 | ! |
id = paste0("Xtalk_", g),
|
| 520 | ! |
fromDatasetIndex = 0, |
| 521 | ! |
transform = list( |
| 522 |
# filter by group first, then by crosstalk key |
|
| 523 | ! |
list(type = "filter", config = list(dimension = grp_var, `=` = g)), |
| 524 | ! |
list(type = "filter", config = list(dimension = "XkeyX", reg = "^")) |
| 525 |
) |
|
| 526 |
) |
|
| 527 |
}) |
|
| 528 | ||
| 529 | ! |
x$opts$dataset <- c( |
| 530 | ! |
list(list(id = "source", dimensions = colnames(flat_data), source = source_data)), |
| 531 | ! |
grp_transforms |
| 532 |
) |
|
| 533 |
} |
|
| 534 | ||
| 535 |
# after building x$opts, add the dataset + Xtalk transform |
|
| 536 | 23x |
if (!is.null(ct_group) & isFALSE(isGroupedData)) {
|
| 537 |
# convert data frame to list of rows |
|
| 538 | ! |
source_data <- lapply(seq_len(nrow(data)), function(i) {
|
| 539 | ! |
as.list(data[i, , drop = FALSE]) |
| 540 |
}) |
|
| 541 | ||
| 542 | ! |
x$opts$dataset <- list( |
| 543 | ! |
list( |
| 544 | ! |
id = "source", |
| 545 | ! |
dimensions = colnames(data), |
| 546 | ! |
source = source_data |
| 547 |
), |
|
| 548 | ! |
list( |
| 549 | ! |
id = "Xtalk", |
| 550 | ! |
fromDatasetId = "source", |
| 551 | ! |
transform = list( |
| 552 | ! |
type = "filter", |
| 553 | ! |
config = list(dimension = "XkeyX", reg = "^") |
| 554 |
) |
|
| 555 |
) |
|
| 556 |
) |
|
| 557 |
} |
|
| 558 | ||
| 559 |
# End crosstalk ----------------------------------------------------------- |
|
| 560 | ||
| 561 | 23x |
if (!is.null(xmap)) {
|
| 562 | 18x |
x$mapping$x <- xmap[1] |
| 563 | 18x |
x$mapping$x_class <- class(data[[xmap]]) |
| 564 | 18x |
x <- .assign_axis(x, data) |
| 565 |
} |
|
| 566 | ||
| 567 | 23x |
if (isTRUE(timeline)) {
|
| 568 | 11x |
if (missing(data)) {
|
| 569 | 2x |
stop("timeline expects data", call. = FALSE)
|
| 570 |
} |
|
| 571 | ||
| 572 | 9x |
if (!dplyr::is_grouped_df(data)) {
|
| 573 | 2x |
stop("must pass grouped data when timeline = TRUE", call. = FALSE)
|
| 574 |
} |
|
| 575 | ||
| 576 | 7x |
if (!is.null(xmap)) {
|
| 577 | 7x |
x$data <- .arrange_data_by_group(x$data, xmap, reorder = reorder) |
| 578 |
} |
|
| 579 | ||
| 580 | 7x |
tl <- list( |
| 581 | 7x |
baseOption = list( |
| 582 | 7x |
yAxis = list( |
| 583 | 7x |
list(show = TRUE) |
| 584 |
) |
|
| 585 |
), |
|
| 586 | 7x |
options = purrr::map(1:dplyr::n_groups(data), function(x) list()) |
| 587 |
) |
|
| 588 | ||
| 589 | 7x |
x$opts <- tl |
| 590 | ||
| 591 | 7x |
x$opts$baseOption$timeline <- list( |
| 592 | 7x |
data = as.list(names(x$data)), |
| 593 | 7x |
axisType = "category", |
| 594 |
... |
|
| 595 |
) |
|
| 596 | ||
| 597 | 7x |
if (!is.null(xmap)) {
|
| 598 | 7x |
x$mapping$x <- xmap[1] |
| 599 | 7x |
x$mapping$x_class <- class(data[[xmap]]) |
| 600 | ||
| 601 | 7x |
x$mapping$include_x <- FALSE |
| 602 | 7x |
cl <- x$mapping$x_class |
| 603 | 7x |
if (any(cl %in% c("character", "factor"))) {
|
| 604 | 1x |
labs <- unique(data[[x$mapping$x]]) |
| 605 | ||
| 606 | 1x |
if (length(labs) == 1) {
|
| 607 | ! |
labs <- list(labs) |
| 608 |
} |
|
| 609 | ||
| 610 | 1x |
x$opts$baseOption$xAxis <- list(list(data = labs, type = "category", boundaryGap = TRUE)) |
| 611 | 6x |
} else if (any(cl %in% c("POSIXct", "POSIXlt", "Date"))) {
|
| 612 | 1x |
labs <- unique(data[[x$mapping$x]]) |
| 613 | ||
| 614 | 1x |
if (length(labs) == 1) {
|
| 615 | ! |
labs <- list(labs) |
| 616 |
} |
|
| 617 | ||
| 618 | 1x |
x$opts$baseOption$xAxis <- list(list(data = labs, type = "time", boundaryGap = TRUE)) |
| 619 |
} else {
|
|
| 620 | 5x |
x$mapping$include_x <- TRUE |
| 621 | 5x |
x$opts$baseOption$xAxis <- list(list(type = "value")) |
| 622 |
} |
|
| 623 |
} |
|
| 624 |
} |
|
| 625 | ||
| 626 | 19x |
x$dispose <- dispose |
| 627 | ||
| 628 |
# create widget |
|
| 629 | 19x |
widget <- htmlwidgets::createWidget( |
| 630 | 19x |
name = "echarts4r", |
| 631 | 19x |
x, |
| 632 | 19x |
width = width, |
| 633 | 19x |
height = height, |
| 634 | 19x |
package = "echarts4r", |
| 635 | 19x |
elementId = elementId, |
| 636 | 19x |
preRenderHook = echarts_build, |
| 637 | 19x |
sizingPolicy = htmlwidgets::sizingPolicy( |
| 638 | 19x |
defaultWidth = "100%", |
| 639 | 19x |
knitr.figure = FALSE, |
| 640 | 19x |
browser.fill = TRUE, |
| 641 | 19x |
padding = 0 |
| 642 |
) |
|
| 643 |
) |
|
| 644 | ||
| 645 |
# Â check for theme |
|
| 646 | 19x |
theme <- getOption("ECHARTS4R_THEME") # Â default theme
|
| 647 | 19x |
if (!is.null(theme)) {
|
| 648 | 1x |
widget <- e_theme(widget, theme) |
| 649 |
} |
|
| 650 | ||
| 651 | 19x |
return(widget) |
| 652 |
} |
|
| 653 | ||
| 654 |
#' @rdname init |
|
| 655 |
#' @export |
|
| 656 |
e_chart <- e_charts |
|
| 657 | ||
| 658 | ||
| 659 |
#' Add a dataset |
|
| 660 |
#' This function can add one or more dataset into a echarts object. |
|
| 661 |
#' |
|
| 662 |
#' @examples |
|
| 663 |
#' points <- mtcars[1:3, ] |
|
| 664 |
#' mtcars |> |
|
| 665 |
#' e_charts_("qsec") |>
|
|
| 666 |
#' e_line(mpg) |> |
|
| 667 |
#' e_data(points, qsec) |> |
|
| 668 |
#' e_scatter(mpg, color = "red", symbol_size = 20) |
|
| 669 |
#' @rdname init |
|
| 670 |
#' @export |
|
| 671 |
e_data <- function(e, data, x) {
|
|
| 672 | 3x |
if (missing(data)) {
|
| 673 | ! |
data <- e$x$data[[1]] |
| 674 |
} |
|
| 675 | ||
| 676 | 3x |
if (!missing(x)) {
|
| 677 | 3x |
xmap <- deparse(substitute(x)) |
| 678 |
} |
|
| 679 | ||
| 680 | 3x |
if (!missing(x)) {
|
| 681 | 3x |
e$x$mapping$x <- xmap |
| 682 | 3x |
e$x$mapping$x_class <- class(data[[xmap]]) |
| 683 | 3x |
e$x <- .assign_axis(e$x, data) |
| 684 |
} |
|
| 685 | ||
| 686 | 3x |
row.names(data) <- NULL |
| 687 | ||
| 688 | 3x |
if (!missing(x)) {
|
| 689 | 3x |
data <- .arrange_data_x(data, xmap) |
| 690 |
} |
|
| 691 | ||
| 692 | 3x |
data <- map_grps_(data, FALSE) |
| 693 | 3x |
e$x$data <- data |
| 694 | ||
| 695 | 3x |
e |
| 696 |
} |
|
| 697 | ||
| 698 |
#' Shiny bindings for echarts4r |
|
| 699 |
#' |
|
| 700 |
#' Output and render functions for using echarts4r within Shiny |
|
| 701 |
#' applications and interactive Rmd documents. |
|
| 702 |
#' |
|
| 703 |
#' @inheritParams init |
|
| 704 |
#' @param outputId output variable to read from. |
|
| 705 |
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
|
|
| 706 |
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
|
|
| 707 |
#' string and have \code{'px'} appended.
|
|
| 708 |
#' @param expr An expression that generates a echarts4r |
|
| 709 |
#' @param env The environment in which to evaluate \code{expr}.
|
|
| 710 |
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
|
|
| 711 |
#' is useful if you want to save an expression in a variable. |
|
| 712 |
#' @param id Target chart id. |
|
| 713 |
#' @param session Shiny session. |
|
| 714 |
#' |
|
| 715 |
#' @section Details: The chart is created inside a parent \code{'<div>'}
|
|
| 716 |
#' element, the dimensions of which are controlled by the \code{'width'} and
|
|
| 717 |
#' \code{'height'} arguments. When these dimensions are small, it is possible
|
|
| 718 |
#' that the chart \code{'grid'} resizes to a size larger than the parent,
|
|
| 719 |
#' which might result in unexpected size given the input arguments. To disable |
|
| 720 |
#' this automatic readjustment, define a static \code{\link{e_grid}} like the
|
|
| 721 |
#' following: \code{'e_grid(e = current_chart, top = 0, left = 20, right = 0,
|
|
| 722 |
#' bottom = 20)'}. |
|
| 723 |
#' |
|
| 724 |
#' @section Callbacks: |
|
| 725 |
#' \itemize{
|
|
| 726 |
#' \item{\code{id_brush}: returns data on brushed data points.}
|
|
| 727 |
#' \item{\code{id_legend_change}: returns series name of legend selected/unselected.}
|
|
| 728 |
#' \item{\code{id_clicked_data}: returns data of clicked data point.}
|
|
| 729 |
#' \item{\code{id_clicked_data_value}: returns value of clicked data point.}
|
|
| 730 |
#' \item{\code{id_clicked_row}: returns row number of clicked data point.}
|
|
| 731 |
#' \item{\code{id_clicked_serie}: returns name of serie of clicked data point.}
|
|
| 732 |
#' \item{\code{id_mouseover_data}: returns data on hovered data point.}
|
|
| 733 |
#' \item{\code{id_mouseover_data_value}: returns value of hovered data point.}
|
|
| 734 |
#' \item{\code{id_mouseover_row}: returns row o hovered data point.}
|
|
| 735 |
#' \item{\code{id_mouseover_serie}: returns name of serie of hovered data point.}
|
|
| 736 |
#' \item{\code{id_dragged_annotation}: returns data on dragged annotation.}
|
|
| 737 |
#' } |
|
| 738 |
#' |
|
| 739 |
#' @section Proxies: |
|
| 740 |
#' The \code{echarts4rProxy} function returns a proxy for chart which allows
|
|
| 741 |
#' manipulating a drawn chart, adding data, adding or removing series, etc. without |
|
| 742 |
#' redrawing the entire chart. |
|
| 743 |
#' |
|
| 744 |
#' \itemize{
|
|
| 745 |
#' \item{\code{\link{e_append1_p}} & \code{\link{e_append2_p}}}
|
|
| 746 |
#' \item{\code{\link{e_showtip_p}} & \code{\link{e_hidetip_p}}}
|
|
| 747 |
#' \item{\code{\link{e_highlight_p}} & \code{\link{e_downplay_p}}}
|
|
| 748 |
#' \item{\code{\link{e_focus_adjacency}} & \code{\link{e_unfocus_adjacency}}}
|
|
| 749 |
#' \item{\code{\link{e_dispatch_action_p}}}
|
|
| 750 |
#' \item{\code{\link{e_execute}}}
|
|
| 751 |
#' \item{\code{\link{e_remove_serie_p}}}
|
|
| 752 |
#' } |
|
| 753 |
#' |
|
| 754 |
#' |
|
| 755 |
#' @name echarts4r-shiny |
|
| 756 |
#' |
|
| 757 |
#' @export |
|
| 758 |
echarts4rOutput <- function(outputId, width = "100%", height = "400px") {
|
|
| 759 | 4x |
htmlwidgets::shinyWidgetOutput(outputId, "echarts4r", width, height, package = "echarts4r") |
| 760 |
} |
|
| 761 | ||
| 762 |
#' @rdname echarts4r-shiny |
|
| 763 |
#' @export |
|
| 764 |
renderEcharts4r <- function(expr, env = parent.frame(), quoted = FALSE) {
|
|
| 765 | 57x |
if (!quoted) {
|
| 766 | 56x |
expr <- substitute(expr) |
| 767 |
} # force quoted |
|
| 768 | 57x |
htmlwidgets::shinyRenderWidget(expr, echarts4rOutput, env, quoted = TRUE) |
| 769 |
} |
|
| 770 | ||
| 771 |
#' @rdname echarts4r-shiny |
|
| 772 |
#' @export |
|
| 773 |
echarts4rProxy <- function(id, data, x, timeline = FALSE, session = shiny::getDefaultReactiveDomain(), reorder = TRUE) {
|
|
| 774 | ||
| 775 | 93x |
if (missing(data) & timeline) {
|
| 776 | 1x |
stop("timeline expects data", call. = FALSE)
|
| 777 |
} |
|
| 778 | ||
| 779 | 92x |
if (missing(data)) {
|
| 780 | 9x |
proxy <- list(id = id, session = session) |
| 781 | 9x |
class(proxy) <- "echarts4rProxy" |
| 782 | 9x |
return(proxy) |
| 783 |
} |
|
| 784 | ||
| 785 | 83x |
xmap <- NULL |
| 786 | ||
| 787 | 83x |
if (!missing(x)) {
|
| 788 | 16x |
xmap <- deparse(substitute(x)) |
| 789 |
} |
|
| 790 | ||
| 791 |
# forward options using x |
|
| 792 | 83x |
x <- list( |
| 793 | 83x |
theme = "", |
| 794 | 83x |
tl = timeline, |
| 795 | 83x |
draw = TRUE, |
| 796 | 83x |
renderer = "canvas", |
| 797 | 83x |
mapping = list(), |
| 798 | 83x |
events = list(), |
| 799 | 83x |
buttons = list(), |
| 800 | 83x |
opts = list( |
| 801 | 83x |
yAxis = list( |
| 802 | 83x |
list(show = TRUE) |
| 803 |
) |
|
| 804 |
) |
|
| 805 |
) |
|
| 806 | ||
| 807 | 83x |
if (!missing(data)) {
|
| 808 | 83x |
row.names(data) <- NULL |
| 809 | ||
| 810 | 83x |
if (!is.null(xmap)) {
|
| 811 | 16x |
data <- .arrange_data_x(data, xmap, reorder = reorder) |
| 812 |
} |
|
| 813 | ||
| 814 | 83x |
x$data <- map_grps_(data, timeline) |
| 815 |
} |
|
| 816 | ||
| 817 | 83x |
if (!is.null(xmap)) {
|
| 818 | 16x |
x$mapping$x <- xmap[1] |
| 819 | 16x |
x$mapping$x_class <- class(data[[xmap]]) |
| 820 | 16x |
x <- .assign_axis(x, data) |
| 821 |
} |
|
| 822 | ||
| 823 | 83x |
if (isTRUE(timeline)) {
|
| 824 | ||
| 825 | 5x |
if (!dplyr::is_grouped_df(data)) {
|
| 826 | 1x |
stop("must pass grouped data when timeline = TRUE", call. = FALSE)
|
| 827 |
} |
|
| 828 | ||
| 829 | 4x |
if (!is.null(xmap)) {
|
| 830 | 4x |
x$data <- .arrange_data_by_group(x$data, xmap, reorder = reorder) |
| 831 |
} |
|
| 832 | ||
| 833 | 4x |
tl <- list( |
| 834 | 4x |
baseOption = list( |
| 835 | 4x |
yAxis = list( |
| 836 | 4x |
list(show = TRUE) |
| 837 |
) |
|
| 838 |
), |
|
| 839 | 4x |
options = purrr::map(1:dplyr::n_groups(data), function(x) list()) |
| 840 |
) |
|
| 841 | ||
| 842 | 4x |
x$opts <- tl |
| 843 | ||
| 844 | 4x |
x$opts$baseOption$timeline <- list( |
| 845 | 4x |
data = as.list(names(x$data)), |
| 846 | 4x |
axisType = "category" |
| 847 |
) |
|
| 848 | ||
| 849 | 4x |
if (!is.null(xmap)) {
|
| 850 | 4x |
x$mapping$x <- xmap[1] |
| 851 | 4x |
x$mapping$x_class <- class(data[[xmap]]) |
| 852 | ||
| 853 | 4x |
x$mapping$include_x <- FALSE |
| 854 | 4x |
cl <- x$mapping$x_class |
| 855 | 4x |
if (any(cl %in% c("character", "factor"))) {
|
| 856 | 1x |
labs <- unique(data[[x$mapping$x]]) |
| 857 | ||
| 858 | 1x |
if (length(labs) == 1) {
|
| 859 | ! |
labs <- list(labs) |
| 860 |
} |
|
| 861 | ||
| 862 | 1x |
x$opts$baseOption$xAxis <- list(list(data = labs, type = "category", boundaryGap = TRUE)) |
| 863 | 3x |
} else if (any(cl %in% c("POSIXct", "POSIXlt", "Date"))) {
|
| 864 | 1x |
labs <- unique(data[[x$mapping$x]]) |
| 865 | ||
| 866 | 1x |
if (length(labs) == 1) {
|
| 867 | ! |
labs <- list(labs) |
| 868 |
} |
|
| 869 | ||
| 870 | 1x |
x$opts$baseOption$xAxis <- list(list(data = labs, type = "time", boundaryGap = TRUE)) |
| 871 |
} else {
|
|
| 872 | 2x |
x$mapping$include_x <- TRUE |
| 873 | 2x |
x$opts$baseOption$xAxis <- list(list(type = "value")) |
| 874 |
} |
|
| 875 |
} |
|
| 876 |
} |
|
| 877 | ||
| 878 | 82x |
chart <- structure(list(x = x), class = c("echarts4r", class(x)))
|
| 879 | ||
| 880 | 82x |
proxy <- list(id = id, session = session, chart = chart) |
| 881 | 82x |
class(proxy) <- "echarts4rProxy" |
| 882 | ||
| 883 | 82x |
return(proxy) |
| 884 |
} |
|
| 885 | ||
| 886 |
#' @rdname echarts4r-shiny |
|
| 887 |
#' @export |
|
| 888 |
echarts4r_proxy <- echarts4rProxy |
| 1 |
.onAttach <- function(libname, pkgname) {
|
|
| 2 | 2x |
shiny::registerInputHandler("echarts4rParse", function(data, ...) {
|
| 3 | ! |
jsonlite::fromJSON(jsonlite::toJSON(data, auto_unbox = TRUE)) |
| 4 | 2x |
}, force = TRUE) |
| 5 | ||
| 6 | 2x |
options( |
| 7 | 2x |
"ECHARTS4R_THEME" = NULL, |
| 8 | 2x |
"ECHARTS4R_FONT_FAMILY" = NULL |
| 9 |
) |
|
| 10 |
} |
|
| 11 | ||
| 12 |
.onLoad <- function(libname, pkgname) {
|
|
| 13 | ! |
shiny::registerInputHandler("echarts4rParse", function(data, ...) {
|
| 14 | ! |
jsonlite::fromJSON(jsonlite::toJSON(data, auto_unbox = TRUE)) |
| 15 | ! |
}, force = TRUE) |
| 16 | ||
| 17 | ! |
options( |
| 18 | ! |
"ECHARTS4R_THEME" = NULL, |
| 19 | ! |
"ECHARTS4R_FONT_FAMILY" = NULL |
| 20 |
) |
|
| 21 |
} |
| 1 |
#' Highlight & Downplay Proxy |
|
| 2 |
#' |
|
| 3 |
#' Proxies to highlight and downplay series. |
|
| 4 |
#' |
|
| 5 |
#' @param proxy An echarts4r proxy as returned by \code{\link{echarts4rProxy}}.
|
|
| 6 |
#' @param series_index Series index, can be a vector. |
|
| 7 |
#' @param series_name Series Name, can be vector. |
|
| 8 |
#' |
|
| 9 |
#' @examples |
|
| 10 |
#' \dontrun{
|
|
| 11 |
#' library(shiny) |
|
| 12 |
#' |
|
| 13 |
#' ui <- fluidPage( |
|
| 14 |
#' fluidRow( |
|
| 15 |
#' column( |
|
| 16 |
#' 3, |
|
| 17 |
#' actionButton("highlightmpg", "Highlight MPG")
|
|
| 18 |
#' ), |
|
| 19 |
#' column( |
|
| 20 |
#' 3, |
|
| 21 |
#' actionButton("highlighthp", "Highlight HP")
|
|
| 22 |
#' ), |
|
| 23 |
#' column( |
|
| 24 |
#' 3, |
|
| 25 |
#' actionButton("downplaympg", "Downplay MPG")
|
|
| 26 |
#' ), |
|
| 27 |
#' column( |
|
| 28 |
#' 3, |
|
| 29 |
#' actionButton("downplayhp", "Downplay HP")
|
|
| 30 |
#' ) |
|
| 31 |
#' ), |
|
| 32 |
#' echarts4rOutput("plot")
|
|
| 33 |
#' ) |
|
| 34 |
#' |
|
| 35 |
#' server <- function(input, output, session) {
|
|
| 36 |
#' output$plot <- renderEcharts4r({
|
|
| 37 |
#' mtcars |> |
|
| 38 |
#' e_charts(mpg) |> |
|
| 39 |
#' e_line(disp) |> |
|
| 40 |
#' e_line(hp, name = "HP") # explicitly pass name |
|
| 41 |
#' }) |
|
| 42 |
#' |
|
| 43 |
#' # highlight |
|
| 44 |
#' |
|
| 45 |
#' observeEvent(input$highlightmpg, {
|
|
| 46 |
#' echarts4rProxy("plot") |>
|
|
| 47 |
#' e_highlight_p(series_index = 0) # using index |
|
| 48 |
#' }) |
|
| 49 |
#' |
|
| 50 |
#' observeEvent(input$highlighthp, {
|
|
| 51 |
#' echarts4rProxy("plot") |>
|
|
| 52 |
#' e_highlight_p(series_name = "HP") # using name |
|
| 53 |
#' }) |
|
| 54 |
#' |
|
| 55 |
#' # downplay |
|
| 56 |
#' |
|
| 57 |
#' observeEvent(input$downplaympg, {
|
|
| 58 |
#' echarts4rProxy("plot") |>
|
|
| 59 |
#' e_downplay_p(series_name = "disp") |
|
| 60 |
#' }) |
|
| 61 |
#' |
|
| 62 |
#' observeEvent(input$downplayhp, {
|
|
| 63 |
#' echarts4rProxy("plot") |>
|
|
| 64 |
#' e_downplay_p(series_index = 1) |
|
| 65 |
#' }) |
|
| 66 |
#' } |
|
| 67 |
#' |
|
| 68 |
#' if (interactive()) {
|
|
| 69 |
#' shinyApp(ui, server) |
|
| 70 |
#' } |
|
| 71 |
#' } |
|
| 72 |
#' |
|
| 73 |
#' @rdname highlight_proxy |
|
| 74 |
#' @export |
|
| 75 |
e_highlight_p <- function(proxy, series_index = NULL, series_name = NULL) {
|
|
| 76 | 7x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 77 | 2x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 78 |
} |
|
| 79 | ||
| 80 | 5x |
data <- list(id = proxy$id) |
| 81 | 3x |
if (!is.null(series_index)) data$seriesIndex <- series_index |
| 82 | 3x |
if (!is.null(series_name)) data$seriesName <- series_name |
| 83 | ||
| 84 | 5x |
proxy$session$sendCustomMessage("e_highlight_p", data)
|
| 85 | ||
| 86 | 5x |
return(proxy) |
| 87 |
} |
|
| 88 | ||
| 89 |
#' @rdname highlight_proxy |
|
| 90 |
#' @export |
|
| 91 |
e_downplay_p <- function(proxy, series_index = NULL, series_name = NULL) {
|
|
| 92 | 2x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 93 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 94 |
} |
|
| 95 | ||
| 96 | 1x |
data <- list(id = proxy$id) |
| 97 | ! |
if (!is.null(series_index)) data$seriesIndex <- series_index |
| 98 | ! |
if (!is.null(series_name)) data$seriesName <- series_name |
| 99 | ||
| 100 | 1x |
proxy$session$sendCustomMessage("e_downplay_p", data)
|
| 101 | ||
| 102 | 1x |
return(proxy) |
| 103 |
} |
|
| 104 | ||
| 105 |
#' Tooltip Proxy |
|
| 106 |
#' |
|
| 107 |
#' Proxies to show or hide tooltip. |
|
| 108 |
#' |
|
| 109 |
#' @inheritParams e_highlight_p |
|
| 110 |
#' @param ... Any other option, see \href{https://echarts.apache.org/en/api.html#action.tooltip}{showTip}.
|
|
| 111 |
#' |
|
| 112 |
#' @examples |
|
| 113 |
#' \dontrun{
|
|
| 114 |
#' library(shiny) |
|
| 115 |
#' |
|
| 116 |
#' ui <- fluidPage( |
|
| 117 |
#' fluidRow( |
|
| 118 |
#' actionButton("show", "Show tooltip"),
|
|
| 119 |
#' actionButton("hide", "Hide tooltip")
|
|
| 120 |
#' ), |
|
| 121 |
#' fluidRow( |
|
| 122 |
#' echarts4rOutput("plot"),
|
|
| 123 |
#' h3("clicked Data"),
|
|
| 124 |
#' verbatimTextOutput("clickedData"),
|
|
| 125 |
#' h3("clicked Serie"),
|
|
| 126 |
#' verbatimTextOutput("clickedSerie"),
|
|
| 127 |
#' h3("clicked Row"),
|
|
| 128 |
#' verbatimTextOutput("clickedRow")
|
|
| 129 |
#' ) |
|
| 130 |
#' ) |
|
| 131 |
#' |
|
| 132 |
#' server <- function(input, output, session) {
|
|
| 133 |
#' output$plot <- renderEcharts4r({
|
|
| 134 |
#' mtcars |> |
|
| 135 |
#' e_charts(mpg) |> |
|
| 136 |
#' e_line(disp, bind = carb, name = "displacement") |> |
|
| 137 |
#' e_line(hp) |> |
|
| 138 |
#' e_x_axis(min = 10) |> |
|
| 139 |
#' e_tooltip(show = FALSE) |> |
|
| 140 |
#' e_theme("westeros")
|
|
| 141 |
#' }) |
|
| 142 |
#' |
|
| 143 |
#' observeEvent(input$show, {
|
|
| 144 |
#' echarts4rProxy("plot") |>
|
|
| 145 |
#' e_showtip_p( |
|
| 146 |
#' name = "displacement", |
|
| 147 |
#' position = list(5, 5) |
|
| 148 |
#' ) |
|
| 149 |
#' }) |
|
| 150 |
#' |
|
| 151 |
#' observeEvent(input$hide, {
|
|
| 152 |
#' echarts4rProxy("plot") |>
|
|
| 153 |
#' e_hidetip_p() |
|
| 154 |
#' }) |
|
| 155 |
#' |
|
| 156 |
#' output$clickedData <- renderPrint({
|
|
| 157 |
#' input$plot_clicked_data |
|
| 158 |
#' }) |
|
| 159 |
#' |
|
| 160 |
#' output$clickedSerie <- renderPrint({
|
|
| 161 |
#' input$plot_clicked_serie |
|
| 162 |
#' }) |
|
| 163 |
#' |
|
| 164 |
#' output$clickedRow <- renderPrint({
|
|
| 165 |
#' input$plot_clicked_row |
|
| 166 |
#' }) |
|
| 167 |
#' } |
|
| 168 |
#' |
|
| 169 |
#' if (interactive()) {
|
|
| 170 |
#' shinyApp(ui, server) |
|
| 171 |
#' } |
|
| 172 |
#' } |
|
| 173 |
#' |
|
| 174 |
#' @rdname tooltip_proxy |
|
| 175 |
#' @export |
|
| 176 |
e_showtip_p <- function(proxy, ...) {
|
|
| 177 | 2x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 178 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 179 |
} |
|
| 180 | ||
| 181 | 1x |
data <- list( |
| 182 | 1x |
id = proxy$id, |
| 183 | 1x |
opts = list( |
| 184 | 1x |
type = "showTip", |
| 185 |
... |
|
| 186 |
) |
|
| 187 |
) |
|
| 188 | ||
| 189 | 1x |
proxy$session$sendCustomMessage("e_showtip_p", data)
|
| 190 | ||
| 191 | 1x |
return(proxy) |
| 192 |
} |
|
| 193 | ||
| 194 |
#' @rdname tooltip_proxy |
|
| 195 |
#' @export |
|
| 196 |
e_hidetip_p <- function(proxy) {
|
|
| 197 | 2x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 198 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 199 |
} |
|
| 200 | ||
| 201 | 1x |
data <- list(id = proxy$id) |
| 202 | ||
| 203 | 1x |
proxy$session$sendCustomMessage("e_hidetip_p", data)
|
| 204 | ||
| 205 | 1x |
return(proxy) |
| 206 |
} |
|
| 207 | ||
| 208 |
#' Node Adjacency |
|
| 209 |
#' |
|
| 210 |
#' Focus or unfocus on node adjacency. |
|
| 211 |
#' |
|
| 212 |
#' @inheritParams e_highlight_p |
|
| 213 |
#' @param index One or more node index to focus on. |
|
| 214 |
#' @param ... Any other options, see |
|
| 215 |
#' \href{https://echarts.apache.org/en/api.html#action.graph}{official documentation} and details.
|
|
| 216 |
#' |
|
| 217 |
#' @details Must pass \code{seriesId}, \code{seriesIndex}, or \code{seriesName}, generally \code{seriesIndex = 0} will work.
|
|
| 218 |
#' |
|
| 219 |
#' @examples |
|
| 220 |
#' value <- rnorm(10, 10, 2) |
|
| 221 |
#' |
|
| 222 |
#' nodes <- data.frame( |
|
| 223 |
#' name = sample(LETTERS, 10), |
|
| 224 |
#' value = value, |
|
| 225 |
#' size = value, |
|
| 226 |
#' grp = rep(c("grp1", "grp2"), 5),
|
|
| 227 |
#' stringsAsFactors = FALSE |
|
| 228 |
#' ) |
|
| 229 |
#' |
|
| 230 |
#' edges <- data.frame( |
|
| 231 |
#' source = sample(nodes$name, 20, replace = TRUE), |
|
| 232 |
#' target = sample(nodes$name, 20, replace = TRUE), |
|
| 233 |
#' stringsAsFactors = FALSE |
|
| 234 |
#' ) |
|
| 235 |
#' \dontrun{
|
|
| 236 |
#' |
|
| 237 |
#' library(shiny) |
|
| 238 |
#' |
|
| 239 |
#' ui <- fluidPage( |
|
| 240 |
#' fluidRow( |
|
| 241 |
#' column( |
|
| 242 |
#' 2, |
|
| 243 |
#' numericInput("index", "Node", value = 3, min = 1, max = 9)
|
|
| 244 |
#' ), |
|
| 245 |
#' column( |
|
| 246 |
#' 2, |
|
| 247 |
#' br(), |
|
| 248 |
#' actionButton("focus", "Focus")
|
|
| 249 |
#' ), |
|
| 250 |
#' column( |
|
| 251 |
#' 2, |
|
| 252 |
#' br(), |
|
| 253 |
#' actionButton("unfocus", "Unfocus")
|
|
| 254 |
#' ) |
|
| 255 |
#' ), |
|
| 256 |
#' fluidRow( |
|
| 257 |
#' column(12, echarts4rOutput("graph"))
|
|
| 258 |
#' ) |
|
| 259 |
#' ) |
|
| 260 |
#' |
|
| 261 |
#' server <- function(input, output, session) {
|
|
| 262 |
#' output$graph <- renderEcharts4r({
|
|
| 263 |
#' e_charts() |> |
|
| 264 |
#' e_graph() |> |
|
| 265 |
#' e_graph_nodes(nodes, name, value, size, grp) |> |
|
| 266 |
#' e_graph_edges(edges, source, target) |
|
| 267 |
#' }) |
|
| 268 |
#' |
|
| 269 |
#' observeEvent(input$focus, {
|
|
| 270 |
#' echarts4rProxy("graph") |>
|
|
| 271 |
#' e_focus_adjacency_p( |
|
| 272 |
#' seriesIndex = 0, |
|
| 273 |
#' index = input$index |
|
| 274 |
#' ) |
|
| 275 |
#' }) |
|
| 276 |
#' |
|
| 277 |
#' observeEvent(input$unfocus, {
|
|
| 278 |
#' echarts4rProxy("graph") |>
|
|
| 279 |
#' e_unfocus_adjacency_p(seriesIndex = 0) |
|
| 280 |
#' }) |
|
| 281 |
#' } |
|
| 282 |
#' |
|
| 283 |
#' if (interactive()) {
|
|
| 284 |
#' shinyApp(ui, server) |
|
| 285 |
#' } |
|
| 286 |
#' } |
|
| 287 |
#' |
|
| 288 |
#' @rdname node_adjacency |
|
| 289 |
#' @export |
|
| 290 |
e_focus_adjacency_p <- function(proxy, index, ...) {
|
|
| 291 | ! |
.Deprecated("Dropped in v5 use: emphasis = list(focus = 'adjacency')")
|
| 292 | ! |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 293 | ! |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 294 |
} |
|
| 295 | ||
| 296 | ! |
args <- list(...) |
| 297 | ||
| 298 | ! |
hasArgs <- c("seriesId", "seriesIndex", "seriesName") %in% names(args)
|
| 299 | ||
| 300 | ! |
if (sum(hasArgs) != 1) {
|
| 301 | ! |
stop("Must pass one of seriesId, seriesIndex, or seriesName", call. = FALSE)
|
| 302 |
} |
|
| 303 | ||
| 304 | ! |
data <- purrr::map(index, function(i, id) {
|
| 305 | ! |
list( |
| 306 | ! |
id = id, |
| 307 | ! |
opts = list( |
| 308 | ! |
type = "focusNodeAdjacency", |
| 309 | ! |
dataIndex = i, |
| 310 |
... |
|
| 311 |
) |
|
| 312 |
) |
|
| 313 | ! |
}, id = proxy$id) |
| 314 | ||
| 315 | ! |
proxy$session$sendCustomMessage("e_focus_node_adjacency_p", data)
|
| 316 | ||
| 317 | ! |
return(proxy) |
| 318 |
} |
|
| 319 | ||
| 320 |
#' @rdname node_adjacency |
|
| 321 |
#' @export |
|
| 322 |
e_unfocus_adjacency_p <- function(proxy, ...) {
|
|
| 323 | ! |
.Deprecated("Dropped in v5 use: emphasis = list(focus = 'adjacency')")
|
| 324 | ! |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 325 | ! |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 326 |
} |
|
| 327 | ||
| 328 | ! |
args <- list(...) |
| 329 | ||
| 330 | ! |
hasArgs <- c("seriesId", "seriesIndex", "seriesName") %in% names(args)
|
| 331 | ||
| 332 | ! |
if (sum(hasArgs) != 1) {
|
| 333 | ! |
stop("Must pass one of seriesId, seriesIndex, or seriesName", call. = FALSE)
|
| 334 |
} |
|
| 335 | ||
| 336 | ! |
data <- list( |
| 337 | ! |
id = proxy$id, |
| 338 | ! |
opts = list( |
| 339 | ! |
type = "unfocusNodeAdjacency", |
| 340 |
... |
|
| 341 |
) |
|
| 342 |
) |
|
| 343 | ||
| 344 | ! |
proxy$session$sendCustomMessage("e_unfocus_node_adjacency_p", data)
|
| 345 | ||
| 346 | ! |
return(proxy) |
| 347 |
} |
|
| 348 | ||
| 349 |
#' Dispatch Action |
|
| 350 |
#' |
|
| 351 |
#' Create your own proxies, essentially a wrapper around the |
|
| 352 |
#' \href{https://echarts.apache.org/en/api.html#action}{action API}.
|
|
| 353 |
#' |
|
| 354 |
#' @inheritParams e_highlight_p |
|
| 355 |
#' @param type Type of action to dispatch, i.e.: \code{highlight}.
|
|
| 356 |
#' @param ... Named options. |
|
| 357 |
#' |
|
| 358 |
#' @examples |
|
| 359 |
#' \dontrun{
|
|
| 360 |
#' |
|
| 361 |
#' library(shiny) |
|
| 362 |
#' |
|
| 363 |
#' ui <- fluidPage( |
|
| 364 |
#' fluidRow( |
|
| 365 |
#' column(8, echarts4rOutput("chart")),
|
|
| 366 |
#' column(4, actionButton("zoom", "Zoom"))
|
|
| 367 |
#' ) |
|
| 368 |
#' ) |
|
| 369 |
#' |
|
| 370 |
#' server <- function(input, output, session) {
|
|
| 371 |
#' output$chart <- renderEcharts4r({
|
|
| 372 |
#' cars |> |
|
| 373 |
#' e_charts(speed) |> |
|
| 374 |
#' e_scatter(dist) |> |
|
| 375 |
#' e_datazoom() |
|
| 376 |
#' }) |
|
| 377 |
#' |
|
| 378 |
#' observe({
|
|
| 379 |
#' req(input$zoom) |
|
| 380 |
#' |
|
| 381 |
#' echarts4rProxy("chart") |>
|
|
| 382 |
#' e_dispatch_action_p("dataZoom", startValue = 1, endValue = 10)
|
|
| 383 |
#' }) |
|
| 384 |
#' } |
|
| 385 |
#' |
|
| 386 |
#' if (interactive()) {
|
|
| 387 |
#' shinyApp(ui, server) |
|
| 388 |
#' } |
|
| 389 |
#' } |
|
| 390 |
#' |
|
| 391 |
#' @export |
|
| 392 |
e_dispatch_action_p <- function(proxy, type, ...) {
|
|
| 393 | 3x |
if (missing(proxy) || missing(type)) {
|
| 394 | 1x |
stop("must pass proxy and type", call. = FALSE)
|
| 395 |
} |
|
| 396 | ||
| 397 | 2x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 398 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 399 |
} |
|
| 400 | ||
| 401 | 1x |
data <- list(id = proxy$id, opts = list(type = type, ...)) |
| 402 | ||
| 403 | 1x |
proxy$session$sendCustomMessage("e_dispatch_action_p", data)
|
| 404 |
} |
|
| 405 | ||
| 406 | ||
| 407 |
#' Capture event |
|
| 408 |
#' |
|
| 409 |
#' Add an event capture. |
|
| 410 |
#' |
|
| 411 |
#' @inheritParams e_bar |
|
| 412 |
#' @param event An event name from the \href{https://echarts.apache.org/en/api.html#events}{event documentation}.
|
|
| 413 |
#' |
|
| 414 |
#' @details Many events can be captured, however not all are integrated, you can pass one that is not implemented with this function. |
|
| 415 |
#' |
|
| 416 |
#' @examples |
|
| 417 |
#' \dontrun{
|
|
| 418 |
#' # add datazoom |
|
| 419 |
#' library(shiny) |
|
| 420 |
#' |
|
| 421 |
#' ui <- fluidPage( |
|
| 422 |
#' echarts4rOutput("chart"),
|
|
| 423 |
#' verbatimTextOutput("zoom")
|
|
| 424 |
#' ) |
|
| 425 |
#' |
|
| 426 |
#' server <- function(input, output) {
|
|
| 427 |
#' output$chart <- renderEcharts4r({
|
|
| 428 |
#' mtcars |> |
|
| 429 |
#' e_charts(mpg) |> |
|
| 430 |
#' e_scatter(qsec) |> |
|
| 431 |
#' e_datazoom() |> |
|
| 432 |
#' e_capture("datazoom")
|
|
| 433 |
#' }) |
|
| 434 |
#' |
|
| 435 |
#' output$zoom <- renderPrint({
|
|
| 436 |
#' input$chart_datazoom |
|
| 437 |
#' }) |
|
| 438 |
#' } |
|
| 439 |
#' |
|
| 440 |
#' if (interactive()) {
|
|
| 441 |
#' shinyApp(ui, server) |
|
| 442 |
#' } |
|
| 443 |
#' } |
|
| 444 |
#' |
|
| 445 |
#' @export |
|
| 446 |
e_capture <- function(e, event) {
|
|
| 447 | 1x |
e$x$capture <- event |
| 448 | 1x |
e |
| 449 |
} |
|
| 450 | ||
| 451 |
#' Draw |
|
| 452 |
#' |
|
| 453 |
#' Draw the chart. |
|
| 454 |
#' |
|
| 455 |
#' @inheritParams e_highlight_p |
|
| 456 |
#' |
|
| 457 |
#' @examples |
|
| 458 |
#' \dontrun{
|
|
| 459 |
#' library(shiny) |
|
| 460 |
#' |
|
| 461 |
#' ui <- fluidPage( |
|
| 462 |
#' echarts4rOutput("chart"),
|
|
| 463 |
#' actionButton("draw", "draw")
|
|
| 464 |
#' ) |
|
| 465 |
#' |
|
| 466 |
#' server <- function(input, output) {
|
|
| 467 |
#' output$chart <- renderEcharts4r({
|
|
| 468 |
#' mtcars |> |
|
| 469 |
#' e_charts(mpg, draw = FALSE) |> |
|
| 470 |
#' e_scatter(qsec) |> |
|
| 471 |
#' e_datazoom() |
|
| 472 |
#' }) |
|
| 473 |
#' |
|
| 474 |
#' observeEvent(input$draw, {
|
|
| 475 |
#' echarts4rProxy("chart") |>
|
|
| 476 |
#' e_draw_p() |
|
| 477 |
#' }) |
|
| 478 |
#' } |
|
| 479 |
#' |
|
| 480 |
#' if (interactive()) {
|
|
| 481 |
#' shinyApp(ui, server) |
|
| 482 |
#' } |
|
| 483 |
#' } |
|
| 484 |
#' |
|
| 485 |
#' @details Useful if you set \code{draw} to \code{FALSE} in \code{\link{e_charts}}.
|
|
| 486 |
#' |
|
| 487 |
#' @export |
|
| 488 |
e_draw_p <- function(proxy) {
|
|
| 489 | 3x |
if (missing(proxy)) {
|
| 490 | 1x |
stop("must pass proxy and type", call. = FALSE)
|
| 491 |
} |
|
| 492 | ||
| 493 | 2x |
if (!"echarts4rProxy" %in% class(proxy)) {
|
| 494 | 1x |
stop("must pass echarts4rProxy object", call. = FALSE)
|
| 495 |
} |
|
| 496 | ||
| 497 | 1x |
data <- list(id = proxy$id) |
| 498 | ||
| 499 | 1x |
proxy$session$sendCustomMessage("e_draw_p", data)
|
| 500 |
} |
|
| 501 | ||
| 502 |
#' Resize |
|
| 503 |
#' |
|
| 504 |
#' Force resize the chart. |
|
| 505 |
#' |
|
| 506 |
#' @inheritParams e_highlight_p |
|
| 507 |
#' |
|
| 508 |
#' @export |
|
| 509 | 1x |
e_resize <- function(proxy) UseMethod("e_resize")
|
| 510 | ||
| 511 |
#' @export |
|
| 512 |
#' @method e_resize echarts4rProxy |
|
| 513 |
e_resize.echarts4rProxy <- function(proxy) {
|
|
| 514 | 1x |
data <- list(id = proxy$id) |
| 515 | 1x |
proxy$session$sendCustomMessage("e_resize", data)
|
| 516 | 1x |
return(proxy) |
| 517 |
} |
| 1 |
#' Facet |
|
| 2 |
#' |
|
| 3 |
#' Create facets for multiple plots. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param rows,cols Number of rows and columns. If both are `NULL` the number of rows and columns |
|
| 7 |
#' will be determined automatically. |
|
| 8 |
#' @param legend_pos Position of the legend. One of "top", "right", "bottom", "left". Determines |
|
| 9 |
#' to which side the `legend_space` argument applies. |
|
| 10 |
#' @param legend_space Space between legend and plot area. The entered number will be used |
|
| 11 |
#' as percentage. |
|
| 12 |
#' @param margin_trbl Adjusts the size of the outside margin around the plotting area. Default is |
|
| 13 |
#' `c(t = 2, r = 2, b = 5, l = 2)`. Numbers are used as percentage of total plotting area. To |
|
| 14 |
#' change only e.g. two sides `c("r" = 8, "l" = 8)` could be used, other sides will use
|
|
| 15 |
#' defaults. |
|
| 16 |
#' @param h_panel_space,v_panel_space Horizontal and vertical spacing between the individual grid |
|
| 17 |
#' elements. Expects numeric input, which will be used as percentage of total plotting area. |
|
| 18 |
#' Default `NULL` will automatically add some panel spacing for low dimensional grids. |
|
| 19 |
#' |
|
| 20 |
#' |
|
| 21 |
#' @examples |
|
| 22 |
#' group_size <- 20 |
|
| 23 |
#' n_groups <- 13 |
|
| 24 |
#' df <- data.frame("day" = rep(1:group_size, times=n_groups),
|
|
| 25 |
#' "temperature" = runif(group_size * n_groups, 10, 40), |
|
| 26 |
#' "location" = rep(LETTERS[1:n_groups], each=group_size)) |
|
| 27 |
#' |
|
| 28 |
#' df |> |
|
| 29 |
#' group_by(location) |> |
|
| 30 |
#' e_charts(day) |> |
|
| 31 |
#' e_line(temperature) |> |
|
| 32 |
#' e_facet(rows = 4, cols=4, legend_pos = "top", legend_space = 12) |
|
| 33 |
#' |
|
| 34 |
#' @details Each serie, i.e.: \code{\link{e_bar}} will be plotted against a facet.
|
|
| 35 |
#' |
|
| 36 |
#' |
|
| 37 |
#' @importFrom grDevices n2mfrow |
|
| 38 |
#' |
|
| 39 |
#' |
|
| 40 |
#' @export |
|
| 41 |
#' @importFrom grDevices n2mfrow |
|
| 42 |
e_facet <- function(e, |
|
| 43 |
rows = NULL, |
|
| 44 |
cols = NULL, |
|
| 45 |
legend_pos = "top", |
|
| 46 |
legend_space = 10, |
|
| 47 |
margin_trbl = c(t = 2, r = 2, b = 5, l = 2), |
|
| 48 |
h_panel_space = NULL, |
|
| 49 |
v_panel_space = NULL) {
|
|
| 50 | ||
| 51 | ||
| 52 |
# Generate default values --------------------------------------------------------------------- |
|
| 53 | ||
| 54 |
# number of series |
|
| 55 | 15x |
nseries <- length(e$x$opts$series) |
| 56 | ||
| 57 | 15x |
if (is.null(rows) && is.null(cols)) {
|
| 58 |
# Automatically generate grid dimensions |
|
| 59 | 1x |
dims <- n2mfrow(nseries) |
| 60 | 1x |
rows <- dims[2] |
| 61 | 1x |
cols <- dims[1] |
| 62 | 14x |
} else if (is.null(rows) || is.null(cols)) {
|
| 63 | 2x |
stop("Must provide both the number of rows and columns.")
|
| 64 |
} else {
|
|
| 65 |
# Do series match the grid? |
|
| 66 | 12x |
if (nseries > rows * cols) {
|
| 67 | 1x |
warning(paste0(nseries, " series exceed the number of panels created by ", |
| 68 | 1x |
rows, " rows and ", cols, " columns.")) |
| 69 |
} |
|
| 70 |
} |
|
| 71 | ||
| 72 |
# Specified margins |
|
| 73 | 13x |
spec_margin <- c("t", "r", "b", "l") %in% names(margin_trbl) # see which elements are present
|
| 74 | 13x |
margin_trbl <- c(margin_trbl, # add missing elements |
| 75 | 13x |
c(t = 2, r = 2, b = 5, l = 5)[!spec_margin]) |
| 76 | ||
| 77 |
# Calculate spacings ------------------------------------------------------ |
|
| 78 | ||
| 79 |
# introduce some spacing between panels for low dimensional grids |
|
| 80 | 13x |
if (is.null(v_panel_space)) {
|
| 81 | 12x |
v_panel_space <- if (rows < 10) 10 - rows else 0 |
| 82 |
} else {
|
|
| 83 | 1x |
v_panel_space |
| 84 |
} |
|
| 85 | ||
| 86 | 13x |
if (is.null(h_panel_space)) {
|
| 87 | 12x |
h_panel_space <- if (cols < 10) 10 - cols else 0 |
| 88 |
} else {
|
|
| 89 | 1x |
h_panel_space |
| 90 |
} |
|
| 91 | ||
| 92 | ||
| 93 |
# Maximum space for facets (depends on legend position, and space for axis labels) |
|
| 94 | 13x |
if (legend_pos %in% c("top", "bottom")) {
|
| 95 | 10x |
w_max <- 100 - margin_trbl[["l"]] - margin_trbl[["r"]] |
| 96 | 10x |
h_max <- 100 - legend_space - margin_trbl[["b"]] - margin_trbl[["t"]] |
| 97 | 3x |
} else if (legend_pos %in% c("left", "right")){
|
| 98 | 2x |
w_max <- 100 - legend_space - margin_trbl[["l"]] - margin_trbl[["r"]] |
| 99 | 2x |
h_max <- 100 - margin_trbl[["b"]] -margin_trbl[["t"]] |
| 100 |
} else {
|
|
| 101 | 1x |
w_max <- 100 - margin_trbl[["l"]] - margin_trbl[["r"]] |
| 102 | 1x |
h_max <- 100 - margin_trbl[["b"]] - margin_trbl[["t"]] |
| 103 |
} |
|
| 104 | ||
| 105 |
# Total space for panels, taking between-panel spacing, legend space, and |
|
| 106 |
# extra space for axis labels into account |
|
| 107 | 13x |
rows_h_max <- h_max - (v_panel_space * (rows-1)) |
| 108 | 13x |
cols_w_max <- w_max - (h_panel_space * (cols-1)) |
| 109 | ||
| 110 |
# Dimensions of each panel |
|
| 111 | 13x |
height <- rows_h_max / rows |
| 112 | 13x |
width <- cols_w_max / cols |
| 113 | ||
| 114 | ||
| 115 |
# Panel positions --------------------------------------------------------- |
|
| 116 | ||
| 117 |
# Offset only when legend is left or top |
|
| 118 | 13x |
top_offset <- 0 |
| 119 | 13x |
left_offset <- 0 |
| 120 | 13x |
if (legend_pos == "top") {
|
| 121 | 9x |
top_offset <- legend_space |
| 122 |
} |
|
| 123 | 13x |
if (legend_pos == "left") {
|
| 124 | 1x |
left_offset <- legend_space |
| 125 |
} |
|
| 126 | ||
| 127 |
# Generate a vector for positions from the top |
|
| 128 | 13x |
top_pos_values <- vapply( |
| 129 | 13x |
X = seq_len(rows), |
| 130 | 13x |
FUN = function(x) {
|
| 131 | 22x |
margin_trbl[["t"]] + top_offset + ((x - 1) * (height + v_panel_space)) |
| 132 |
}, |
|
| 133 | 13x |
FUN.VALUE = numeric(1) |
| 134 |
) |
|
| 135 | ||
| 136 | 13x |
top_positions <- rep(top_pos_values, each=cols)[1:nseries] |
| 137 | ||
| 138 |
# Generate a vector for positions from the left |
|
| 139 | 13x |
left_pos_values <- vapply( |
| 140 | 13x |
X = seq_len(cols), |
| 141 | 13x |
FUN = function(x) {
|
| 142 | 26x |
margin_trbl[["l"]] + left_offset + ((x-1) * (width + h_panel_space)) |
| 143 |
}, |
|
| 144 | 13x |
FUN.VALUE = numeric(1) |
| 145 |
) |
|
| 146 | ||
| 147 | 13x |
left_positions <- rep(left_pos_values, times=rows)[1:nseries] |
| 148 | ||
| 149 | ||
| 150 |
# Create grid ------------------------------------------------------------- |
|
| 151 | ||
| 152 |
# Act differently depending on chart types |
|
| 153 | 13x |
chart_type <- e$x$opts$series[[1]]$type |
| 154 | ||
| 155 |
# No chart type found |
|
| 156 | 1x |
if (is.null(chart_type)) stop("No chart type found for first series")
|
| 157 | ||
| 158 |
# When it is a pie chart |
|
| 159 | 12x |
if (chart_type == "pie") {
|
| 160 | 1x |
for (i in 1:nseries) {
|
| 161 | ||
| 162 | 2x |
e$x$opts$series[[i]]$height <- paste0(height, "%") |
| 163 | 2x |
e$x$opts$series[[i]]$width <- paste0(width, "%") |
| 164 | 2x |
e$x$opts$series[[i]]$top <- paste0(top_positions[i], "%") |
| 165 | 2x |
e$x$opts$series[[i]]$left <- paste0(left_positions[i], "%") |
| 166 | 2x |
e$x$opts$series[[i]]$avoidLabelOverlap <- FALSE # I noticed that many labels stay hidden when this is on the default: TRUE |
| 167 | ||
| 168 |
} |
|
| 169 |
} |
|
| 170 | ||
| 171 |
# For any other chart type, might not always produce desired results |
|
| 172 |
else {
|
|
| 173 | ||
| 174 | 11x |
for (i in 1:nseries) {
|
| 175 |
# Create new grid elements |
|
| 176 | 27x |
e <- e |> |
| 177 | 27x |
e_grid( |
| 178 | 27x |
height = paste0(height, "%"), |
| 179 | 27x |
width = paste0(width, "%"), |
| 180 | 27x |
top = paste0(top_positions[i], "%"), |
| 181 | 27x |
left = paste0(left_positions[i], "%") |
| 182 |
) |
|
| 183 | ||
| 184 |
# Create new axes for the series, that mimic the first axes |
|
| 185 | 27x |
e$x$opts$xAxis[[i]] <- e$x$opts$xAxis[[1]] |
| 186 | 27x |
e$x$opts$yAxis[[i]] <- e$x$opts$yAxis[[1]] |
| 187 | ||
| 188 |
# Assign the new axes to the new grid element |
|
| 189 | 27x |
e$x$opts$xAxis[[i]]$gridIndex <- i - 1 |
| 190 | 27x |
e$x$opts$yAxis[[i]]$gridIndex <- i - 1 |
| 191 | ||
| 192 |
# Dictate to which grid panel the series will be assigned |
|
| 193 | 27x |
e$x$opts$series[[i]]$yAxisIndex <- i - 1 |
| 194 | 27x |
e$x$opts$series[[i]]$xAxisIndex <- i - 1 |
| 195 | ||
| 196 |
} |
|
| 197 |
} |
|
| 198 | ||
| 199 | 12x |
e |
| 200 |
} |
| 1 |
#' Choropleth |
|
| 2 |
#' |
|
| 3 |
#' Draw maps. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param serie Values to plot. |
|
| 7 |
#' @param map Map type. |
|
| 8 |
#' @param coord_system Coordinate system to use, one of \code{cartesian3D}, \code{geo3D}, \code{globe}.
|
|
| 9 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 10 |
#' @param id,value,height Columns corresponding to registered map. |
|
| 11 |
#' |
|
| 12 |
#' @examples |
|
| 13 |
#' \dontrun{
|
|
| 14 |
#' choropleth <- data.frame( |
|
| 15 |
#' countries = c( |
|
| 16 |
#' "France", |
|
| 17 |
#' "Brazil", |
|
| 18 |
#' "China", |
|
| 19 |
#' "Russia", |
|
| 20 |
#' "Canada", |
|
| 21 |
#' "India", |
|
| 22 |
#' "United States", |
|
| 23 |
#' "Argentina", |
|
| 24 |
#' "Australia" |
|
| 25 |
#' ), |
|
| 26 |
#' values = round(runif(9, 10, 25)) |
|
| 27 |
#' ) |
|
| 28 |
#' |
|
| 29 |
#' choropleth |> |
|
| 30 |
#' e_charts(countries) |> |
|
| 31 |
#' e_map(values) |> |
|
| 32 |
#' e_visual_map(min = 10, max = 25) |
|
| 33 |
#' |
|
| 34 |
#' choropleth |> |
|
| 35 |
#' e_charts(countries) |> |
|
| 36 |
#' e_map_3d(values, shading = "lambert") |> |
|
| 37 |
#' e_visual_map(min = 10, max = 30) |
|
| 38 |
#' |
|
| 39 |
#' # custom |
|
| 40 |
#' buildings <- jsonlite::read_json( |
|
| 41 |
#' paste0( |
|
| 42 |
#' "https://echarts.apache.org/examples/", |
|
| 43 |
#' "data-gl/asset/data/buildings.json" |
|
| 44 |
#' ) |
|
| 45 |
#' ) |
|
| 46 |
#' |
|
| 47 |
#' heights <- purrr::map(buildings$features, "properties") |> |
|
| 48 |
#' purrr::map("height") |>
|
|
| 49 |
#' unlist() |
|
| 50 |
#' |
|
| 51 |
#' names <- purrr::map(buildings$features, "properties") |> |
|
| 52 |
#' purrr::map("name") |>
|
|
| 53 |
#' unlist() |
|
| 54 |
#' |
|
| 55 |
#' data <- dplyr::tibble( |
|
| 56 |
#' name = names, |
|
| 57 |
#' value = round(runif(length(names), 0, 1), 6), |
|
| 58 |
#' height = heights / 10 |
|
| 59 |
#' ) |
|
| 60 |
#' |
|
| 61 |
#' data |> |
|
| 62 |
#' e_charts() |> |
|
| 63 |
#' e_map_register("buildings", buildings) |>
|
|
| 64 |
#' e_map_3d_custom(name, value, height) |> |
|
| 65 |
#' e_visual_map( |
|
| 66 |
#' show = FALSE, |
|
| 67 |
#' min = 0.4, |
|
| 68 |
#' max = 1 |
|
| 69 |
#' ) |
|
| 70 |
#' |
|
| 71 |
#' # timeline |
|
| 72 |
#' choropleth <- data.frame( |
|
| 73 |
#' countries = rep(choropleth$countries, 3) |
|
| 74 |
#' ) |> |
|
| 75 |
#' dplyr::mutate( |
|
| 76 |
#' grp = c( |
|
| 77 |
#' rep(2016, nrow(choropleth)), |
|
| 78 |
#' rep(2017, nrow(choropleth)), |
|
| 79 |
#' rep(2018, nrow(choropleth)) |
|
| 80 |
#' ), |
|
| 81 |
#' values = runif(27, 1, 10) |
|
| 82 |
#' ) |
|
| 83 |
#' |
|
| 84 |
#' choropleth |> |
|
| 85 |
#' group_by(grp) |> |
|
| 86 |
#' e_charts(countries, timeline = TRUE) |> |
|
| 87 |
#' e_map(values) |> |
|
| 88 |
#' e_visual_map(min = 1, max = 10) |
|
| 89 |
#' |
|
| 90 |
#' choropleth |> |
|
| 91 |
#' group_by(grp) |> |
|
| 92 |
#' e_charts(countries, timeline = TRUE) |> |
|
| 93 |
#' e_map_3d(values) |> |
|
| 94 |
#' e_visual_map(min = 1, max = 10) |
|
| 95 |
#' } |
|
| 96 |
#' |
|
| 97 |
#' @seealso \code{\link{e_country_names}},
|
|
| 98 |
#' \href{https://echarts.apache.org/en/option.html#series-map}{Additional map arguments},
|
|
| 99 |
#' \href{https://echarts.apache.org/en/option-gl.html#series-map3D}{Additional map 3D arguments}
|
|
| 100 |
#' |
|
| 101 |
#' @rdname map |
|
| 102 |
#' @export |
|
| 103 |
e_map <- function(e, serie, map = "world", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 104 | 7x |
if (missing(e)) {
|
| 105 | 1x |
stop("must pass e", call. = FALSE)
|
| 106 |
} |
|
| 107 | ||
| 108 | 6x |
if (!missing(serie)) {
|
| 109 | 6x |
sr <- deparse(substitute(serie)) |
| 110 |
} else {
|
|
| 111 | ! |
sr <- NULL |
| 112 |
} |
|
| 113 | ||
| 114 | 6x |
e_map_(e, sr, map, name, rm_x, rm_y, ...) |
| 115 |
} |
|
| 116 | ||
| 117 |
#' @rdname map |
|
| 118 |
#' @export |
|
| 119 |
e_map_ <- function(e, serie = NULL, map = "world", name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 120 | 9x |
if (missing(e)) {
|
| 121 | 1x |
stop("must pass e", call. = FALSE)
|
| 122 |
} |
|
| 123 | ||
| 124 | 8x |
e <- .rm_axis(e, rm_x, "x") |
| 125 | 8x |
e <- .rm_axis(e, rm_y, "y") |
| 126 | ||
| 127 | 8x |
for (i in seq_along(e$x$data)) {
|
| 128 | 12x |
app <- list( |
| 129 | 12x |
type = "map", |
| 130 | 12x |
map = map, |
| 131 | 12x |
name = name, |
| 132 |
... |
|
| 133 |
) |
|
| 134 | ||
| 135 | 12x |
app_data <- list() |
| 136 | 12x |
if (!is.null(serie)) {
|
| 137 | 12x |
data <- .build_data2(e$x$data[[i]], serie) |
| 138 | 12x |
data <- .add_bind2(e, data, e$x$mapping$x, i = i) |
| 139 | 12x |
app_data <- list(data = data) |
| 140 |
} |
|
| 141 | ||
| 142 | 12x |
if (!e$x$tl) {
|
| 143 | 7x |
if (is.null(name) && !is.null(serie)) {
|
| 144 | 7x |
app$name <- serie |
| 145 |
} |
|
| 146 | ||
| 147 | 7x |
app <- append(app, app_data) |
| 148 | ||
| 149 | 7x |
e$x$opts$series <- append(e$x$opts$series, list(app)) |
| 150 |
} else {
|
|
| 151 | 5x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(app_data)) |
| 152 |
} |
|
| 153 |
} |
|
| 154 | ||
| 155 | 8x |
if(length(e$x$data) == 0){
|
| 156 | ! |
app <- list( |
| 157 | ! |
type = "map", |
| 158 | ! |
map = map, |
| 159 | ! |
name = name, |
| 160 |
... |
|
| 161 |
) |
|
| 162 | ! |
e$x$opts$series <- append(e$x$opts$series, list(app)) |
| 163 |
} |
|
| 164 | ||
| 165 | 8x |
if (isTRUE(e$x$tl)) {
|
| 166 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(app)) |
| 167 |
} |
|
| 168 | ||
| 169 | 8x |
if (map == "world") {
|
| 170 |
# add dependency |
|
| 171 | 7x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 172 | 7x |
dep <- htmltools::htmlDependency( |
| 173 | 7x |
name = "echarts-world", |
| 174 | 7x |
version = "1.0.0", |
| 175 | 7x |
src = c(file = path), |
| 176 | 7x |
script = "world.js" |
| 177 |
) |
|
| 178 | ||
| 179 | 7x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 180 |
} |
|
| 181 | ||
| 182 | 8x |
e |
| 183 |
} |
|
| 184 | ||
| 185 |
#' @rdname map |
|
| 186 |
#' @export |
|
| 187 |
e_svg <- e_map |
|
| 188 | ||
| 189 |
#' @rdname map |
|
| 190 |
#' @export |
|
| 191 |
e_svg_ <- e_map_ |
|
| 192 | ||
| 193 |
#' @rdname map |
|
| 194 |
#' @export |
|
| 195 |
e_map_3d <- function(e, serie, map = "world", name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 196 | 2x |
if (missing(e)) {
|
| 197 | 1x |
stop("must pass e", call. = FALSE)
|
| 198 |
} |
|
| 199 | ||
| 200 | 1x |
if (!missing(serie)) {
|
| 201 | 1x |
sr <- deparse(substitute(serie)) |
| 202 |
} else {
|
|
| 203 | ! |
sr <- NULL |
| 204 |
} |
|
| 205 | ||
| 206 | 1x |
e_map_3d_(e = e, serie = sr, map, name, coord_system, rm_x, rm_y, ...) |
| 207 |
} |
|
| 208 | ||
| 209 |
#' @rdname map |
|
| 210 |
#' @export |
|
| 211 |
e_map_3d_ <- function(e, serie = NULL, map = "world", name = NULL, coord_system = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 212 | 4x |
if (missing(e)) {
|
| 213 | 1x |
stop("must pass e", call. = FALSE)
|
| 214 |
} |
|
| 215 | ||
| 216 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 217 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 218 | ||
| 219 | 3x |
for (i in seq_along(e$x$data)) {
|
| 220 | 7x |
if (!is.null(serie)) {
|
| 221 | 7x |
data <- .build_data2(e$x$data[[i]], serie) |
| 222 | 7x |
data <- .add_bind2(e, data, e$x$mapping$x, i = i) |
| 223 | 7x |
dat <- data |
| 224 |
} |
|
| 225 | ||
| 226 | 7x |
app <- list( |
| 227 | 7x |
type = "map3D", |
| 228 | 7x |
map = map, |
| 229 | 7x |
coordinateSystem = coord_system, |
| 230 | 7x |
name = name, |
| 231 |
... |
|
| 232 |
) |
|
| 233 | ||
| 234 | 7x |
app_data <- list(data = data) |
| 235 | ||
| 236 | 7x |
if (!e$x$tl) {
|
| 237 | 2x |
if (is.null(name) && !is.null(serie)) {
|
| 238 | 2x |
app$name <- serie |
| 239 |
} |
|
| 240 | ||
| 241 | 2x |
app <- append(app, app_data) |
| 242 | ||
| 243 | 2x |
e$x$opts$series <- append(e$x$opts$series, list(app)) |
| 244 |
} else {
|
|
| 245 | 5x |
e$x$opts$options[[i]]$series <- append(e$x$opts$options[[i]]$series, list(app_data)) |
| 246 |
} |
|
| 247 |
} |
|
| 248 | ||
| 249 | 3x |
if (isTRUE(e$x$tl)) {
|
| 250 | 1x |
e$x$opts$baseOption$series <- append(e$x$opts$baseOption$series, list(app)) |
| 251 |
} |
|
| 252 | ||
| 253 | 3x |
if (map == "world") {
|
| 254 |
# add dependency |
|
| 255 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 256 | 3x |
dep <- htmltools::htmlDependency( |
| 257 | 3x |
name = "echarts-world", |
| 258 | 3x |
version = "1.0.0", |
| 259 | 3x |
src = c(file = path), |
| 260 | 3x |
script = "world.js" |
| 261 |
) |
|
| 262 | ||
| 263 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 264 |
} |
|
| 265 | ||
| 266 |
# add dependency |
|
| 267 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 268 | 3x |
dep <- htmltools::htmlDependency( |
| 269 | 3x |
name = "echarts-gl", |
| 270 | 3x |
version = "1.1.2", |
| 271 | 3x |
src = c(file = path), |
| 272 | 3x |
script = "echarts-gl.min.js" |
| 273 |
) |
|
| 274 | ||
| 275 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 276 | ||
| 277 | 3x |
e |
| 278 |
} |
|
| 279 | ||
| 280 |
#' @rdname map |
|
| 281 |
#' @export |
|
| 282 |
e_map_3d_custom <- function(e, id, value, height, map = NULL, name = NULL, rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 283 | 8x |
if (missing(e)) {
|
| 284 | 1x |
stop("must pass e", call. = FALSE)
|
| 285 |
} |
|
| 286 | ||
| 287 | 7x |
if (missing(id) || missing(value) || missing(height)) {
|
| 288 | 1x |
stop("must pass id, value, and height", call. = FALSE)
|
| 289 |
} |
|
| 290 | ||
| 291 | 6x |
if (is.null(map) && length(e$x$registerMap[[1]]$mapName)) {
|
| 292 | 5x |
map <- unlist(e$x$registerMap[[1]]$mapName) |
| 293 | 1x |
} else if (is.null(map)) { # fix when map value is not NULL
|
| 294 | 1x |
stop("not map registered, see e_map_register", call. = FALSE)
|
| 295 |
} |
|
| 296 | ||
| 297 | 5x |
e$x$mainOpts$renderer <- "webgl" # 'canvas' works also, maybe even more efficient |
| 298 | ||
| 299 | 5x |
e <- .rm_axis(e, rm_x, "x") |
| 300 | 5x |
e <- .rm_axis(e, rm_y, "y") |
| 301 | ||
| 302 | 5x |
app <- list( |
| 303 | 5x |
type = "map3D", |
| 304 | 5x |
map = map, |
| 305 |
... |
|
| 306 |
) |
|
| 307 | ||
| 308 | 5x |
if (!is.null(name)) {
|
| 309 | 2x |
app$name <- name |
| 310 |
} |
|
| 311 | ||
| 312 | 5x |
name_quo <- dplyr::enquo(id) |
| 313 | 5x |
value_quo <- dplyr::enquo(value) |
| 314 | 5x |
height_quo <- dplyr::enquo(height) |
| 315 | ||
| 316 | 5x |
data <- e$x$data[[1]] |> |
| 317 | 5x |
dplyr::select( |
| 318 | 5x |
c(name = !!name_quo, |
| 319 | 5x |
value = !!value_quo, |
| 320 | 5x |
height = !!height_quo) |
| 321 |
) |> |
|
| 322 | 5x |
apply(1, as.list) |
| 323 | ||
| 324 | 5x |
app$data <- data |
| 325 | ||
| 326 | 5x |
e$x$opts$series <- append(e$x$opts$series, list(app)) |
| 327 | ||
| 328 |
# add dependency |
|
| 329 | 5x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 330 | 5x |
dep <- htmltools::htmlDependency( |
| 331 | 5x |
name = "echarts-gl", |
| 332 | 5x |
version = "1.1.2", |
| 333 | 5x |
src = c(file = path), |
| 334 | 5x |
script = "echarts-gl.min.js" |
| 335 |
) |
|
| 336 | 5x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 337 | ||
| 338 | 5x |
e |
| 339 |
} |
|
| 340 | ||
| 341 |
#' Register map |
|
| 342 |
#' |
|
| 343 |
#' Register a \href{https://geojson.org/}{geojson} map.
|
|
| 344 |
#' |
|
| 345 |
#' @param e An \code{echarts4r} object as returned by \code{\link{e_charts}}.
|
|
| 346 |
#' @param name Name of map, to used in \code{\link{e_map}}.
|
|
| 347 |
#' @param json,svg \href{https://geojson.org/}{Geojson}, or SVG.
|
|
| 348 |
#' @param async Whether to read the file asynchronously. |
|
| 349 |
#' @param session A valid Shiny session. |
|
| 350 |
#' @param ... Additional options passed to \href{https://echarts.apache.org/en/api.html#echarts.registerMap}{registerMap}.
|
|
| 351 |
#' |
|
| 352 |
#' @details \code{e_map_register_p} is not truly a proxy as it does not require
|
|
| 353 |
#' a chart to function. While the function \code{e_map_register_ui} is meant to
|
|
| 354 |
#' register the map globally in the Shiny UI, not that then \code{json} must be accessible
|
|
| 355 |
#' from the UI (generally www folder). |
|
| 356 |
#' |
|
| 357 |
#' @examples |
|
| 358 |
#' \dontrun{
|
|
| 359 |
#' json <- jsonlite::read_json("https://echarts.apache.org/examples/data/asset/geo/USA.json")
|
|
| 360 |
#' |
|
| 361 |
#' USArrests |> |
|
| 362 |
#' tibble::rownames_to_column("states") |>
|
|
| 363 |
#' e_charts(states) |> |
|
| 364 |
#' e_map_register("USA", json) |>
|
|
| 365 |
#' e_map(Murder, map = "USA") |> |
|
| 366 |
#' e_visual_map(Murder) |
|
| 367 |
#' } |
|
| 368 |
#' |
|
| 369 |
#' @rdname e_map_register |
|
| 370 |
#' @export |
|
| 371 | 6x |
e_map_register <- function(e, name, json, ...) UseMethod("e_map_register")
|
| 372 | ||
| 373 |
#' @export |
|
| 374 |
#' @method e_map_register echarts4r |
|
| 375 |
e_map_register.echarts4r <- function(e, name, json, ...) {
|
|
| 376 | 6x |
if (!length(e$x$registerMap)) {
|
| 377 | 6x |
e$x$registerMap <- list() |
| 378 |
} |
|
| 379 | ||
| 380 | 6x |
opts <- list( |
| 381 | 6x |
mapName = name, |
| 382 | 6x |
geoJSON = json |
| 383 |
) |
|
| 384 | ||
| 385 | 6x |
extra <- list(...) |
| 386 | 6x |
if(length(extra) > 0) |
| 387 | ! |
opts$extra <- extra |
| 388 | ||
| 389 | 6x |
e$x$registerMap <- append(e$x$registerMap, list(opts)) |
| 390 | 6x |
e |
| 391 |
} |
|
| 392 | ||
| 393 |
#' @rdname e_map_register |
|
| 394 |
#' @export |
|
| 395 | 4x |
e_svg_register <- function(e, name, svg) UseMethod("e_svg_register")
|
| 396 | ||
| 397 |
#' @export |
|
| 398 |
#' @method e_svg_register echarts4r |
|
| 399 |
e_svg_register.echarts4r <- function(e, name, svg) {
|
|
| 400 | 4x |
if (!length(e$x$registerMap)) {
|
| 401 | 3x |
e$x$registerMap <- list() |
| 402 |
} |
|
| 403 | ||
| 404 | 4x |
opts <- list( |
| 405 | 4x |
mapName = name, |
| 406 | 4x |
geoJSON = list(svg = svg) |
| 407 |
) |
|
| 408 | ||
| 409 | 4x |
e$x$registerMap <- append(e$x$registerMap, list(opts)) |
| 410 | 4x |
e |
| 411 |
} |
|
| 412 | ||
| 413 |
#' @rdname e_map_register |
|
| 414 |
#' @export |
|
| 415 |
e_map_register_p <- function(name, json, async = FALSE, session = shiny::getDefaultReactiveDomain()) {
|
|
| 416 | 2x |
opts <- list( |
| 417 | 2x |
mapAsync = async, |
| 418 | 2x |
mapName = name, |
| 419 | 2x |
geoJSON = json |
| 420 |
) |
|
| 421 | ||
| 422 | 2x |
session$sendCustomMessage("e_register_map", opts)
|
| 423 | 2x |
invisible() |
| 424 |
} |
|
| 425 | ||
| 426 |
#' @rdname e_map_register |
|
| 427 |
#' @export |
|
| 428 |
e_map_register_ui <- function(name, json, async = FALSE) {
|
|
| 429 | 1x |
async <- paste0(tolower(async)) |
| 430 | 1x |
script <- paste0("
|
| 431 | 1x |
$.ajax({
|
| 432 | 1x |
url: '", json, "', |
| 433 | 1x |
dataType: 'json', |
| 434 | 1x |
async: ", async, ", |
| 435 | 1x |
success: function(map){
|
| 436 | 1x |
echarts.registerMap('", name, "', map);
|
| 437 |
} |
|
| 438 |
});") |
|
| 439 | 1x |
shiny::tags$script( |
| 440 | 1x |
script[1] |
| 441 |
) |
|
| 442 |
} |
|
| 443 | ||
| 444 |
#' Mapbox |
|
| 445 |
#' |
|
| 446 |
#' Use mapbox. |
|
| 447 |
#' |
|
| 448 |
#' @inheritParams e_bar |
|
| 449 |
#' @param token Your mapbox token from \href{https://www.mapbox.com/}{mapbox}.
|
|
| 450 |
#' @param ... Any option. |
|
| 451 |
#' |
|
| 452 |
#' @examples |
|
| 453 |
#' \dontrun{
|
|
| 454 |
#' url <- paste0( |
|
| 455 |
#' "https://echarts.apache.org/examples/", |
|
| 456 |
#' "data-gl/asset/data/population.json" |
|
| 457 |
#' ) |
|
| 458 |
#' data <- jsonlite::fromJSON(url) |
|
| 459 |
#' data <- as.data.frame(data) |
|
| 460 |
#' names(data) <- c("lon", "lat", "value")
|
|
| 461 |
#' |
|
| 462 |
#' data |> |
|
| 463 |
#' e_charts(lon) |> |
|
| 464 |
#' e_mapbox( |
|
| 465 |
#' token = "YOUR_MAPBOX_TOKEN", |
|
| 466 |
#' style = "mapbox://styles/mapbox/dark-v9" |
|
| 467 |
#' ) |> |
|
| 468 |
#' e_bar_3d(lat, value, coord_system = "mapbox") |> |
|
| 469 |
#' e_visual_map() |
|
| 470 |
#' } |
|
| 471 |
#' |
|
| 472 |
#' @note Mapbox may not work properly in the RSudio console. |
|
| 473 |
#' |
|
| 474 |
#' @seealso \href{https://echarts.apache.org/en/option-gl.html#mapbox3D.style}{Official documentation},
|
|
| 475 |
#' \href{https://docs.mapbox.com/mapbox-gl-js/api/}{mapbox documentation}
|
|
| 476 |
#' |
|
| 477 |
#' @name mapbox |
|
| 478 |
#' @export |
|
| 479 |
e_mapbox <- function(e, token, ...) {
|
|
| 480 | 2x |
if (missing(token)) {
|
| 481 | 1x |
stop("missing token", call. = FALSE)
|
| 482 |
} |
|
| 483 | ||
| 484 | 1x |
e$x$mapboxToken <- token |
| 485 | ||
| 486 | 1x |
if (!e$x$tl) {
|
| 487 | 1x |
e$x$opts$mapbox <- list(...) |
| 488 |
} else {
|
|
| 489 | ! |
e$x$opts$baseOption$mapbox <- list(...) |
| 490 |
} |
|
| 491 | ||
| 492 |
# add dependency |
|
| 493 | 1x |
path <- system.file("htmlwidgets/lib/mapbox", package = "echarts4r")
|
| 494 | 1x |
dep <- htmltools::htmlDependency( |
| 495 | 1x |
name = "echarts-mapbox", |
| 496 | 1x |
version = "3.17.0", |
| 497 | 1x |
src = c(file = path), |
| 498 | 1x |
script = "mapbox-gl.js", |
| 499 | 1x |
stylesheet = "mapbox-gl.css" |
| 500 |
) |
|
| 501 | ||
| 502 | 1x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 503 | ||
| 504 | 1x |
e |
| 505 |
} |
| 1 |
#' General options |
|
| 2 |
#' |
|
| 3 |
#' @param font_family Font family. |
|
| 4 |
#' @param theme A theme. |
|
| 5 |
#' |
|
| 6 |
#' @export |
|
| 7 |
e_common <- function(font_family = NULL, theme = NULL) {
|
|
| 8 | 4x |
options( |
| 9 | 4x |
"ECHARTS4R_THEME" = theme, |
| 10 | 4x |
"ECHARTS4R_FONT_FAMILY" = font_family |
| 11 |
) |
|
| 12 |
} |
| 1 |
#' Color |
|
| 2 |
#' |
|
| 3 |
#' Customise chart and background colors. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param color Vector of colors. |
|
| 7 |
#' @param background Background color. |
|
| 8 |
#' @param append Only applicable to `echarts4rProxy`. |
|
| 9 |
#' Whether to append the `color` to the existing |
|
| 10 |
#' array (vector) or colors or to replace it. |
|
| 11 |
#' |
|
| 12 |
#' @examples |
|
| 13 |
#' mtcars |> |
|
| 14 |
#' e_charts(drat) |> |
|
| 15 |
#' e_line(mpg) |> |
|
| 16 |
#' e_area(qsec) |> |
|
| 17 |
#' e_color( |
|
| 18 |
#' c("red", "blue"),
|
|
| 19 |
#' "#d3d3d3" |
|
| 20 |
#' ) |
|
| 21 |
#' @seealso \code{\link{e_theme}},
|
|
| 22 |
#' \href{https://echarts.apache.org/en/option.html#color}{Official color documentation},
|
|
| 23 |
#' \href{https://echarts.apache.org/en/option.html#backgroundColor}{Official background documentation}
|
|
| 24 |
#' |
|
| 25 |
#' @name e_color |
|
| 26 |
#' @export |
|
| 27 | 6x |
e_color <- function(e, color = NULL, background = NULL, append = TRUE) UseMethod("e_color")
|
| 28 | ||
| 29 |
#' @rdname e_color |
|
| 30 |
#' @export |
|
| 31 |
e_color.echarts4r <- function(e, color = NULL, background = NULL, append = TRUE) {
|
|
| 32 | 5x |
if (!e$x$tl) {
|
| 33 | 2x |
if (!is.null(color)) e$x$opts$color <- as.list(color) |
| 34 | 2x |
if (!is.null(background)) e$x$opts$backgroundColor <- background |
| 35 |
} else {
|
|
| 36 | 1x |
if (!is.null(color)) e$x$opts$baseOption$color <- color |
| 37 | 1x |
if (!is.null(background)) e$x$opts$baseOption$backgroundColor <- background |
| 38 |
} |
|
| 39 | 5x |
e |
| 40 |
} |
|
| 41 | ||
| 42 |
#' @rdname e_color |
|
| 43 |
#' @export |
|
| 44 |
e_color.echarts4rProxy <- function(e, color = NULL, background = NULL, append = TRUE) {
|
|
| 45 | 1x |
if (!is.null(color)) e$chart$x$opts$color <- as.list(color) |
| 46 | 1x |
if (!is.null(background)) e$chart$x$opts$backgroundColor <- background |
| 47 | 1x |
e$chart$x$opts$appendColor <- append |
| 48 | ||
| 49 | 1x |
e |
| 50 |
} |
| 1 |
#' Axis 3D |
|
| 2 |
#' |
|
| 3 |
#' Customise 3D axis. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param axis Axis to customise. |
|
| 7 |
#' @param index Index of axis to customise. |
|
| 8 |
#' |
|
| 9 |
#' @examples |
|
| 10 |
#' # phony data |
|
| 11 |
#' v <- LETTERS[1:10] |
|
| 12 |
#' matrix <- data.frame( |
|
| 13 |
#' x = sample(v, 300, replace = TRUE), |
|
| 14 |
#' y = sample(v, 300, replace = TRUE), |
|
| 15 |
#' z1 = rnorm(300, 10, 1), |
|
| 16 |
#' z2 = rnorm(300, 10, 1), |
|
| 17 |
#' stringsAsFactors = FALSE |
|
| 18 |
#' ) |> |
|
| 19 |
#' dplyr::group_by(x, y) |> |
|
| 20 |
#' dplyr::summarise( |
|
| 21 |
#' z1 = sum(z1), |
|
| 22 |
#' z2 = sum(z2) |
|
| 23 |
#' ) |> |
|
| 24 |
#' dplyr::ungroup() |
|
| 25 |
#' |
|
| 26 |
#' trans <- list(opacity = 0.4) # transparency |
|
| 27 |
#' emphasis <- list(itemStyle = list(color = "#313695")) |
|
| 28 |
#' |
|
| 29 |
#' matrix |> |
|
| 30 |
#' e_charts(x) |> |
|
| 31 |
#' e_bar_3d(y, z1, stack = "stack", name = "Serie 1", itemStyle = trans, emphasis = emphasis) |> |
|
| 32 |
#' e_bar_3d(y, z2, stack = "stack", name = "Serie 2", itemStyle = trans, emphasis = emphasis) |> |
|
| 33 |
#' e_x_axis_3d(axisLine = list(lineStyle = list(color = "blue"))) |
|
| 34 |
#' @seealso \href{https://echarts.apache.org/en/option-gl.html#xAxis3D}{Additional x arguments},
|
|
| 35 |
#' \href{https://echarts.apache.org/en/option-gl.html#yAxis3D}{Additional y arguments},
|
|
| 36 |
#' \href{https://echarts.apache.org/en/option-gl.html#zAxis3D}{Additional z arguments}
|
|
| 37 |
#' |
|
| 38 |
#' @rdname axis3d |
|
| 39 |
#' @export |
|
| 40 |
e_axis_3d <- function(e, axis = c("x", "y", "z"), index = 0, ...) {
|
|
| 41 | 6x |
if (missing(e)) {
|
| 42 | 1x |
stop("missing e", call. = FALSE)
|
| 43 |
} |
|
| 44 | ||
| 45 | 5x |
axis <- .r2axis3D(axis[1]) |
| 46 | ||
| 47 | 5x |
r.index <- index + 1 |
| 48 | ||
| 49 | 5x |
x_data <- if(e$x$tl){
|
| 50 | 1x |
e$x$opts$baseOption[[axis]] |
| 51 |
} else {
|
|
| 52 | 4x |
e$x$opts[[axis]]} |
| 53 | ||
| 54 | 5x |
max <- length(x_data) |
| 55 | ||
| 56 | 5x |
if (r.index > max) {
|
| 57 | 1x |
stop("invalid axis", call. = FALSE)
|
| 58 |
} |
|
| 59 | ||
| 60 | 4x |
attrs <- list(...) |
| 61 | ||
| 62 | 4x |
if (!length(attrs)) {
|
| 63 | 1x |
stop("no attribute", call. = FALSE)
|
| 64 |
} |
|
| 65 | ||
| 66 | 3x |
for (i in seq_along(attrs)) {
|
| 67 | 3x |
arg <- names(attrs)[i] |
| 68 | ||
| 69 | 3x |
if (!e$x$tl) {
|
| 70 | 2x |
e$x$opts[[axis]][[r.index]][[arg]] <- attrs[[i]] |
| 71 |
} else {
|
|
| 72 | 1x |
e$x$opts$baseOption[[axis]][[r.index]][[arg]] <- attrs[[i]] |
| 73 |
} |
|
| 74 |
} |
|
| 75 | ||
| 76 | 3x |
e |
| 77 |
} |
|
| 78 | ||
| 79 |
#' @rdname axis3d |
|
| 80 |
#' @export |
|
| 81 |
e_x_axis_3d <- function(e, index = 0, ...) {
|
|
| 82 | 2x |
if (missing(e)) {
|
| 83 | 1x |
stop("missing e", call. = FALSE)
|
| 84 |
} |
|
| 85 | 1x |
e <- e_axis_3d(e, axis = "x", index = index, ...) |
| 86 | 1x |
e |
| 87 |
} |
|
| 88 | ||
| 89 |
#' @rdname axis3d |
|
| 90 |
#' @export |
|
| 91 |
e_y_axis_3d <- function(e, index = 0, ...) {
|
|
| 92 | 1x |
if (missing(e)) {
|
| 93 | 1x |
stop("missing e", call. = FALSE)
|
| 94 |
} |
|
| 95 | ! |
e <- e_axis_3d(e = e, axis = "y", index = index, ...) |
| 96 | ! |
e |
| 97 |
} |
|
| 98 | ||
| 99 |
#' @rdname axis3d |
|
| 100 |
#' @export |
|
| 101 |
e_z_axis_3d <- function(e, index = 0, ...) {
|
|
| 102 | 1x |
if (missing(e)) {
|
| 103 | 1x |
stop("missing e", call. = FALSE)
|
| 104 |
} |
|
| 105 | ! |
e <- e_axis_3d(e = e, axis = "z", index = index, ...) |
| 106 | ! |
e |
| 107 |
} |
|
| 108 | ||
| 109 |
#' Grid |
|
| 110 |
#' |
|
| 111 |
#' Customise grid. |
|
| 112 |
#' |
|
| 113 |
#' @inheritParams e_bar |
|
| 114 |
#' @inheritParams e_axis |
|
| 115 |
#' |
|
| 116 |
#' @examples |
|
| 117 |
#' # phony data |
|
| 118 |
#' v <- LETTERS[1:10] |
|
| 119 |
#' matrix <- data.frame( |
|
| 120 |
#' x = sample(v, 300, replace = TRUE), |
|
| 121 |
#' y = sample(v, 300, replace = TRUE), |
|
| 122 |
#' z1 = rnorm(300, 10, 1), |
|
| 123 |
#' z2 = rnorm(300, 10, 1), |
|
| 124 |
#' stringsAsFactors = FALSE |
|
| 125 |
#' ) |> |
|
| 126 |
#' dplyr::group_by(x, y) |> |
|
| 127 |
#' dplyr::summarise( |
|
| 128 |
#' z1 = sum(z1), |
|
| 129 |
#' z2 = sum(z2) |
|
| 130 |
#' ) |> |
|
| 131 |
#' dplyr::ungroup() |
|
| 132 |
#' |
|
| 133 |
#' trans <- list(opacity = 0.4) # transparency |
|
| 134 |
#' emphasis <- list(itemStyle = list(color = "#313695")) |
|
| 135 |
#' |
|
| 136 |
#' matrix |> |
|
| 137 |
#' e_charts(x) |> |
|
| 138 |
#' e_bar_3d(y, z1, stack = "stack", name = "Serie 1", itemStyle = trans, emphasis = emphasis) |> |
|
| 139 |
#' e_bar_3d(y, z2, stack = "stack", name = "Serie 2", itemStyle = trans, emphasis = emphasis) |> |
|
| 140 |
#' e_grid_3d(splitLine = list(lineStyle = list(color = "blue"))) |
|
| 141 |
#' @seealso \href{https://echarts.apache.org/en/option-gl.html#grid3D}{Additional arguments}
|
|
| 142 |
#' |
|
| 143 |
#' @export |
|
| 144 |
e_grid_3d <- function(e, index = 0, ...) {
|
|
| 145 | 6x |
if (missing(e)) {
|
| 146 | 1x |
stop("missing e", call. = FALSE)
|
| 147 |
} |
|
| 148 | ||
| 149 | 5x |
r.index <- index + 1 |
| 150 | ||
| 151 |
# initialise if not existing |
|
| 152 | 5x |
if (!e$x$tl) {
|
| 153 | 4x |
if (!length(e$x$opts[["grid3D"]])) {
|
| 154 | 1x |
e$x$opts$grid3D <- list(NULL) |
| 155 |
} |
|
| 156 |
} else {
|
|
| 157 | 1x |
if (!length(e$x$opts$baseOption[["grid3D"]])) {
|
| 158 | ! |
e$x$opts$baseOption$grid3D <- list(NULL) |
| 159 |
} |
|
| 160 |
} |
|
| 161 | ||
| 162 | 5x |
attrs <- list(...) |
| 163 | ||
| 164 | 5x |
if (!length(attrs)) {
|
| 165 | 1x |
stop("no attribute", call. = FALSE)
|
| 166 |
} |
|
| 167 | ||
| 168 | 4x |
for (i in seq_along(attrs)) {
|
| 169 | 4x |
arg <- names(attrs)[i] |
| 170 | ||
| 171 | 4x |
if (!e$x$tl) {
|
| 172 | 3x |
e$x$opts$grid3D[[r.index]][[arg]] <- attrs[[i]] |
| 173 |
} else {
|
|
| 174 | 1x |
e$x$opts$baseOption$grid3D[[r.index]][[arg]] <- attrs[[i]] |
| 175 |
} |
|
| 176 |
} |
|
| 177 | ||
| 178 | 4x |
e |
| 179 |
} |
| 1 |
#' Geo 3D |
|
| 2 |
#' |
|
| 3 |
#' Initialise geo 3D. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param color Color. |
|
| 7 |
#' @param type Map type. |
|
| 8 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 9 |
#' |
|
| 10 |
#' @examples |
|
| 11 |
#' choropleth <- data.frame( |
|
| 12 |
#' countries = c( |
|
| 13 |
#' "France", |
|
| 14 |
#' "Brazil", |
|
| 15 |
#' "China", |
|
| 16 |
#' "Russia", |
|
| 17 |
#' "Canada", |
|
| 18 |
#' "India", |
|
| 19 |
#' "United States", |
|
| 20 |
#' "Argentina", |
|
| 21 |
#' "Australia" |
|
| 22 |
#' ), |
|
| 23 |
#' height = runif(9, 1, 5), |
|
| 24 |
#' color = c( |
|
| 25 |
#' "#F7FBFF", |
|
| 26 |
#' "#DEEBF7", |
|
| 27 |
#' "#C6DBEF", |
|
| 28 |
#' "#9ECAE1", |
|
| 29 |
#' "#6BAED6", |
|
| 30 |
#' "#4292C6", |
|
| 31 |
#' "#2171B5", |
|
| 32 |
#' "#08519C", |
|
| 33 |
#' "#08306B" |
|
| 34 |
#' ) |
|
| 35 |
#' ) |
|
| 36 |
#' |
|
| 37 |
#' choropleth |> |
|
| 38 |
#' e_charts(countries) |> |
|
| 39 |
#' e_geo_3d(height, color) |
|
| 40 |
#' @seealso \code{\link{e_country_names}},
|
|
| 41 |
#' \href{https://echarts.apache.org/en/option-gl.html#geo3D}{Additional arguments}
|
|
| 42 |
#' |
|
| 43 |
#' @rdname e_geo_3d |
|
| 44 |
#' @export |
|
| 45 |
e_geo_3d <- function(e, serie, color, type = "world", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 46 | 3x |
if (missing(e)) {
|
| 47 | 1x |
stop("must pass e", call. = FALSE)
|
| 48 |
} |
|
| 49 | ||
| 50 | 2x |
if (!missing(serie)) {
|
| 51 | 1x |
sr <- deparse(substitute(serie)) |
| 52 |
} else {
|
|
| 53 | 1x |
sr <- NULL |
| 54 |
} |
|
| 55 | ||
| 56 | 2x |
if (!missing(color)) {
|
| 57 | 1x |
cl <- deparse(substitute(color)) |
| 58 |
} else {
|
|
| 59 | 1x |
cl <- NULL |
| 60 |
} |
|
| 61 | ||
| 62 | 2x |
e_geo_3d_(e, sr, cl, type, rm_x, rm_y, ...) |
| 63 |
} |
|
| 64 | ||
| 65 |
#' @rdname e_geo_3d |
|
| 66 |
#' @export |
|
| 67 |
e_geo_3d_ <- function(e, serie = NULL, color = NULL, type = "world", rm_x = TRUE, rm_y = TRUE, ...) {
|
|
| 68 | 4x |
if (missing(e)) {
|
| 69 | 1x |
stop("must pass e", call. = FALSE)
|
| 70 |
} |
|
| 71 | ||
| 72 | 3x |
e <- .rm_axis(e, rm_x, "x") |
| 73 | 3x |
e <- .rm_axis(e, rm_y, "y") |
| 74 | ||
| 75 | 3x |
series <- list( |
| 76 | 3x |
map = type, |
| 77 |
... |
|
| 78 |
) |
|
| 79 | ||
| 80 | 3x |
if (!is.null(color) && !is.null(serie)) {
|
| 81 | 1x |
series$regions <- .build_height(e, serie, color, j = 1) |
| 82 | 2x |
} else if (is.null(color) && !is.null(serie)) {
|
| 83 | ! |
series$regions <- .build_height(e, serie, j = 1) |
| 84 |
} |
|
| 85 | ||
| 86 | 3x |
if (!e$x$tl) {
|
| 87 | 3x |
e$x$opts$geo3D <- series |
| 88 |
} else {
|
|
| 89 | ! |
e$x$opts$baseOption$geo3D <- series |
| 90 |
} |
|
| 91 | ||
| 92 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 93 | 3x |
dep <- htmltools::htmlDependency( |
| 94 | 3x |
name = "echarts-gl", |
| 95 | 3x |
version = "1.1.2", |
| 96 | 3x |
src = c(file = path), |
| 97 | 3x |
script = "echarts-gl.min.js" |
| 98 |
) |
|
| 99 | ||
| 100 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 101 | ||
| 102 | 3x |
if (type == "world") {
|
| 103 |
# add dependency |
|
| 104 | 3x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 105 | 3x |
dep <- htmltools::htmlDependency( |
| 106 | 3x |
name = "echarts-world", |
| 107 | 3x |
version = "1.0.0", |
| 108 | 3x |
src = c(file = path), |
| 109 | 3x |
script = "world.js" |
| 110 |
) |
|
| 111 | ||
| 112 | 3x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 113 |
} |
|
| 114 | ||
| 115 | 3x |
e |
| 116 |
} |
|
| 117 | ||
| 118 |
#' Geo |
|
| 119 |
#' |
|
| 120 |
#' Initialise geo. |
|
| 121 |
#' |
|
| 122 |
#' @inheritParams e_bar |
|
| 123 |
#' @param map Map type. |
|
| 124 |
#' @param rm_x,rm_y Whether to remove x and y axis, defaults to \code{TRUE}.
|
|
| 125 |
#' |
|
| 126 |
#' @examples |
|
| 127 |
#' flights |> |
|
| 128 |
#' e_charts() |> |
|
| 129 |
#' e_geo() |> |
|
| 130 |
#' e_lines( |
|
| 131 |
#' start_lon, |
|
| 132 |
#' start_lat, |
|
| 133 |
#' end_lon, |
|
| 134 |
#' end_lat, |
|
| 135 |
#' name = "flights", |
|
| 136 |
#' lineStyle = list(normal = list(curveness = 0.3)) |
|
| 137 |
#' ) |
|
| 138 |
#' @seealso \href{https://echarts.apache.org/en/option.html#geo}{Additional arguments}
|
|
| 139 |
#' |
|
| 140 |
#' @export |
|
| 141 |
e_geo <- function(e, map = "world", ..., rm_x = TRUE, rm_y = TRUE) {
|
|
| 142 | 5x |
opts <- list( |
| 143 | 5x |
map = map, |
| 144 |
... |
|
| 145 |
) |
|
| 146 | ||
| 147 | 5x |
e <- .rm_axis(e, rm_x, "x") |
| 148 | 5x |
e <- .rm_axis(e, rm_y, "y") |
| 149 | ||
| 150 | 5x |
if (!e$x$tl) {
|
| 151 | 4x |
e$x$opts$geo <- opts |
| 152 |
} else {
|
|
| 153 | 1x |
e$x$opts$baseOption$geo <- opts |
| 154 |
} |
|
| 155 | ||
| 156 | 5x |
if (map == "world") {
|
| 157 |
# add dependency |
|
| 158 | 5x |
path <- system.file("htmlwidgets/lib/echarts-6.1.0", package = "echarts4r")
|
| 159 | 5x |
dep <- htmltools::htmlDependency( |
| 160 | 5x |
name = "echarts-world", |
| 161 | 5x |
version = "1.0.0", |
| 162 | 5x |
src = c(file = path), |
| 163 | 5x |
script = "world.js" |
| 164 |
) |
|
| 165 | ||
| 166 | 5x |
e$dependencies <- append(e$dependencies, list(dep)) |
| 167 |
} |
|
| 168 | ||
| 169 | 5x |
e |
| 170 |
} |
| 1 |
#' Morphing |
|
| 2 |
#' |
|
| 3 |
#' __This is experimental__ |
|
| 4 |
#' |
|
| 5 |
#' Morph between graphs. |
|
| 6 |
#' |
|
| 7 |
#' @param e,... Graphs (from `e_graph`). |
|
| 8 |
#' @param callback JavaScript callback function as a |
|
| 9 |
#' character string (vector of length 1). |
|
| 10 |
#' This function has access to the `chart` object, as well |
|
| 11 |
#' as `opts` an array containing the options of the charts |
|
| 12 |
#' passed to `e` and `...`. |
|
| 13 |
#' @param default Default chart to show. |
|
| 14 |
#' |
|
| 15 |
#' @examples |
|
| 16 |
#' mtcars2 <- mtcars |> |
|
| 17 |
#' head() |> |
|
| 18 |
#' tibble::rownames_to_column("model")
|
|
| 19 |
#' |
|
| 20 |
#' e1 <- mtcars2 |> |
|
| 21 |
#' e_charts(model) |> |
|
| 22 |
#' e_bar( |
|
| 23 |
#' carb, |
|
| 24 |
#' universalTransition = TRUE, |
|
| 25 |
#' animationDurationUpdate = 1000L |
|
| 26 |
#' ) |
|
| 27 |
#' |
|
| 28 |
#' e2 <- mtcars2 |> |
|
| 29 |
#' e_charts(model) |> |
|
| 30 |
#' e_pie( |
|
| 31 |
#' carb, |
|
| 32 |
#' universalTransition = TRUE, |
|
| 33 |
#' animationDurationUpdate = 1000L |
|
| 34 |
#' ) |
|
| 35 |
#' |
|
| 36 |
#' cb <- "() => {
|
|
| 37 |
#' let x = 0; |
|
| 38 |
#' setInterval(() => {
|
|
| 39 |
#' x++ |
|
| 40 |
#' chart.setOption(opts[x % 2], true); |
|
| 41 |
#' }, 3000); |
|
| 42 |
#' }" |
|
| 43 |
#' |
|
| 44 |
#' e_morph(e1, e2, callback = cb) |
|
| 45 |
#' |
|
| 46 |
#' @export |
|
| 47 |
e_morph <- \(e, ..., callback, default = 1L) {
|
|
| 48 | 4x |
warning("This is experimental")
|
| 49 | ||
| 50 | 4x |
if(...length() < 1) |
| 51 | 1x |
stop("Must pass at least two charts")
|
| 52 | ||
| 53 | 3x |
if(missing(callback)) |
| 54 | 1x |
stop("Missing callback")
|
| 55 | ||
| 56 | 2x |
opts <- lapply(list(e, ...), \(p) {
|
| 57 | 4x |
p$x$opts |
| 58 |
}) |> |
|
| 59 | 2x |
unname() |
| 60 | ||
| 61 | 2x |
e$x$opts <- list(opts) |
| 62 | 2x |
e$x$morphed <- list( |
| 63 | 2x |
callback = callback, |
| 64 | 2x |
default = default - 1L |
| 65 |
) |
|
| 66 | ||
| 67 | 2x |
return(e) |
| 68 |
} |
| 1 |
#' Calendar |
|
| 2 |
#' |
|
| 3 |
#' @inheritParams e_bar |
|
| 4 |
#' @param range Range of calendar format, string or vector. |
|
| 5 |
#' |
|
| 6 |
#' @examples |
|
| 7 |
#' dates <- seq.Date(as.Date("2017-01-01"), as.Date("2019-12-31"), by = "day")
|
|
| 8 |
#' values <- rnorm(length(dates), 20, 6) |
|
| 9 |
#' year <- data.frame(date = dates, values = values) |
|
| 10 |
#' |
|
| 11 |
#' year |> |
|
| 12 |
#' e_charts(date) |> |
|
| 13 |
#' e_calendar(range = "2017") |> |
|
| 14 |
#' e_visual_map(max = 30) |> |
|
| 15 |
#' e_heatmap(values, coord_system = "calendar") |
|
| 16 |
#' |
|
| 17 |
#' # month |
|
| 18 |
#' year |> |
|
| 19 |
#' e_charts(date) |> |
|
| 20 |
#' e_calendar(range = "2017-01") |> |
|
| 21 |
#' e_visual_map(max = 30) |> |
|
| 22 |
#' e_heatmap(values, coord_system = "calendar") |
|
| 23 |
#' |
|
| 24 |
#' # range |
|
| 25 |
#' year |> |
|
| 26 |
#' e_charts(date) |> |
|
| 27 |
#' e_calendar(range = c("2018-01", "2018-07")) |>
|
|
| 28 |
#' e_visual_map(max = 30) |> |
|
| 29 |
#' e_heatmap(values, coord_system = "calendar") |
|
| 30 |
#' |
|
| 31 |
#' @seealso \href{https://echarts.apache.org/en/option.html#calendar}{Additional arguments}
|
|
| 32 |
#' |
|
| 33 |
#' @export |
|
| 34 |
e_calendar <- function(e, range, ...) {
|
|
| 35 | 4x |
if (missing(e) || missing(range)) {
|
| 36 | 2x |
stop("missing e or range", call. = FALSE)
|
| 37 |
} |
|
| 38 | ||
| 39 |
# initialise |
|
| 40 | 2x |
if (!length(e$x$opts$calendar)) {
|
| 41 | 2x |
e$x$opts$calendar <- list() |
| 42 |
} |
|
| 43 | ||
| 44 | 2x |
cal <- list(range = range, ...) |
| 45 | ||
| 46 | 2x |
if (!e$x$tl) {
|
| 47 | 1x |
e$x$opts$calendar <- append(e$x$opts$calendar, list(cal)) |
| 48 |
} else {
|
|
| 49 | 1x |
e$x$opts$baseOption$calendar <- append(e$x$opts$baseOption$calendar, list(cal)) |
| 50 |
} |
|
| 51 | ||
| 52 | 2x |
e |
| 53 |
} |
| 1 |
#' Callbacks |
|
| 2 |
#' |
|
| 3 |
#' Binds events to chart interactions. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param query Condition that triggers the handler |
|
| 7 |
#' @param handler Javascript handler, passed to \link[htmlwidgets]{JS}.
|
|
| 8 |
#' @param event Event that triggers the handler. |
|
| 9 |
#' |
|
| 10 |
#' @examples |
|
| 11 |
#' cars |> |
|
| 12 |
#' e_charts(speed) |> |
|
| 13 |
#' e_scatter(dist) |> |
|
| 14 |
#' e_on( |
|
| 15 |
#' list(seriesName = "dist"), |
|
| 16 |
#' "function(){alert('Serie clicked')}"
|
|
| 17 |
#' ) |
|
| 18 |
#' @seealso |
|
| 19 |
#' \href{https://echarts.apache.org/en/api.html#echartsInstance.on}{official
|
|
| 20 |
#' documentation} |
|
| 21 |
#' |
|
| 22 |
#' @name callbacks |
|
| 23 |
#' @export |
|
| 24 |
e_on <- function(e, query, handler, event = "click") {
|
|
| 25 | 5x |
if (missing(query) || missing(handler)) {
|
| 26 | 2x |
stop("must pass query and handler", call. = FALSE)
|
| 27 |
} |
|
| 28 | ||
| 29 | 3x |
if (!length(e$x$on)) {
|
| 30 | 3x |
e$x$on <- list() |
| 31 |
} |
|
| 32 | ||
| 33 | 3x |
opts <- list( |
| 34 | 3x |
event = event, |
| 35 | 3x |
query = query, |
| 36 | 3x |
handler = htmlwidgets::JS(handler) |
| 37 |
) |
|
| 38 | ||
| 39 | 3x |
e$x$on <- append(e$x$on, list(opts)) |
| 40 | ||
| 41 | 3x |
e |
| 42 |
} |
|
| 43 | ||
| 44 |
#' @rdname callbacks |
|
| 45 |
#' @export |
|
| 46 |
e_off <- function(e, query, handler, event = "click") {
|
|
| 47 | 5x |
if (missing(query) || missing(handler)) {
|
| 48 | 2x |
stop("must pass query and handler", call. = FALSE)
|
| 49 |
} |
|
| 50 | ||
| 51 | 3x |
if (!length(e$x$off)) {
|
| 52 | 3x |
e$x$off <- list() |
| 53 |
} |
|
| 54 | ||
| 55 | 3x |
opts <- list( |
| 56 | 3x |
event = event, |
| 57 | 3x |
query = query, |
| 58 | 3x |
handler = htmlwidgets::JS(handler) |
| 59 |
) |
|
| 60 | ||
| 61 | 3x |
e$x$off <- append(e$x$off, list(opts)) |
| 62 | ||
| 63 | 3x |
e |
| 64 |
} |
|
| 65 | ||
| 66 | ||
| 67 |
#' Blank Area |
|
| 68 |
#' |
|
| 69 |
#' Use this function to capture a click on a |
|
| 70 |
#' blank area of the canvas. |
|
| 71 |
#' Note that this may stops other "click" events |
|
| 72 |
#' from working. |
|
| 73 |
#' |
|
| 74 |
#' @inheritParams e_bar |
|
| 75 |
#' @export |
|
| 76 |
e_get_zr <- function(e){
|
|
| 77 | 1x |
e$x$zr <- TRUE |
| 78 | 1x |
e |
| 79 |
} |
| 1 |
#' Globe |
|
| 2 |
#' |
|
| 3 |
#' Add globe. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param environment Texture of background. |
|
| 7 |
#' @param base_texture Base texture of globe. |
|
| 8 |
#' @param height_texture Texture of height. |
|
| 9 |
#' |
|
| 10 |
#' @examples |
|
| 11 |
#' \dontrun{
|
|
| 12 |
#' url <- paste0( |
|
| 13 |
#' "https://echarts.apache.org/examples/", |
|
| 14 |
#' "data-gl/asset/data/population.json" |
|
| 15 |
#' ) |
|
| 16 |
#' data <- jsonlite::fromJSON(url) |
|
| 17 |
#' data <- as.data.frame(data) |
|
| 18 |
#' names(data) <- c("lon", "lat", "value")
|
|
| 19 |
#' |
|
| 20 |
#' data |> |
|
| 21 |
#' e_charts(lon) |> |
|
| 22 |
#' e_globe( |
|
| 23 |
#' displacementScale = 0.04 |
|
| 24 |
#' ) |> |
|
| 25 |
#' e_bar_3d(lat, value, "globe") |> |
|
| 26 |
#' e_visual_map(show = FALSE) |
|
| 27 |
#' } |
|
| 28 |
#' |
|
| 29 |
#' @seealso \code{\link{e_country_names}}, \href{https://echarts.apache.org/en/option-gl.html#globe}{Additional arguments}
|
|
| 30 |
#' |
|
| 31 |
#' @export |
|
| 32 |
e_globe <- function(e, environment = NULL, base_texture = NULL, height_texture = NULL, ...) {
|
|
| 33 | 5x |
if (missing(e)) {
|
| 34 | 1x |
stop("must pass e", call. = FALSE)
|
| 35 |
} |
|
| 36 | ||
| 37 | 4x |
if (is.null(base_texture)) {
|
| 38 | 4x |
message("`base_texture` is `NULL`, see echarts4r-assets.john-coene.com")
|
| 39 |
} |
|
| 40 | ||
| 41 | 4x |
e$x$mainOpts$renderer <- "webgl" |
| 42 | ||
| 43 | 4x |
ls <- list( |
| 44 | 4x |
environment = environment, |
| 45 | 4x |
heightTexture = height_texture, |
| 46 | 4x |
baseTexture = base_texture, |
| 47 | 4x |
series = list(), |
| 48 |
... |
|
| 49 |
) |
|
| 50 | ||
| 51 | 4x |
if (!e$x$tl) {
|
| 52 | 3x |
e$x$opts$globe <- ls |
| 53 |
} else {
|
|
| 54 | 1x |
e$x$opts$baseOption$globe <- ls |
| 55 |
} |
|
| 56 | ||
| 57 | 4x |
e |
| 58 |
} |
| 1 |
#' Loading |
|
| 2 |
#' |
|
| 3 |
#' Show or hide loading. |
|
| 4 |
#' |
|
| 5 |
#' @inheritParams e_bar |
|
| 6 |
#' @param hide_overlay Hides the white overlay that appears in shiny when a plot is recalculating. |
|
| 7 |
#' @param text Text to display. |
|
| 8 |
#' @param color Color of spinner. |
|
| 9 |
#' @param text_color Color of text. |
|
| 10 |
#' @param mask_color Color of mask. |
|
| 11 |
#' @param zlevel Z level. |
|
| 12 |
#' |
|
| 13 |
#' @details This only applies to Shiny. |
|
| 14 |
#' |
|
| 15 |
#' @examples |
|
| 16 |
#' \dontrun{
|
|
| 17 |
#' |
|
| 18 |
#' # no redraw |
|
| 19 |
#' # no loading |
|
| 20 |
#' library(shiny) |
|
| 21 |
#' ui <- fluidPage( |
|
| 22 |
#' fluidRow( |
|
| 23 |
#' column(12, actionButton("update", "Update"))
|
|
| 24 |
#' ), |
|
| 25 |
#' fluidRow( |
|
| 26 |
#' column(12, echarts4rOutput("plot"))
|
|
| 27 |
#' ) |
|
| 28 |
#' ) |
|
| 29 |
#' |
|
| 30 |
#' server <- function(input, output) {
|
|
| 31 |
#' data <- eventReactive(input$update, {
|
|
| 32 |
#' data.frame( |
|
| 33 |
#' x = 1:10, |
|
| 34 |
#' y = rnorm(10) |
|
| 35 |
#' ) |
|
| 36 |
#' }) |
|
| 37 |
#' |
|
| 38 |
#' output$plot <- renderEcharts4r({
|
|
| 39 |
#' data() |> |
|
| 40 |
#' e_charts(x) |> |
|
| 41 |
#' e_bar(y) |
|
| 42 |
#' }) |
|
| 43 |
#' } |
|
| 44 |
#' |
|
| 45 |
#' if (interactive()) {
|
|
| 46 |
#' shinyApp(ui, server) |
|
| 47 |
#' } |
|
| 48 |
#' |
|
| 49 |
#' # add loading |
|
| 50 |
#' server <- function(input, output) {
|
|
| 51 |
#' data <- eventReactive(input$update, {
|
|
| 52 |
#' Sys.sleep(1) # sleep one second to show loading |
|
| 53 |
#' data.frame( |
|
| 54 |
#' x = 1:10, |
|
| 55 |
#' y = rnorm(10) |
|
| 56 |
#' ) |
|
| 57 |
#' }) |
|
| 58 |
#' |
|
| 59 |
#' output$plot <- renderEcharts4r({
|
|
| 60 |
#' data() |> |
|
| 61 |
#' e_charts(x) |> |
|
| 62 |
#' e_bar(y) |> |
|
| 63 |
#' e_show_loading() |
|
| 64 |
#' }) |
|
| 65 |
#' } |
|
| 66 |
#' |
|
| 67 |
#' if (interactive()) {
|
|
| 68 |
#' shinyApp(ui, server) |
|
| 69 |
#' } |
|
| 70 |
#' } |
|
| 71 |
#' |
|
| 72 |
#' @rdname loading |
|
| 73 |
#' @export |
|
| 74 |
e_show_loading <- function( |
|
| 75 |
e, |
|
| 76 |
hide_overlay = TRUE, |
|
| 77 |
text = "loading", |
|
| 78 |
color = "#c23531", |
|
| 79 |
text_color = "#000", |
|
| 80 |
mask_color = "rgba(255, 255, 255, 0.8)", |
|
| 81 |
zlevel = 0 |
|
| 82 |
) {
|
|
| 83 | 1x |
e$x$loading <- TRUE |
| 84 | 1x |
e$x$hideWhite <- hide_overlay |
| 85 | 1x |
opts <- list( |
| 86 | 1x |
text = text, |
| 87 | 1x |
color = color, |
| 88 | 1x |
textColor = text_color, |
| 89 | 1x |
maskColor = mask_color, |
| 90 | 1x |
zlevel = zlevel |
| 91 |
) |
|
| 92 | 1x |
e$x$loadingOpts <- opts |
| 93 | 1x |
e |
| 94 |
} |
|
| 95 | ||
| 96 |
#' @rdname loading |
|
| 97 |
#' @export |
|
| 98 |
e_hide_loading <- function(e) {
|
|
| 99 | 1x |
e$x$loading <- FALSE |
| 100 | 1x |
e |
| 101 |
} |