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

refactor: move {raster} and {terra} to Suggests #943

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
4 changes: 1 addition & 3 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ jobs:
uses: rstudio/shiny-workflows/.github/workflows/website.yaml@v1
routine:
uses: rstudio/shiny-workflows/.github/workflows/routine.yaml@v1
with:
node-version: '12'
R-CMD-check:
uses: rstudio/shiny-workflows/.github/workflows/R-CMD-check.yaml@v1
with:
minimum-r-version: "3.5.0"
ubuntu: "ubuntu-20.04"
ubuntu: "ubuntu-22.04"
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ Imports:
magrittr,
methods,
png,
raster (>= 3.6.3),
rlang,
RColorBrewer,
scales (>= 1.0.0),
sf (>= 0.9-6),
stats,
terra (>= 1.6-3),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to keep {terra} as a Suggests? To me, it's similar to {hexbin} being suggested by {ggplot2}.

If a package w/ install difficulties is not critical path to the main package, I like to make the package a Suggest dependency when possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If so, this will require a few reverts to rlang::check_installed("terra")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's do this! Could you run revdep checks after merge? I could help if some revdeps are failing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you run revdep checks after merge?

Sure, np! I can also run it on this branch and push back results, if you'd like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏃

viridisLite,
xfun
Suggests:
knitr,
maps,
purrr,
R6,
raster (>= 3.6-3),
RJSONIO,
rmarkdown,
s2,
shiny (>= 1.0.0),
sp,
terra,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Config/Needs/website: dplyr, ncdf4, rnaturalearth, tidyverse/tidytemplate
Expand Down
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# leaflet (development version)

