Skip to content

Support nativeRaster objects in annotation_raster() and raster2uri() #2174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* `ggplotly()` now supports the `{ggalluvial}` package. (#2061, thanks @moutikabdessabour)
* `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)
* `raster2uri()` supports nativeRaster objects. This enables nativeRaster support for
the `annotation_raster()` geom (#2174, @zeehio).

## Bug fixes

Expand Down
23 changes: 14 additions & 9 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,19 @@ plotly_empty <- function(...) {
raster2uri <- function(r, ...) {
try_library("png", "raster2uri")
# should be 4 x n matrix
r <- grDevices::as.raster(r, ...)
rgbs <- col2rgb(c(r), alpha = T) / 255
nr <- dim(r)[1]
nc <- dim(r)[2]
reds <- matrix(rgbs[1, ], nrow = nr, ncol = nc, byrow = TRUE)
greens <- matrix(rgbs[2, ], nrow = nr, ncol = nc, byrow = TRUE)
blues <- matrix(rgbs[3, ], nrow = nr, ncol = nc, byrow = TRUE)
alphas <- matrix(rgbs[4, ], nrow = nr, ncol = nc, byrow = TRUE)
png <- array(c(reds, greens, blues, alphas), dim = c(dim(r), 4))
if (inherits(r, "nativeRaster")) {
# png::writePNG directly supports nativeRaster objects
png <- r
} else {
r <- grDevices::as.raster(r, ...)
rgbs <- col2rgb(c(r), alpha = T) / 255
nr <- dim(r)[1]
nc <- dim(r)[2]
reds <- matrix(rgbs[1, ], nrow = nr, ncol = nc, byrow = TRUE)
greens <- matrix(rgbs[2, ], nrow = nr, ncol = nc, byrow = TRUE)
blues <- matrix(rgbs[3, ], nrow = nr, ncol = nc, byrow = TRUE)
alphas <- matrix(rgbs[4, ], nrow = nr, ncol = nc, byrow = TRUE)
png <- array(c(reds, greens, blues, alphas), dim = c(dim(r), 4))
}
base64enc::dataURI(png::writePNG(png), mime = "image/png")
}
15 changes: 15 additions & 0 deletions tests/testthat/test-plotly-subplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,21 @@ test_that("shape paper repositioning", {
expect_equal(y1, c(30, 0.75))
})

test_that("raster2uri supports nativeRaster objects", {
skip_if_not_installed("png")

r <- as.raster(matrix(c("black", "red", "green", "blue"), ncol = 4L))
nr <- structure(
c(-16777216L, -16776961L, -16711936L, -65536L),
dim = c(1L, 4L),
class = "nativeRaster",
channels = 4L
)
uri_r <- raster2uri(r)
uri_nr <- raster2uri(nr)
expect_equal(uri_r, uri_nr)
})


test_that("image paper repositioning", {
skip_if_not_installed("png")
Expand Down