Skip to content

Commit

Permalink
Merge pull request #1 from schloerke/barret/addLegend_data
Browse files Browse the repository at this point in the history
added data arg to addXXX methods, including addLegend
  • Loading branch information
schloerke authored Feb 12, 2018
2 parents 69a5211 + 6961167 commit f273edd
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@
^inst/htmlwidgets/lib/leaflet-measure/leaflet-measure.js$
^inst/htmlwidgets/plugins/Proj4Leaflet/proj4.js$
^revdep/
^\.lintr$
^man-roxygen/
32 changes: 19 additions & 13 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,16 @@ addTiles <- function(
attribution = NULL,
layerId = NULL,
group = NULL,
options = tileOptions()
options = tileOptions(),
data = getMapData(map)
) {
options$attribution = attribution
if (missing(urlTemplate) && is.null(options$attribution))
options$attribution = paste(
'&copy; <a href="http://openstreetmap.org">OpenStreetMap</a>',
'contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
)
invokeMethod(map, getMapData(map), 'addTiles', urlTemplate, layerId, group,
invokeMethod(map, data, 'addTiles', urlTemplate, layerId, group,
options)
}

Expand Down Expand Up @@ -217,6 +218,7 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
#' Ignored if \code{project = FALSE}. See \code{\link{projectRaster}} for details.
#' @param maxBytes the maximum number of bytes to allow for the projected image
#' (before base64 encoding); defaults to 4MB.
#' @template data-getMapData
#'
#' @examples
#' library(raster)
Expand All @@ -238,7 +240,8 @@ addRasterImage <- function(
group = NULL,
project = TRUE,
method = c("bilinear", "ngb"),
maxBytes = 4*1024*1024
maxBytes = 4*1024*1024,
data = getMapData(map)
) {
stopifnot(inherits(x, "RasterLayer"))

Expand Down Expand Up @@ -268,7 +271,7 @@ addRasterImage <- function(
list(raster::ymin(bounds), raster::xmax(bounds))
)

invokeMethod(map, getMapData(map), "addRasterImage", uri, latlng, opacity, attribution, layerId, group) %>%
invokeMethod(map, data, "addRasterImage", uri, latlng, opacity, attribution, layerId, group) %>%
expandLimits(c(raster::ymin(bounds), raster::ymax(bounds)), c(raster::xmin(bounds), raster::xmax(bounds)))
}

Expand Down Expand Up @@ -377,14 +380,15 @@ clearTiles <- function(map) {
#' @export
addWMSTiles <- function(
map, baseUrl, layerId = NULL, group = NULL,
options = WMSTileOptions(), attribution = NULL, layers = ''
options = WMSTileOptions(), attribution = NULL, layers = '',
data = getMapData(map)
) {
if(identical(layers, '')) {
stop("layers is a required argument with comma-separated list of WMS layers to show")
}
options$attribution = attribution
options$layers = layers
invokeMethod(map, getMapData(map), 'addWMSTiles', baseUrl, layerId, group, options)
invokeMethod(map, data, 'addWMSTiles', baseUrl, layerId, group, options)
}

#' @param styles comma-separated list of WMS styles
Expand Down Expand Up @@ -426,9 +430,7 @@ WMSTileOptions <- function(
#' Human-friendly group names are permitted--they need not be short,
#' identifier-style names. Any number of layers and even different types of
#' layers (e.g. markers and polygons) can share the same group name.
#' @param data the data object from which the argument values are derived; by
#' default, it is the \code{data} object provided to \code{leaflet()}
#' initially, but can be overridden
#' @template data-getMapData
#' @describeIn map-layers Add popups to the map
#' @export
addPopups <- function(
Expand Down Expand Up @@ -1173,14 +1175,15 @@ addGeoJSON <- function(map, geojson, layerId = NULL, group = NULL,
dashArray = NULL,
smoothFactor = 1.0,
noClip = FALSE,
options = pathOptions()
options = pathOptions(),
data = getMapData(map)
) {
options = c(options, filterNULL(list(
stroke = stroke, color = color, weight = weight, opacity = opacity,
fill = fill, fillColor = fillColor, fillOpacity = fillOpacity,
dashArray = dashArray, smoothFactor = smoothFactor, noClip = noClip
)))
invokeMethod(map, getMapData(map), 'addGeoJSON', geojson, layerId, group, options)
invokeMethod(map, data, 'addGeoJSON', geojson, layerId, group, options)
}

#' @rdname remove
Expand Down Expand Up @@ -1212,6 +1215,7 @@ clearGeoJSON <- function(map) {
#' 'bottomright'
#' @param options a list of additional options, intended to be provided by
#' a call to \code{layersControlOptions}
#' @template data-getMapData
#'
#' @examples
#' \donttest{
Expand All @@ -1229,10 +1233,12 @@ clearGeoJSON <- function(map) {
addLayersControl <- function(map,
baseGroups = character(0), overlayGroups = character(0),
position = c('topright', 'bottomright', 'bottomleft', 'topleft'),
options = layersControlOptions()) {
options = layersControlOptions(),
data = getMapData(map)
) {

options = c(options, list(position = match.arg(position)))
invokeMethod(map, getMapData(map), 'addLayersControl', baseGroups,
invokeMethod(map, data, 'addLayersControl', baseGroups,
overlayGroups, options)
}

Expand Down
9 changes: 6 additions & 3 deletions R/legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@
#' group is added/removed, for example via layerControl.
#' You will need to set the \code{group} when you add a layer
#' (e.g. \code{\link{addPolygons}}) and supply the same name here.
#' @param data the data object from which the argument values are derived; by
#' default, it is the \code{data} object provided to \code{leaflet()}
#' initially, but can be overridden
#' @example inst/examples/legend.R
#' @export
addLegend <- function(
map, position = c('topright', 'bottomright', 'bottomleft', 'topleft'),
pal, values, na.label = 'NA', bins = 7, colors, opacity = 0.5, labels = NULL,
labFormat = labelFormat(), title = NULL, className = "info legend",
layerId = NULL, group = NULL
layerId = NULL, group = NULL, data = getMapData(map)
) {
position = match.arg(position)
type = 'unknown'; na.color = NULL
Expand All @@ -73,7 +76,7 @@ addLegend <- function(

# a better default title when values is formula
if (missing(title) && inherits(values, 'formula')) title = deparse(values[[2]])
values = evalFormula(values, getMapData(map))
values = evalFormula(values, data)

type = attr(pal, 'colorType', exact = TRUE)
args = attr(pal, 'colorArgs', exact = TRUE)
Expand Down Expand Up @@ -155,7 +158,7 @@ addLegend <- function(
position = position, type = type, title = title, extra = extra,
layerId = layerId, className = className, group = group
)
invokeMethod(map, getMapData(map), "addLegend", legend)
invokeMethod(map, data, "addLegend", legend)
}

#' @param prefix a prefix of legend labels
Expand Down
3 changes: 3 additions & 0 deletions man-roxygen/data-getMapData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#' @param data the data object from which the argument values are derived; by
#' default, it is the \code{data} object provided to \code{leaflet()}
#' initially, but can be overridden
7 changes: 6 additions & 1 deletion man/addLayersControl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/addLegend.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/addRasterImage.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions man/map-layers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f273edd

Please sign in to comment.