From a1b858b1dad4ba8244cb91aec9a323ba5e246344 Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Wed, 24 Aug 2022 10:20:28 +0200 Subject: [PATCH 1/6] Add failing test with native raster --- tests/testthat/test-plotly-subplot.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/testthat/test-plotly-subplot.R b/tests/testthat/test-plotly-subplot.R index e91b733cfe..bd33f65a5e 100644 --- a/tests/testthat/test-plotly-subplot.R +++ b/tests/testthat/test-plotly-subplot.R @@ -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") From 2ebf8956fd81cb1741cdeb7d0f5f15e04367a1ce Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Wed, 24 Aug 2022 10:20:41 +0200 Subject: [PATCH 2/6] Support nativeRaster objects in raster2uri --- R/helpers.R | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/R/helpers.R b/R/helpers.R index 87e1421a20..115ecf2ca4 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -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 <- nr + } 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") } From a9c90e97e1841b0a9d398c66707bc5a152179192 Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Wed, 24 Aug 2022 10:20:53 +0200 Subject: [PATCH 3/6] News entry for nativeRaster support --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 8401d34223..14472df9dc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,8 @@ ## Changes to plotly.js * This version of the R package upgrades the version of the underlying plotly.js library from v2.5.1 to v2.11.1. This includes many bug fixes and improvements. The [plotly.js release page](https://github.com/plotly/plotly.js/releases) has the full list of changes. +* raster2uri() supports nativeRaster objects. This enables nativeRaster support for + the annotation_raster() geom. ## New features From d02a6f4b256cb361ff471a8c0c1efc10fa0bac51 Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Wed, 24 Aug 2022 10:34:04 +0200 Subject: [PATCH 4/6] Update NEWS entry with pull request number --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 14472df9dc..a8d7d97eff 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,7 @@ * This version of the R package upgrades the version of the underlying plotly.js library from v2.5.1 to v2.11.1. This includes many bug fixes and improvements. The [plotly.js release page](https://github.com/plotly/plotly.js/releases) has the full list of changes. * raster2uri() supports nativeRaster objects. This enables nativeRaster support for - the annotation_raster() geom. + the annotation_raster() geom (#2174, @zeehio). ## New features From ddf77272cd680cc4078b215a7b9d4b50d22d8105 Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Wed, 24 Aug 2022 10:54:06 +0200 Subject: [PATCH 5/6] Fix typo --- R/helpers.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/helpers.R b/R/helpers.R index 115ecf2ca4..c5d97d5070 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -221,7 +221,7 @@ raster2uri <- function(r, ...) { # should be 4 x n matrix if (inherits(r, "nativeRaster")) { # png::writePNG directly supports nativeRaster objects - png <- nr + png <- r } else { r <- grDevices::as.raster(r, ...) rgbs <- col2rgb(c(r), alpha = T) / 255 From 1f10f361aff3b0e8d005881470ef9604a8560d32 Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Tue, 20 Sep 2022 04:55:16 +0200 Subject: [PATCH 6/6] Move NEWS entry to new features section --- NEWS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index a8d7d97eff..acf9d7390f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,13 +3,13 @@ ## Changes to plotly.js * This version of the R package upgrades the version of the underlying plotly.js library from v2.5.1 to v2.11.1. This includes many bug fixes and improvements. The [plotly.js release page](https://github.com/plotly/plotly.js/releases) has the full list of changes. -* raster2uri() supports nativeRaster objects. This enables nativeRaster support for - the annotation_raster() geom (#2174, @zeehio). ## New features * `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