Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check tile providers in addProviderTiles() #929

Merged
merged 11 commits into from
Aug 7, 2024
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Color palette improvements. All color palette functions now support all `{viridisLite}` palettes ("magma", "inferno", "plasma", "viridis", "cividis", "rocket", "mako", and "turbo").

* `addProviderTiles()` will now error if the chosen `provider` does not match any currently loaded provider (by default, those in `providers`). This behaviour can be toggled off by setting the new `.check` argument to `FALSE` (@jack-davison, #929)
schloerke marked this conversation as resolved.
Show resolved Hide resolved

# leaflet 2.2.2

* Fixed #893: Correctly call `terra::crs()` when checking the CRS of a `SpatVector` object in `pointData()` or `polygonData()` (thanks @mkoohafkan, #894).
Expand Down
21 changes: 17 additions & 4 deletions R/plugin-providers.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ leafletProviderDependencies <- function() {
#' <https://github.com/leaflet-extras/leaflet-providers>)
#' @param layerId the layer id to assign
#' @param group the name of the group the newly created layers should belong to
#' (for [clearGroup()] and [addLayersControl()] purposes).
#' Human-friendly group names are permitted--they need not be short,
#' identifier-style names.
#' (for [clearGroup()] and [addLayersControl()] purposes). Human-friendly
#' group names are permitted--they need not be short, identifier-style names.
#' @param options tile options
#' @param check Check that the specified `provider` matches the available
#' currently loaded leaflet providers? Defaults to `TRUE`, but can be toggled
#' to `FALSE` for advanced users.
#' @return modified map object
#'
#' @examples
Expand All @@ -36,8 +38,19 @@ addProviderTiles <- function(
provider,
layerId = NULL,
group = NULL,
options = providerTileOptions()
options = providerTileOptions(),
check = TRUE
) {
if (check) {
loaded_providers <- leaflet.providers::providers_loaded()
if (!provider %in% names(loaded_providers$providers)) {
stop(
"Unknown tile provider '",
provider,
"'; either use a known provider or pass `check = FALSE` to `addProviderTiles()`"
)
}
}
map$dependencies <- c(map$dependencies, leafletProviderDependencies())
invokeMethod(map, getMapData(map), "addProviderTiles",
provider, layerId, group, options)
Expand Down
17 changes: 11 additions & 6 deletions man/addProviderTiles.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test-tiles.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

testthat::test_that("Checking of tile providers works correctly", {
expect_no_error(
leaflet() %>% addProviderTiles(providers[[1]])
)

expect_no_error(
leaflet() %>% addProviderTiles("FAKETILESET123", check = FALSE)
)

expect_error(
leaflet() %>% addProviderTiles("FAKETILESET123")
)
})
Loading