* `{leaflet}` no longer install sp by default and attempts to convert object to sf internally before creating a map and warns when it fails conversion (@olivroy, #942).
* `{leaflet}` no longer installs `{raster}`by default and uses terra (@olivroy, #943).

* `{leaflet}` no longer installs `{sp}` by default and attempts to convert object to `{sf}` internally before creating a map and warns when it fails conversion (@olivroy, #942).

* The `breweries91`, `atlStorms2005`, and `gadmCHE` datasets are now `{sf}` objects (@olivroy, #944).

Expand All @@ -10,8 +12,6 @@

* Updated vignettes to replace `{sp}`/`{raster}` usage with `{sf}`/`{terra}` and their corresponding examples. (@jack-davison, #928)

* Updated vignettes to replace `{sp}`/`{raster}` usage with `{sf}`/`{terra} and their corresponding examples. (@jack-davison, #928)

* `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)

# leaflet 2.2.2
Expand Down
1 change: 1 addition & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ NULL
#' @description Administrative borders of Switzerland (level 1)
#' @details This dataset comes from <https://gadm.org>.
#' It was downloaded using [raster::getData()].
#' @format `sp::SpatialPolygonsDataFrame`
#' @format `sf` object
#' @family built in datasets
#' @source
Expand Down
20 changes: 10 additions & 10 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
#' @param method the method used for computing values of the new, projected raster image.
#' `"bilinear"` (the default) is appropriate for continuous data,
#' `"ngb"` - nearest neighbor - is appropriate for categorical data.
#' Ignored if `project = FALSE`. See [projectRaster()] for details.
#' Ignored if `project = FALSE`. See [terra::project()] for details.
#' @param maxBytes the maximum number of bytes to allow for the projected image
#' (before base64 encoding); defaults to 4MB.
#' @param options a list of additional options, intended to be provided by
Expand All @@ -237,11 +237,11 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
#' SpatRaster with a color table.
#'
#' @examples
#' \donttest{library(raster)
#' \donttest{
#'
#' r <- raster(xmn = -2.8, xmx = -2.79, ymn = 54.04, ymx = 54.05, nrows = 30, ncols = 30)
#' values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
#' crs(r) <- CRS("+init=epsg:4326")
#' r <- terra::rast(xmin = -2.8, xmax = -2.79, ymin = 54.04, ymax = 54.05, nrows = 30, ncols = 30)
#' terra::values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
#' terra::crs(r) <- "epsg:4326"
#'
#' pal <- colorNumeric("Spectral", domain = c(0, 1000))
#' leaflet() %>% addTiles() %>%
Expand Down Expand Up @@ -401,7 +401,7 @@ addRasterImage_RasterLayer <- function(
data = getMapData(map)
) {


rlang::check_installed("raster")
options$opacity <- opacity
options$attribution <- attribution

Expand All @@ -425,8 +425,8 @@ addRasterImage_RasterLayer <- function(

bounds <- raster::extent(
raster::projectExtent(
raster::projectExtent(x, crs = sp::CRS(epsg3857)),
crs = sp::CRS(epsg4326)
raster::projectExtent(x, crs = raster::crs(epsg3857)),
crs = raster::crs(epsg4326)
)
)

Expand Down Expand Up @@ -477,7 +477,6 @@ addRasterImage_SpatRaster <- function(
options = gridOptions(),
data = getMapData(map)
) {
rlang::check_installed("terra (>= 1.6-3)", reason = "to use addRasterImage() for SpatRaster objects.") # for terra::has.RGB()

options$opacity <- opacity
options$attribution <- attribution
Expand Down Expand Up @@ -581,10 +580,11 @@ projectRasterForLeaflet <- function(x, method) {
method=method
)
} else {
rlang::check_installed("raster")
raster_is_factor <- raster::is.factor(x);
projected <- raster::projectRaster(
x,
raster::projectExtent(x, crs = sp::CRS(epsg3857)),
raster::projectExtent(x, crs = raster::crs(epsg3857)),
method = method
)
# if data is factor data, make the result factors as well.
Expand Down
11 changes: 7 additions & 4 deletions R/leaflet.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ leafletSizingPolicy <- function(

#' Create a Leaflet map widget
#'
#' This function creates a Leaflet map widget using \pkg{htmlwidgets}. The
#' @description
#' * `leaflet()` creates a Leaflet map widget using \pkg{htmlwidgets}. The
#' widget can be rendered on HTML pages generated from R Markdown, Shiny, or
#' other applications.
#' * `leafletOptions()`: options for map creation
#' * `leafletCRS()`: class to create custom CRS.
#'
#' @details
#' The `data` argument is only needed if you are going to reference
#' variables in this object later in map layers. For example, `data` can be
#' a data frame containing columns `latitude` and `longitude`, then
Expand Down Expand Up @@ -152,13 +156,12 @@ mapOptions <- function(map, zoomToLimits = c("always", "first", "never")) {
#' @param minZoom Minimum zoom level of the map. Overrides any `minZoom` set on map layers.
#' @param maxZoom Maximum zoom level of the map. This overrides any `maxZoom` set on map layers.
#' @param crs Coordinate Reference System to use. Don't change this if you're not sure what it means.
#' @seealso [leafletCRS()] for creating a custom CRS.
#' @param worldCopyJump With this option enabled, the map tracks when you pan
#' to another "copy" of the world and seamlessly jumps to the original
#' one so that all overlays like markers and vector layers are still visible.
#' @param preferCanvas Whether leaflet.js Paths should be rendered on a Canvas renderer.
#' @param ... other options used for leaflet.js map creation.
#' @describeIn leaflet Options for map creation
#' @rdname leaflet
#' @seealso See <https://web.archive.org/web/20220702182250/https://leafletjs.com/reference-1.3.4.html#map-option> for details and more options.
#' @export
leafletOptions <- function(
Expand Down Expand Up @@ -200,7 +203,7 @@ crsClasses <- list("L.CRS.EPSG3857", "L.CRS.EPSG4326", "L.CRS.EPSG3395",
#' Proj4Leaflet will use this in the getSize method, otherwise
#' defaulting to Leaflet's default CRS size
#' @param tileSize DEPRECATED! Specify the tilesize in the [tileOptions()] argument.
#' @describeIn leaflet class to create a custom CRS
#' @rdname leaflet
#' @export
leafletCRS <- function(
crsClass = "L.CRS.EPSG3857",
Expand Down
1 change: 0 additions & 1 deletion R/normalize-terra.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ polygonData.SpatVector <- function(obj) {

# helpers -----------------------------------------------------------------
check_crs_terra <- function(x) {
rlang::check_installed("terra")
crs <- terra::crs(x)

# Don't have enough information to check
Expand Down
9 changes: 4 additions & 5 deletions inst/examples/geojson.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
library(leaflet)
library(sp)
#' <br/><br/>
#' The V8 part is simply to read the JSON embeded in the Javascript.<br/>
#' For a geojson file `jsonlite::fromfromJSON()` or `geojsonio::regeojson_read()` will do
#' For a geojson file `jsonlite::fromJSON()` or `geojsonio::geojson_read()` will do
#'
jsURL <- "https://rawgit.com/Norkart/Leaflet-MiniMap/master/example/local_pubs_restaurant_norway.js"
v8 <- V8::v8()
v8$source(jsURL)
geoJson <- geojsonio::as.json(v8$get("pubsGeoJSON"))

# This is the kicker, convert geojson to a Spatial object.
# This is the kicker, convert geojson to a sf object.
# This then allows us to use formulas in our markers, polygons etc.
spdf <- geojsonio::geojson_sp(geoJson)
spdf <- geojsonio::geojson_sf(geoJson)

icons <- awesomeIconList(
pub = makeAwesomeIcon(icon = "glass", library = "fa", markerColor = "red"),
Expand All @@ -34,7 +33,7 @@ leaflet() %>%
#' Another examples this time with polygons
url <- "https://www.partners-popdev.org/wp-content/themes/original-child/vendor/Geojson/States/Maharashtra.geojson"

mhSPDF <- geojsonio::geojson_read(url, what = "sp")
mhSPDF <- sf::st_read(url)

cols <- colorFactor(topo.colors(nrow(mhSPDF)), mhSPDF$NAME_2)

Expand Down
1 change: 0 additions & 1 deletion inst/examples/leaflet.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,3 @@ m %>% addCircleMarkers(~lng, ~lat, radius = ~size,
color = ~greens(value), fillOpacity = 0.5)

}

1 change: 1 addition & 0 deletions inst/examples/normalize.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# obsolete
library(leaflet)
library(sp)
library(maps)
Expand Down
4 changes: 2 additions & 2 deletions inst/examples/proj4Leaflet-PolarProjections.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ resolutions <- c(8192, 4096, 2048, 1024, 512, 256)
zoom <- 0
maxZoom <- 5

border <- geojsonio::geojson_read(system.file("examples/Seamask_medium_res_polygon.kml", package = "leaflet"), what = "sp")
points <- geojsonio::geojson_read(system.file("examples/Historic_sites_and_monuments_point.kml", package = "leaflet"), what = "sp")
border <- sf::st_read(system.file("examples/Seamask_medium_res_polygon.kml", package = "leaflet"))
points <- sf::st_read(system.file("examples/Historic_sites_and_monuments_point.kml", package = "leaflet"))

crsAntartica <- leafletCRS(
crsClass = "L.Proj.CRS",
Expand Down
10 changes: 5 additions & 5 deletions man/addRasterImage.Rd

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

16 changes: 5 additions & 11 deletions man/leaflet.Rd

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

1 change: 0 additions & 1 deletion scripts/raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ plot(r - invn)
r <- raster(xmn = -60, xmx = -25, ymn = 70, ymx = 81, nrows = 30, ncols = 30)
set.seed(0)
values(r) <- matrix(sample(1:5, 900, replace = TRUE), nrow(r), ncol(r), byrow = TRUE)
# crs(r) <- CRS("+init=epsg:4326")
l <- function(method = "auto") {
leaflet() %>%
addTiles() %>%
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ create_square <- function(width = 2, lng = 0, lat = 0, hole = FALSE, type = sp::
}
}


expect_maps_equal <- function(m1, m2) {
attr(m1$x, "leafletData") <- NULL
attr(m2$x, "leafletData") <- NULL
expect_equal(m1, m2, ignore_function_env = TRUE, ignore_formula_env = TRUE)
}
7 changes: 0 additions & 7 deletions tests/testthat/test-normalize-2.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@

expect_maps_equal <- function(m1, m2) {
attr(m1$x, "leafletData") <- NULL
attr(m2$x, "leafletData") <- NULL
expect_equal(m1, m2, ignore_function_env = TRUE, ignore_formula_env = TRUE)
}

normalize_multipolygon <- function(df) {
# A multipolygon is a nested list of lng/lat data frames. Each data frame
# represents a single polygon (may be an island or a hole), that is, a series
Expand Down
12 changes: 2 additions & 10 deletions tests/testthat/test-normalize-3.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@

expect_maps_equal <- function(m1, m2) {
attr(m1$x, "leafletData") <- NULL
attr(m2$x, "leafletData") <- NULL
expect_equal(m1, m2, ignore_function_env = TRUE, ignore_formula_env = TRUE)
}


test_that("normalize terra", {
skip_if_not_installed("raster")
skip_if_not_installed("sp")
Expand All @@ -20,7 +12,7 @@ test_that("normalize terra", {
leaflet(x) %>% addTiles() %>% addPolygons()
}

poldata <- vect(gadmCHE)
poldata <- terra::vect(gadmCHE)
crs(poldata) <- "+proj=longlat +datum=WGS84"

(r1 <- pgontest(poldata))
Expand All @@ -30,7 +22,7 @@ test_that("normalize terra", {

### lines -----------------------------------------------------------------

lindata <- vect(atlStorms2005)
lindata <- terra::vect(atlStorms2005)
crs(lindata) <- "+proj=longlat +datum=WGS84"

plinetest <- function(x) {
Expand Down
11 changes: 2 additions & 9 deletions tests/testthat/test-raster.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@

expect_maps_equal <- function(m1, m2) {
attr(m1$x, "leafletData") <- NULL
attr(m2$x, "leafletData") <- NULL
expect_equal(m1, m2, ignore_function_env = TRUE, ignore_formula_env = TRUE)
}

# Some proj4string values differ only by one having whole numbers represented as
# x while others have x.0. So, strip each trailing .0 value.
normalize_zero_values <- function(str) {
gsub("=(\\d+).0( |$)", "=\\1\\2", str)
}

test_that("rasters", {
skip_if_not_installed("terra")
skip_if_not_installed("raster")

library(terra)
library(raster)
Expand All @@ -37,7 +30,7 @@ test_that("rasters", {

expect_maps_equal(r1, r2)

# test with color map
# test with color map
r <- terra::rast(ncols=10, nrows=10, vals=rep_len(10:15, length.out=100), xmin=0, xmax=10^6, ymin=0, ymax=10^6, crs=pmerc)
r[5,] <- NA
coltab(r) <- c(rep("#FFFFFF", 10), rainbow(6, end=.9))
Expand Down
Loading