Drawing multiple polygons with a brochure in Shiny
I am struggling to draw multiple polygons in the Shiny app - based on the flyer package.
Below is the output without an attachment that I desire:
data <- list(
beam1 = data.frame(lat = c(-115,-125, -125, -115),
lon = c(32, 32, 45,45)),
beam2 = data.frame(lat = c(-100, -111, -111, -100),
lon = c(42, 42, 50,50))
)
dataTemp <- do.call(rbind, lapply(data, function(x) rbind(x, NA)))
library(leaflet)
m = leaflet() %>% addTiles()
m %>%
addPolygons(
dataTemp[,"lat"],
dataTemp[,"lon"],
color = c('red', 'green'), weight = 3
)
However, creating a Glitter app out of it turns out to be difficult - polygons are formed and then disappear one by one when a new polygon is overlaid. Here is the code I am using. You need to click Draw to draw the polygons. Notice how: 1 - the red polygon disappears when the green one is drawn (after 3 seconds) 2 - the polygons here are triangles, and in the above code they are rectangles. 3 - if you look at the code on the .R server below, I actually had to flip the "lat" and "lon" columns to get even the remotely correct picture.
ui.R:
library(shiny)
library(leaflet)
shinyUI(navbarPage("Beams", id="nav",
tabPanel("Interactive map",
div(class="outer",
leafletMap("map", "100%", 650,
options = list(center = c(37.45, -93.85), zoom = 4)),
actionButton("drawPoints", "Draw")
)
)
))
server.R:
library(shiny)
library(leaflet)
data <- list(
beam1 = data.frame(lat = c(-115,-125, -125, -115),
lon = c(32, 32, 45,45)),
beam2 = data.frame(lat = c(-100, -111, -111, -100),
lon = c(42, 42, 50,50))
)
# server activity
shinyServer(function(input, output, session) {
map <- createLeafletMap(session, "map")
observe({
if(input$drawPoints == 0) {
return(NULL)
} else {
map$clearShapes()
for (i in seq_along(data)) {
map$addPolygon(
data[[i]][,"lon"], # - notice, i had to change lat and lon
data[[i]][,"lat"],
layerId=c("1"),
list( fillOpacity=0.4),
list(color = c('red','green')[i])
)
Sys.sleep(3) # - this is to see first (red) polygon
}
}
})
})
Any ideas would be appreciated!
source to share
, -, leaflet
( ). "lat" "lon" data.frame, . , - , . , layerId
i
, - .
I assumed that you want the shapes to be hardcoded, but there are ways to expose the polygon plot selection (or even drawing) to the user. An interesting side note: in solving this, after learning about the parameter deferUntilFlush
in leafletProxy
- without this value in FALSE
, both shapes will only show up after the Sys.sleep
count is done.
ui.R
library(shiny)
library(leaflet)
shinyUI(navbarPage("Beams", id="nav",
tabPanel("Interactive map",
div(class="outer",
leafletOutput("map", "100%", 650),
actionButton("drawPoints", "Draw")
)
)
))
server.R
library(shiny)
library(leaflet)
data <- list(
beam1 = data.frame(lon = c(-115,-125, -125, -115, -115),
lat = c(32, 32, 45, 45, 32)),
beam2 = data.frame(lon = c(-100, -111, -111, -100, -100),
lat = c(42, 42, 50, 50, 42))
)
shinyServer(function(input, output, session) {
map <- leaflet() %>% addTiles() %>% setView(-93.85, 37.45, zoom = 4)
output$map <- renderLeaflet(map)
proxy <- leafletProxy("map", deferUntilFlush = FALSE)
observeEvent(input$drawPoints, {
proxy %>% clearShapes()
for (i in seq_along(data)) {
proxy %>% addPolygons(
data[[i]][,"lon"],
data[[i]][,"lat"],
layerId=i,
opacity=0.4,
color = c('red','green')[i]
)
Sys.sleep(2) # - this is to see first (red) polygon
}
})
})
source to share