Hi Joe,
Thank you for your reply and yes, of course, sorry if it is not the best way to share it:
ui.R
shinyUI(navbarPage("divRs", id="nav",
tabPanel("Interactive map",
div(class="outer",
tags$head(
includeCSS("styles.css")
),
leafletOutput("map", width="100%", height="100%")
)
),
absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
width = 330, height = "auto",
h2("Controls"),
textInput("user","User",value="user 1"),
htmlOutput("selected"),
checkboxInput('addMarkerOnClick', 'Add marker on click', FALSE),
checkboxInput('addPolygonOnClick', 'Add polygon on clicks', TRUE),
htmlOutput("buffer")
)
)
)
server.R
library(shiny)
library(leaflet)
library(dplyr)
#######Functions and reactiveValues######
server <- reactiveValues(items=list(),
points=list(),
polygons=list(),
lines=list(),
polylines=list())
bindEvent <- function(eventExpr, callback, env=parent.frame(), quoted=FALSE) {
eventFunc <- exprToFunction(eventExpr, env, quoted)
initialized <- FALSE
invisible(observe({
eventVal <- eventFunc()
if (!initialized)
initialized <<- TRUE
else
isolate(callback())
}))
}
#######Functions and reactiveValues######
shinyServer(function(input, output, session) {
client<-reactiveValues(selected=NULL,
buffer=NULL,
events=list())
####Add marker######
bindEvent(input$map_click, function() {
if (!input$addMarkerOnClick)
return()
event <- input$map_click
client$selected<-data.frame(lng=event$lng,
lat=event$lat,
layerId=as.character(as.integer(length(server$items)+1)),
pointId=as.character(as.integer(length(server$items)+1)),
user=as.character(input$user))
server$items[[length(server$items)+1]]<-server$points[[length(server$points)+1]]<-client$selected
leafletProxy("map") %>% addMarkers(lng=as.double(event$lng),lat=as.double(event$lat), layerId = client$selected$layerId)
saveRDS(server$items,"items.Rds")
})
####Add marker######
####Add polygon######
#Creates a new point on the map and adds it to the list of points (buffer) that will later define the polygon
bindEvent(input$map_click, function() {
if (!input$addPolygonOnClick)
return()
#Saves the event for debugging
client$events[[length(client$events)+1]]<-input$map_click
saveRDS(client$events,"events.Rds")
event <- input$map_click
client$selected<-data.frame(lng=event$lng,
lat=event$lat,
layerId=NA,
pointId=as.character(as.integer(length(server$items)+1)),
user=as.character(input$user))
server$items[[length(server$items)+1]]<-server$points[[length(server$points)+1]]<-client$selected
leafletProxy("map") %>% addMarkers(lng=as.double(event$lng),
lat=as.double(event$lat),
layerId = client$selected$layerId)
client$buffer<-rbind(client$buffer,client$selected)
leafletProxy("map") %>% addPolygons(lng=client$buffer$lng,
lat=client$buffer$lat,
layerId = "buffer")
})
#Adds an existing point to the list of points (buffer) that will later define the polygon
bindEvent(input$map_marker_click, function() {
if (!input$addPolygonOnClick)
return()
#Saves the event for debugging
client$events[[length(client$events)+1]]<-input$map_marker_click
saveRDS(client$events,"events.Rds")
#Then we add the point to the list
event<-input$map_marker_click
client$selected<-data.frame(lng=event$lng,
lat=event$lat,
layerId=NA,
pointId=event$id,
user=as.character(input$user))
client$buffer<-rbind(client$buffer,client$selected)
leafletProxy("map") %>% addPolygons(lng=client$buffer$lng,
lat=client$buffer$lat,
layerId = "buffer")
})
####Add polygons####
####Output####
output$selected <- renderTable({
data<-client$selected
data
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles(urlTemplate = "http://{s}.
tile.openstreetmap.org/{z}/{x}/{y}.png", attribution = NULL, layerId = NULL, options = tileOptions()) %>%
setView(2,46,6)
})
})
####Output####
I hope that helps.