Skip to content

Commit 34541df

Browse files
committed
ensure geometry column is a simple features collection before using st_as_sf(), add test that sf works with crosstalk
1 parent ca7e1b2 commit 34541df

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

R/layers2traces.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) {
273273
#' @export
274274
to_basic.GeomSf <- function(data, prestats_data, layout, params, p, ...) {
275275

276-
data <- sf::st_as_sf(data)
276+
data[["geometry"]] <- sf::st_sfc(data[["geometry"]])
277+
data <- sf::st_as_sf(data, sf_column_name = "geometry")
277278
geom_type <- sf::st_geometry_type(data)
278279
# st_cast should "expand" a collection into multiple rows (one per feature)
279280
if ("GEOMETRYCOLLECTION" %in% geom_type) {

tests/testthat/test-animate-highlight.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,21 @@ test_that("animation button can be customized", {
368368
expect_true(menu$font$color == "white")
369369
expect_true(menu$buttons[[1]]$label == "Custom")
370370
})
371+
372+
373+
test_that("sf works with crosstalk", {
374+
skip_if_not_installed("sf")
375+
376+
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"))
377+
# shared data will make the polygons "query-able"
378+
ncsd <- SharedData$new(nc)
379+
p <- ggplot(ncsd) +
380+
geom_sf(aes(fill = AREA, text = paste0(NAME, "\n", "FIPS: ", FIPS))) +
381+
ggthemes::theme_map()
382+
gg <- ggplotly(p, tooltip = "text")
383+
d <- gg$x$data
384+
for (i in seq_along(d)) {
385+
expect_false(is.null(d[[i]]$key))
386+
expect_false(is.null(d[[i]]$set))
387+
}
388+
})

0 commit comments

Comments
 (0)