3

I want to develop an application where a user can draw a line, polygon etc...

I currently develop with R and leaflet package and I found that it is possible to work with Leaflet.draw under JavaScript.

My question is, can I operate it under R?

Edit :

I copied in my Directory : leaflet.draw.js,leaflet.draw.css,leaflet-draw-plugin.js

plugin-draw.R

leafletDrawDependencies <- function() {
  list(
    htmltools::htmlDependency(
      "Leaflet.draw",
      "0.2.3",
      system.file("Users/insa/Documents/Biblio-graphique/toolbar/dist", package = "leaflet"),
      script = "leaflet.draw.js",
      stylesheet="leaflet.draw.css"
    ),
    htmltools::htmlDependency(
      "leaflet-draw-plugin",
      "0.0.1",
      system.file("Users/insa/Documents/Biblio-graphique/toolbar", package = "leaflet"),
      script = "leaflet-draw-plugin.js"
    )
  )
}


addDrawToolbar <- function(map,layerID="drawnItems",
                           group = layerID,
                           position = c('topleft', 'topright', 'bottomleft',
                                        'bottomright'),
                           polyline=TRUE,polygon=TRUE,rectangle=TRUE,
                           circle=TRUE,marker=TRUE,edit=TRUE, remove=TRUE) {

  position = match.arg(position)
  map$dependencies <- c(map$dependencies, leafletDrawDependencies())
  map$drawToolbar<-T
  invokeMethod(map,getMapData(map),method =  'addDrawToolbar',layerID,group,position,
               polyline,polygon,rectangle,circle,marker,edit,remove)
}

drawPolylineOptions <- function(allowIntersection = TRUE,
                                drawError = list(color = "#b00b00", timeout = 2500),
                                guidelineDistance = 20,
                                shapeOptions = list(stroke = TRUE, color = '#f06eaa', weight = 4,
                                                    opacity = 0.5, fill = FALSE, clickable = TRUE
                                ), metric = TRUE, zIndexOffset = 2000, repeatMode = FALSE
) {
  list(
    allowIntersection = allowIntersection,
    drawError = drawError,
    guidelineDistance = guidelineDistance,
    shapeOptions = shapeOptions,
    metric = metric,
    zIndexOffset = zIndexOffset,
    repeatMode = repeatMode
  )
}


drawPolygonOptions <- function(allowIntersection = TRUE,
                               drawError = list(color = "#b00b00", timeout = 2500),
                               guidelineDistance = 20,
                               shapeOptions = list(stroke = TRUE, color = '#f06eaa', weight = 4,
                                                   opacity = 0.5, fill = TRUE, fillColor = NULL, fillOpacity = 0.2,
                                                   clickable = TRUE
                               ), metric = TRUE, zIndexOffset = 2000, repeatMode = FALSE, showArea = FALSE
) {
  if (isTRUE(showArea) && isTRUE(allowIntersection)) {
    warning("showArea = TRUE will be ignored because allowIntersection is TRUE")
  }

  list(
    allowIntersection = allowIntersection,
    drawError = drawError,
    guidelineDistance = guidelineDistance,
    shapeOptions = shapeOptions,
    metric = metric,
    zIndexOffset = zIndexOffset,
    repeatMode = repeatMode,
    showArea = showArea
  )
}

#' @rdname drawPolylineOptions
#' @export
drawRectangleOptions <- function(shapeOptions = list(
  stroke = TRUE, color = '#f06eaa', weight = 4, opacity = 0.5,
  fill = TRUE, fillColor = NULL, fillOpacity = 0.2, clickable = TRUE),
  metric = TRUE, repeatMode = FALSE) {

  list(
    shapeOptions = shapeOptions,
    metric = metric,
    repeatMode = repeatMode
  )
}

#' @rdname drawPolylineOptions
#' @param showRadius whether to show the radius of the drawn circle
#' @export
drawCircleOptions <- function(shapeOptions = list(
  stroke = TRUE, color = '#f06eaa', weight = 4, opacity = 0.5,
  fill = TRUE, fillColor = NULL, fillOpacity = 0.2, clickable = TRUE),
  showRadius = TRUE, metric = TRUE, repeatMode = FALSE) {

  list(
    shapeOptions = shapeOptions,
    showRadius = showRadius,
    metric = metric,
    repeatMode = repeatMode
  )
}

#' @rdname drawPolylineOptions
#'
#' @param icon a custom icon, as created by \code{\link{makeIcon}}
#'
#' @export
drawMarkerOptions <- function(icon = NULL, zIndexOffset = 2000,
                              repeatMode = FALSE) {

  if (!is.null(icon)) {
    if (!inherits(icon, "leaflet_icon")) {
      stop("Icon must be created using the makeIcon() function")
    }
    icon$iconUrl <- b64EncodePackedIcons(packStrings(icon$iconUrl))$data
    icon$iconRetinaUrl <- b64EncodePackedIcons(packStrings(icon$iconRetinaUrl))$data
    icon$iconShadowUrl <- b64EncodePackedIcons(packStrings(icon$iconShadowUrl))$data
    icon$iconShadowRetinaUrl <- b64EncodePackedIcons(packStrings(icon$iconShadowRetinaUrl))$data
  }

  filterNULL(list(icon = icon, zIndexOffset = zIndexOffset, repeatMode = repeatMode))
}

#' @describeIn addDrawToolbar
#' @export
removeDrawToolbar <- function(map){
  invokeMethod(map,getMapData(map),method =  'removeDrawToolbar')
}


map <- addTiles(leaflet())
map <- addDrawToolbar(map)
map

but the toolbar is not displayed, what's missing?

enter image description here

3

In short, Yes you can. You can find some good examples of tutorials as well as reading up on "Leaflet for R" using RStudio here, offering the following features:

Interactive panning/zooming
Compose maps using arbitrary combinations of:
    Map tiles
    Markers
    Polygons
    Lines
    Popups
    GeoJSON
Create maps right from the R console or RStudio
Embed maps in knitr/R Markdown documents and Shiny apps
Easily render Spatial objects from the sp package, 
or data frames with latitude/longitude columns
Use map bounds and mouse events to drive Shiny logic
  • 1
    But I'm not sure its trivial to include a Leaflet plugin to a leaflet map made with the R leaflet package. There's a couple of examples but the plugins need to be integrated into the package code itself, so that the generated leaflet map uses the plugin. – Spacedman Feb 29 '16 at 17:00
  • Yes, I found this program github.com/byzheng/leaflet/blob/… with plugin that I try followed but it does not work for me, I can show you what I did? – zina_GIS Feb 29 '16 at 18:50
  • 1
    Update your question with would you have tried already. – whyzar Feb 29 '16 at 18:51
1

To use leaflet.draw (and numerous other leaflet plugins) in R, try https://github.com/RCura/leaflet.extras.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.