Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 32567a0

Browse files
authored
Support nativeRaster objects in annotation_raster() and raster2uri() (#2174)
* Add failing test with native raster * Support nativeRaster objects in raster2uri * News entry for nativeRaster support * Update NEWS entry with pull request number * Fix typo * Move NEWS entry to new features section
1 parent 39a0055 commit 32567a0

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

‎NEWS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
* `ggplotly()` now supports the `{ggalluvial}` package. (#2061, thanks @moutikabdessabour)
1010
* `highlight()` now supports `on="plotly_selecting"`, enabling client-side linked brushing via mouse click+drag (no mouse-up event required, as with `on="plotly_selected"`). (#1280)
11+
* `raster2uri()` supports nativeRaster objects. This enables nativeRaster support for
12+
the `annotation_raster()` geom (#2174, @zeehio).
1113

1214
## Bug fixes
1315

‎R/helpers.R‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,19 @@ plotly_empty <- function(...) {
219219
raster2uri <- function(r, ...) {
220220
try_library("png", "raster2uri")
221221
# should be 4 x n matrix
222-
r <- grDevices::as.raster(r, ...)
223-
rgbs <- col2rgb(c(r), alpha = T) / 255
224-
nr <- dim(r)[1]
225-
nc <- dim(r)[2]
226-
reds <- matrix(rgbs[1, ], nrow = nr, ncol = nc, byrow = TRUE)
227-
greens <- matrix(rgbs[2, ], nrow = nr, ncol = nc, byrow = TRUE)
228-
blues <- matrix(rgbs[3, ], nrow = nr, ncol = nc, byrow = TRUE)
229-
alphas <- matrix(rgbs[4, ], nrow = nr, ncol = nc, byrow = TRUE)
230-
png <- array(c(reds, greens, blues, alphas), dim = c(dim(r), 4))
222+
if (inherits(r, "nativeRaster")) {
223+
# png::writePNG directly supports nativeRaster objects
224+
png <- r
225+
} else {
226+
r <- grDevices::as.raster(r, ...)
227+
rgbs <- col2rgb(c(r), alpha = T) / 255
228+
nr <- dim(r)[1]
229+
nc <- dim(r)[2]
230+
reds <- matrix(rgbs[1, ], nrow = nr, ncol = nc, byrow = TRUE)
231+
greens <- matrix(rgbs[2, ], nrow = nr, ncol = nc, byrow = TRUE)
232+
blues <- matrix(rgbs[3, ], nrow = nr, ncol = nc, byrow = TRUE)
233+
alphas <- matrix(rgbs[4, ], nrow = nr, ncol = nc, byrow = TRUE)
234+
png <- array(c(reds, greens, blues, alphas), dim = c(dim(r), 4))
235+
}
231236
base64enc::dataURI(png::writePNG(png), mime = "image/png")
232237
}

‎tests/testthat/test-plotly-subplot.R‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,21 @@ test_that("shape paper repositioning", {
274274
expect_equal(y1, c(30, 0.75))
275275
})
276276

277+
test_that("raster2uri supports nativeRaster objects", {
278+
skip_if_not_installed("png")
279+
280+
r <- as.raster(matrix(c("black", "red", "green", "blue"), ncol = 4L))
281+
nr <- structure(
282+
c(-16777216L, -16776961L, -16711936L, -65536L),
283+
dim = c(1L, 4L),
284+
class = "nativeRaster",
285+
channels = 4L
286+
)
287+
uri_r <- raster2uri(r)
288+
uri_nr <- raster2uri(nr)
289+
expect_equal(uri_r, uri_nr)
290+
})
291+
277292

278293
test_that("image paper repositioning", {
279294
skip_if_not_installed("png")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /