From 90e0a96d95dc524b3b0ed979962dff81a5fbbbd2 Mon Sep 17 00:00:00 2001 From: Benoit Thieurmel Date: Wed, 29 Sep 2021 15:11:21 +0200 Subject: [PATCH] mapLayout : fix bug if one area and/or no link --- R/map_layout.R | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/R/map_layout.R b/R/map_layout.R index 972b862..efaa499 100644 --- a/R/map_layout.R +++ b/R/map_layout.R @@ -242,10 +242,12 @@ changeCoordsServer <- function(input, output, session, coords <- copy(layout()$areas) info <- coords$area links <- copy(layout()$links) + if(is.null(links)) links <- data.table() } else { coords <- copy(layout()$districts) info <- coords$district links <- copy(layout()$districtLinks) + if(is.null(links)) links <- data.table() } links$x0 <- as.numeric(links$x0) @@ -294,7 +296,7 @@ changeCoordsServer <- function(input, output, session, cex[pt] <- 2 par (mar = rep(0.1, 4)) graphics::plot.default(points$oldLon, points$oldLat, bty = "n", xaxt = "n", yaxt = "n", - xlab = "", ylab = "", main = "", col = col, asp = 1, pch = 19, cex = cex) + xlab = "", ylab = "", main = "", col = col, asp = 1, pch = 19, cex = cex) } }) } @@ -386,6 +388,10 @@ changeCoordsServer <- function(input, output, session, coords <- sp::SpatialPoints(coords()[, c("lon", "lat")], proj4string = sp::CRS("+proj=longlat +datum=WGS84")) + # special with only one area... + if(nrow(data()$coords) == 1){ + coords <- coords[1, ] + } map <- current_map() @@ -396,8 +402,12 @@ changeCoordsServer <- function(input, output, session, } # Put coords in right order - ord <- order(c(data_points$pt1, data_points$pt2, (1:length(coords))[-c(data_points$pt1, data_points$pt2)])) - mapCoords <- coords[ord, ] + if(nrow(data()$coords) > 1){ + ord <- order(c(data_points$pt1, data_points$pt2, (1:length(coords))[-c(data_points$pt1, data_points$pt2)])) + mapCoords <- coords[ord, ] + } else { + mapCoords <- coords + } final_coords <- data()$coords final_links <- data()$links @@ -406,11 +416,15 @@ changeCoordsServer <- function(input, output, session, final_coords$y <- sp::coordinates(mapCoords)[, 2] if (what() == "areas") { - final_links[final_coords, `:=`(x0 = x, y0 = y), on = c(from = "area")] - final_links[final_coords, `:=`(x1 = x, y1 = y), on = c(to = "area")] + if(!is.null(final_links) && nrow(final_links) > 0){ + final_links[final_coords, `:=`(x0 = x, y0 = y), on = c(from = "area")] + final_links[final_coords, `:=`(x1 = x, y1 = y), on = c(to = "area")] + } } else { - final_links[final_coords, `:=`(x0 = x, y0 = y), on = c(fromDistrict = "district")] - final_links[final_coords, `:=`(x1 = x, y1 = y), on = c(toDistrict = "district")] + if(!is.null(final_links) && nrow(final_links) > 0){ + final_links[final_coords, `:=`(x0 = x, y0 = y), on = c(fromDistrict = "district")] + final_links[final_coords, `:=`(x1 = x, y1 = y), on = c(toDistrict = "district")] + } } if (!is.null(map)) {