|
| 1 | +test_that("y error arrays follow points when color splits a trace", { |
| 2 | + data <- data.frame( |
| 3 | + x = 1:6, |
| 4 | + y = 11:16, |
| 5 | + group = rep(c("B", "A"), 3), |
| 6 | + upper = 21:26, |
| 7 | + lower = 31:36 |
| 8 | + ) |
| 9 | + p <- plot_ly( |
| 10 | + data, x = ~x, y = ~y, color = ~group, type = "scatter", mode = "markers", |
| 11 | + error_y = list(array = ~upper, arrayminus = ~lower, symmetric = FALSE) |
| 12 | + ) |
| 13 | + traces <- plotly_build(p)$x$data |
| 14 | + |
| 15 | + expect_length(traces, 2) |
| 16 | + for (trace in traces) { |
| 17 | + rows <- match(trace$x, data$x) |
| 18 | + expect_equal(as.vector(trace$error_y$array), data$upper[rows]) |
| 19 | + expect_equal(as.vector(trace$error_y$arrayminus), data$lower[rows]) |
| 20 | + expect_false(trace$error_y$symmetric) |
| 21 | + } |
| 22 | +}) |
| 23 | + |
| 24 | +test_that("y error arrays stay aligned in a seven-row color split", { |
| 25 | + data <- data.frame(x = 1:7, y = 11:17, group = rep(c("A", "B"), length.out = 7)) |
| 26 | + p <- plot_ly( |
| 27 | + data, x = ~x, y = ~y, color = ~group, type = "scatter", mode = "markers", |
| 28 | + error_y = list(array = 21:27) |
| 29 | + ) |
| 30 | + traces <- plotly_build(p)$x$data |
| 31 | + |
| 32 | + expect_length(traces, 2) |
| 33 | + for (trace in traces) { |
| 34 | + expect_equal(as.vector(trace$error_y$array), (21:27)[trace$x]) |
| 35 | + } |
| 36 | +}) |
| 37 | + |
| 38 | +test_that("y error arrays follow a line through a gap in a color split", { |
| 39 | + data <- data.frame( |
| 40 | + x = 1:8, y = c(11, 12, NA, 14:18), |
| 41 | + group = rep(c("A", "B"), c(5, 3)), upper = 21:28 |
| 42 | + ) |
| 43 | + p <- plot_ly( |
| 44 | + data, x = ~x, y = ~y, color = ~group, type = "scatter", mode = "lines+markers", |
| 45 | + error_y = list(array = ~upper) |
| 46 | + ) |
| 47 | + traces <- plotly_build(p)$x$data |
| 48 | + |
| 49 | + expect_length(traces, 2) |
| 50 | + expect_equal(as.vector(traces[[1]]$x), c(1, 2, NA, 4, 5)) |
| 51 | + for (trace in traces) { |
| 52 | + expect_equal(as.vector(trace$error_y$array), data$upper[trace$x]) |
| 53 | + } |
| 54 | +}) |
| 55 | + |
| 56 | +test_that("x error arrays follow points when symbol splits a line trace", { |
| 57 | + data <- data.frame( |
| 58 | + x = c(5, 6, 3, 4, 1, 2), |
| 59 | + y = c(11, 12, NA, 14, 15, 16), |
| 60 | + group = rep(c("B", "A"), 3), |
| 61 | + upper = 21:26, |
| 62 | + lower = 31:36 |
| 63 | + ) |
| 64 | + p <- plot_ly(data, x = ~x, y = ~y, symbol = ~group) %>% |
| 65 | + add_lines(error_x = ~list( |
| 66 | + array = upper, arrayminus = as.list(lower), symmetric = FALSE |
| 67 | + )) |
| 68 | + traces <- plotly_build(p)$x$data |
| 69 | + |
| 70 | + expect_length(traces, 2) |
| 71 | + for (trace in traces) { |
| 72 | + rows <- match(trace$x, data$x) |
| 73 | + expect_equal(as.vector(trace$error_x$array), data$upper[rows]) |
| 74 | + expect_equivalent(trace$error_x$arrayminus, as.list(data$lower)[rows]) |
| 75 | + } |
| 76 | +}) |
| 77 | + |
| 78 | +test_that("short error arrays preserve missing values when groups are sorted", { |
| 79 | + data <- data.frame(x = c(3, 2, 1, 4), y = 11:14, group = c("A", "B", "A", "B")) |
| 80 | + p <- plot_ly(data, x = ~x, y = ~y, color = ~group) %>% |
| 81 | + add_lines(error_y = list(array = c(21, 22), arrayminus = list(31, 32))) |
| 82 | + traces <- jsonlite::parse_json(plotly_json(p, jsonedit = FALSE))$data |
| 83 | + |
| 84 | + expect_length(traces, 2) |
| 85 | + for (trace in traces) { |
| 86 | + rows <- match(unlist(trace$x), data$x) |
| 87 | + expect_equal(trace$error_y$array, list(21, 22, "NaN", "NaN")[rows]) |
| 88 | + expect_equal(trace$error_y$arrayminus, list(31, 32, "NaN", "NaN")[rows]) |
| 89 | + } |
| 90 | +}) |
| 91 | + |
| 92 | +test_that("short named error vectors serialize as arrays in each color group", { |
| 93 | + data <- data.frame(x = 1:4, y = 11:14, group = c("A", "B", "A", "B")) |
| 94 | + p <- plot_ly( |
| 95 | + data, x = ~x, y = ~y, color = ~group, type = "scatter", mode = "markers", |
| 96 | + error_y = list( |
| 97 | + array = c(first = 21, second = 22), |
| 98 | + arrayminus = c(first = 31, second = 32), symmetric = FALSE |
| 99 | + ) |
| 100 | + ) |
| 101 | + traces <- jsonlite::parse_json(plotly_json(p, jsonedit = FALSE))$data |
| 102 | + |
| 103 | + expect_length(traces, 2) |
| 104 | + for (trace in traces) { |
| 105 | + rows <- unlist(trace$x) |
| 106 | + expect_equal(trace$error_y$array, list(21, 22, "NaN", "NaN")[rows]) |
| 107 | + expect_equal(trace$error_y$arrayminus, list(31, 32, "NaN", "NaN")[rows]) |
| 108 | + } |
| 109 | +}) |
| 110 | + |
| 111 | +test_that("style can add points with pre-supplied upper and lower errors", { |
| 112 | + p <- plot_ly( |
| 113 | + x = 1:2, y = 11:12, type = "scatter", mode = "markers", |
| 114 | + error_y = list(array = 21:24, arrayminus = 31:34, symmetric = FALSE) |
| 115 | + ) |
| 116 | + p <- style(p, x = 1:4, y = 11:14) |
| 117 | + trace <- jsonlite::parse_json(plotly_json(p, jsonedit = FALSE))$data[[1]] |
| 118 | + |
| 119 | + expect_equal(unlist(trace$x), 1:4) |
| 120 | + expect_equal(unlist(trace$y), 11:14) |
| 121 | + expect_equal(unlist(trace$error_y$array), 21:24) |
| 122 | + expect_equal(unlist(trace$error_y$arrayminus), 31:34) |
| 123 | +}) |
| 124 | + |
| 125 | +test_that("building a trace preserves JavaScript error array expressions", { |
| 126 | + errors <- htmlwidgets::JS("[2, 4, 6]") |
| 127 | + p <- plot_ly(x = 1:3, y = 11:13, type = "scatter", mode = "markers", |
| 128 | + error_y = list(array = errors)) |
| 129 | + |
| 130 | + expect_s3_class(plotly_build(p)$x$data[[1]]$error_y$array, "JS_EVAL") |
| 131 | +}) |
| 132 | + |
| 133 | +test_that("segment error arrays refer to the output vertices", { |
| 134 | + p <- add_segments(plot_ly(), x = 1, y = 2, xend = 3, yend = 4, |
| 135 | + error_y = list(array = 5)) |
| 136 | + trace <- plotly_build(p)$x$data[[1]] |
| 137 | + |
| 138 | + expect_equal(as.vector(trace$x), c(1, 3)) |
| 139 | + expect_equal(as.vector(trace$error_y$array), 5) |
| 140 | +}) |
| 141 | + |
| 142 | +test_that("ribbon error arrays refer to the output vertices", { |
| 143 | + p <- add_ribbons(plot_ly(), x = c(2, 1), ymin = c(3, 2), ymax = c(5, 4), |
| 144 | + error_y = list(array = c(6, 7))) |
| 145 | + trace <- plotly_build(p)$x$data[[1]] |
| 146 | + |
| 147 | + expect_equal(as.vector(trace$x), c(1, 2, 2, 2, 1)) |
| 148 | + expect_equal(as.vector(trace$error_y$array), c(6, 7)) |
| 149 | +}) |
| 150 | + |
| 151 | +test_that("histograms retain bin errors when there are fewer samples than bins", { |
| 152 | + upper <- c(0.1, 0.2, 0.3) |
| 153 | + lower <- c(0.4, 0.5, 0.6) |
| 154 | + p <- add_histogram( |
| 155 | + plot_ly(), x = c(0.1, 2.1), xbins = list(start = 0, end = 3, size = 1), |
| 156 | + error_y = list(array = upper, arrayminus = lower, symmetric = FALSE) |
| 157 | + ) |
| 158 | + trace <- plotly_build(p)$x$data[[1]] |
| 159 | + |
| 160 | + expect_equal(as.vector(trace$error_y$array), upper) |
| 161 | + expect_equal(as.vector(trace$error_y$arrayminus), lower) |
| 162 | +}) |
| 163 | + |
| 164 | +test_that("color groups retain horizontal histogram bin errors", { |
| 165 | + data <- data.frame( |
| 166 | + y = c(0.1, 0.2, 1.1, 1.2, 2.1, 2.2, 2.3, 2.4), |
| 167 | + group = rep(c("A", "B"), 4) |
| 168 | + ) |
| 169 | + upper <- c(0.1, 0.2, 0.3) |
| 170 | + lower <- c(0.4, 0.5, 0.6) |
| 171 | + p <- plot_ly( |
| 172 | + data, y = ~y, color = ~group, type = "histogram", |
| 173 | + ybins = list(start = 0, end = 3, size = 1), |
| 174 | + error_x = list(array = upper, arrayminus = lower, symmetric = FALSE) |
| 175 | + ) |
| 176 | + traces <- plotly_build(p)$x$data |
| 177 | + |
| 178 | + expect_length(traces, 2) |
| 179 | + for (trace in traces) { |
| 180 | + expect_equal(as.vector(trace$error_x$array), upper) |
| 181 | + expect_equal(as.vector(trace$error_x$arrayminus), lower) |
| 182 | + } |
| 183 | +}) |
0 commit comments