I am having some trouble with an app which reacts completely random when I launch it. The goal of this app is to display a map and zoom on a polygon. I have 4 random behaviours for this app everytime I launch it :
- I have no polygon and no fitbounds
- I have no polygon and fitbounds
- I have a polygon and no fitbounds
- I have a polygon and fitbounds (this is what I want) (launch it a dozen times to seen all of them)
On every behaviour, I checked that :
- I do have coordinates for the fitbounds
- I do have data for the polygon
I tried on my computer, on a coworker computer, with R 4.0.3, with restart sessions and clear environment etc ... And yet it behaves the weirdest I have ever seen.
Here is an example :
library(shiny)
library(leaflet)
library(sf)
data_test <- structure(list(id_commune = "18202", nom = "SAINT-CEOLS", capacite_res_max_mw = 1,
nb_postes_sans_capacites = 0, geometry = structure(list(structure(list(
list(structure(c(2.6458238, 2.6477812, 2.6484066, 2.649831,
2.6507915, 2.6511036, 2.6517993, 2.6528354, 2.6545799,
2.6549011, 2.6549502, 2.6547718, 2.6545772, 2.6535482,
2.6525085, 2.6524269, 2.6502692, 2.6499694, 2.6496993,
2.6486484, 2.6472603, 2.6468348, 2.646813, 2.6467724,
2.6462654, 2.6461965, 2.644425, 2.6426654, 2.6425145,
2.642463, 2.6424197, 2.6414236, 2.6413511, 2.6395177,
2.6394395, 2.6389405, 2.6388905, 2.6383785, 2.6382954,
2.6381761, 2.6381461, 2.638186, 2.6381608, 2.6380602,
2.638002, 2.6377445, 2.6373366, 2.6373082, 2.6372993,
2.6352306, 2.6339108, 2.6331391, 2.6323018, 2.630977,
2.6294247, 2.6277346, 2.6275991, 2.6268624, 2.6262724,
2.6249652, 2.623575, 2.623222, 2.6229762, 2.6225395,
2.6222879, 2.6222642, 2.6219018, 2.6219991, 2.6225956,
2.6227605, 2.623416, 2.6238106, 2.6241467, 2.6244811,
2.6245576, 2.6244541, 2.6244768, 2.6242877, 2.6235295,
2.6234182, 2.6228713, 2.6227321, 2.6227215, 2.622147,
2.6219206, 2.6218587, 2.6216581, 2.6215656, 2.6212786,
2.6211429, 2.6204778, 2.6202699, 2.619702, 2.6190935,
2.6189488, 2.6189859, 2.6189866, 2.6193732, 2.6193855,
2.6195909, 2.6202148, 2.6203842, 2.6218921, 2.6235095,
2.6235765, 2.6229605, 2.6229707, 2.6230166, 2.6230169,
2.623025, 2.6232132, 2.6236883, 2.623755, 2.6237803,
2.6238459, 2.6250047, 2.6255796, 2.6256568, 2.6267558,
2.6267487, 2.6272799, 2.6272588, 2.627615, 2.6276634,
2.6281639, 2.6287334, 2.6306123, 2.632018, 2.6297713,
2.6317394, 2.6320673, 2.6326171, 2.6338706, 2.6339841,
2.6340001, 2.6398288, 2.6406091, 2.6413647, 2.6417256,
2.6418551, 2.6424519, 2.6426639, 2.6428104, 2.6428913,
2.6431882, 2.6437142, 2.6440162, 2.6444019, 2.6451135,
2.6451626, 2.6451878, 2.6451594, 2.6449946, 2.6449467,
2.6449184, 2.6448659, 2.6448473, 2.6448003, 2.6447739,
2.6448092, 2.6448726, 2.6458238, 47.2030789, 47.2041242,
47.2044617, 47.2052741, 47.2058465, 47.2059629, 47.2063177,
47.2068196, 47.2076232, 47.2077748, 47.207798, 47.2078729,
47.2079734, 47.2085919, 47.2092205, 47.2092697, 47.2105759,
47.2107543, 47.2109151, 47.2115405, 47.2123698, 47.2126351,
47.2126488, 47.212674, 47.2136032, 47.2137273, 47.2169194,
47.2200753, 47.2203518, 47.2204247, 47.2204989, 47.2223111,
47.2224431, 47.2257657, 47.225919, 47.2268299, 47.2269208,
47.2278555, 47.2280104, 47.2282327, 47.2282886, 47.2283424,
47.2284058, 47.2290548, 47.2294467, 47.2311789, 47.2339225,
47.2341134, 47.234174, 47.2329974, 47.2322674, 47.2318654,
47.2314645, 47.2308682, 47.2301554, 47.2293718, 47.229313,
47.2289934, 47.2286495, 47.2277764, 47.2268012, 47.2264362,
47.2261105, 47.2254873, 47.2249668, 47.2248704, 47.223651,
47.2236223, 47.2234132, 47.2233637, 47.2231742, 47.2231036,
47.2230163, 47.2229901, 47.2229519, 47.2228438, 47.2228892,
47.2229262, 47.2230845, 47.2231212, 47.2232982, 47.2233444,
47.2233271, 47.2235039, 47.2235299, 47.2233291, 47.2226318,
47.2224426, 47.2219788, 47.2218215, 47.2212407, 47.2211097,
47.2206426, 47.2199843, 47.2197792, 47.2197648, 47.2197644,
47.2196022, 47.2195969, 47.2195089, 47.219242, 47.2194368,
47.2187239, 47.2179579, 47.2179098, 47.2176291, 47.2175602,
47.2172533, 47.2172456, 47.2170647, 47.2170833, 47.2168698,
47.2168368, 47.21689, 47.21686, 47.2163312, 47.2160703,
47.2160354, 47.2155384, 47.2155147, 47.2153232, 47.2153059,
47.2151439, 47.2151218, 47.2148937, 47.2146339, 47.2137747,
47.2131359, 47.2150888, 47.2134273, 47.213141, 47.21295,
47.2125158, 47.2124765, 47.2124709, 47.2112353, 47.2110704,
47.2109095, 47.2108326, 47.2108051, 47.2102223, 47.2100051,
47.2098274, 47.2097408, 47.2095098, 47.2089996, 47.2087066,
47.2083325, 47.2076352, 47.2076387, 47.2075394, 47.2073869,
47.2069951, 47.2067433, 47.2058733, 47.2056406, 47.2052145,
47.2050061, 47.2048261, 47.2045156, 47.2042548, 47.2030789
), .Dim = c(162L, 2L)))), class = c("XY", "MULTIPOLYGON",
"sfg"))), class = c("sfc_MULTIPOLYGON", "sfc"), precision = 0, bbox = structure(c(xmin = 2.6189488,
ymin = 47.2030789, xmax = 2.6549502, ymax = 47.234174), class = "bbox"), crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCS[\"WGS 84\",\n DATUM[\"WGS_1984\",\n SPHEROID[\"WGS 84\",6378137,298.257223563,\n AUTHORITY[\"EPSG\",\"7030\"]],\n AUTHORITY[\"EPSG\",\"6326\"]],\n PRIMEM[\"Greenwich\",0,\n AUTHORITY[\"EPSG\",\"8901\"]],\n UNIT[\"degree\",0.0174532925199433,\n AUTHORITY[\"EPSG\",\"9122\"]],\n AUTHORITY[\"EPSG\",\"4326\"]]"), class = "crs"), n_empty = 0L)), sf_column = "geometry", agr = structure(c(id_commune = NA_integer_,
nom = NA_integer_, capacite_res_max_mw = NA_integer_, nb_postes_sans_capacites = NA_integer_
), .Label = c("constant", "aggregate", "identity"), class = "factor"), row.names = 19L, class = c("sf",
"data.frame"))
ui <- fluidPage(
leafletOutput("map")
)
server <- function(input, output, session) {
rv <- reactiveValues()
observe({
rv$data_poly <- data_test
})
output$map <- renderLeaflet({
# The fitBounds is working if it is written here
com_bbox <- unname(st_bbox(data_test$geometry))
leaflet() %>%
# fitBounds(com_bbox[1], com_bbox[2], com_bbox[3], com_bbox[4]) %>% #working
addProviderTiles(provider = providers$CartoDB.Positron)
})
observeEvent(rv$data_poly, {
req(rv$data_poly)
print(rv$data_poly) # we do have data in rv$data_poly
com_bbox <- unname(st_bbox(rv$data_poly$geometry))
print(com_bbox) # we do have coordinates
# The fitBounds is not working from here
leafletProxy("map") %>%
fitBounds(com_bbox[1], com_bbox[2], com_bbox[3], com_bbox[4]) %>% # not working
addPolygons(
data = rv$data_poly,
layerId = ~id_commune,
label = ~nom
)
})
}
shinyApp(ui = ui, server = server)
EDIT : I forgot to specify I am on RStudio Server. I tried on a local RStudio with same R and packages versions, and it works all the time. Others colleagues tried on our compagny RStudio Server, and the problem is still there.