Error in addLegend: unused arguments
amegbor opened this issue · comments
Hello,
I encountered an issue while attempting to incorporate a legend into my Leaflet map. The error message received is: "Error in addLegend(., pal = pal, position = "bottomleft") : unused arguments (pal = pal, position = "bottomleft")."
Interestingly, the Leaflet map functions properly when the addLegend option is omitted. Please see the reproducible example using a sample code and data from https://edzer.github.io/sdsr_exercises/12.html
library(leaflet)
#> Warning: package 'leaflet' was built under R version 4.3.2
library(tidyverse) |> suppressPackageStartupMessages()
#> Warning: package 'readr' was built under R version 4.3.2
#> Warning: package 'dplyr' was built under R version 4.3.2
#> Warning: package 'stringr' was built under R version 4.3.2
no2 <- read_csv(system.file("external/no2.csv",
package = "gstat"), show_col_types = FALSE)
library(sf)
#> Warning: package 'sf' was built under R version 4.3.2
#> Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
# Linking to GEOS 3.11.1, GDAL 3.6.4, PROJ 9.1.1; sf_use_s2() is TRUE
crs <- st_crs("EPSG:32632")
st_as_sf(no2, crs = "OGC:CRS84", coords =
c("station_longitude_deg", "station_latitude_deg")) |>
st_transform(crs) -> no2.sf
read_sf("C:/Users/pma311/Downloads/de_nuts1.gpkg") |> st_transform(crs) -> de
ggplot() + geom_sf(data = de) +
geom_sf(data = no2.sf, mapping = aes(col = NO2))
library(stars) |> suppressPackageStartupMessages()
st_bbox(de) |>
st_as_stars(dx = 10000) |>
st_crop(de) -> grd
grd
#> stars object with 2 dimensions and 1 attribute
#> attribute(s):
#> Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
#> values 0 0 0 0 0 0 2076
#> dimension(s):
#> from to offset delta refsys x/y
#> x 1 65 280741 10000 WGS 84 / UTM zone 32N [x]
#> y 1 87 6101239 -10000 WGS 84 / UTM zone 32N [y]
# stars object with 2 dimensions and 1 attribute
# attribute(s):
# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
# values 0 0 0 0 0 0 2076
# dimension(s):
# from to offset delta refsys x/y
# x 1 65 280741 10000 WGS 84 / UTM z... [x]
# y 1 87 6101239 -10000 WGS 84 / UTM z... [y]
library(gstat)
#> Warning: package 'gstat' was built under R version 4.3.2
i <- idw(NO2~1, no2.sf, grd)
#> [inverse distance weighted interpolation]
# [inverse distance weighted interpolation]
require(fields)
#> Loading required package: fields
#> Loading required package: spam
#> Warning: package 'spam' was built under R version 4.3.2
#> Spam version 2.10-0 (2023-10-23) is loaded.
#> Type 'help( Spam)' or 'demo( spam)' for a short introduction
#> and overview of this package.
#> Help for individual functions is also obtained by adding the
#> suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
#>
#> Attaching package: 'spam'
#> The following objects are masked from 'package:base':
#>
#> backsolve, forwardsolve
#> Loading required package: viridisLite
#>
#> Try help(fields) to get started.
#>
#> Attaching package: 'fields'
#> The following object is masked from 'package:leaflet':
#>
#> addLegend
zlim<- c(1.5,21.0)
pal <- colorNumeric(tim.colors(), domain = c(1.5,21.0),
na.color = "transparent")
require(leafem)
#> Loading required package: leafem
#> Warning: package 'leafem' was built under R version 4.3.2
leaflet() %>%
addProviderTiles("OpenStreetMap") %>%
addStarsImage(i, colors = pal, opacity = 0.5) %>%
addLegend(pal=pal,position="bottomleft")
#> Error in addLegend(., pal = pal, position = "bottomleft"): unused arguments (pal = pal, position = "bottomleft")
Created on 2024-02-19 with reprex v2.1.0
#> Attaching package: 'fields' #> The following object is masked from 'package:leaflet': #> #> addLegend
The {fields}
package has its own addLegend
function.
Please use leaflet::addLegend(pal=pal, position="bottomleft")
to get around this error.
Thank you for the clarification, Schloerke. I overlooked that particular warning or information in the Fields package.