Skip to content

Commit

Permalink
fix checkDim / checkDimPop, remove warnings for diff lengths, fix dep…
Browse files Browse the repository at this point in the history
…endency order
  • Loading branch information
trafficonese committed Dec 3, 2023
1 parent 5bc0eff commit 5eafebc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 77 deletions.
28 changes: 16 additions & 12 deletions R/utils-color.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@
#' makeColorMatrix(data.frame(matrix(1:99, ncol = 3, byrow = TRUE)), data.frame(x=c(1:33)))
#'
#' ## For characters
#' library(leaflet)
#' makeColorMatrix("red", breweries91)
#' makeColorMatrix("blue", breweries91)
#' makeColorMatrix("#36ba01", breweries91)
#' makeColorMatrix("founded", data.frame(breweries91))
#' testdf <- data.frame(
#' texts = LETTERS[1:10],
#' vals = 1:10,
#' vals1 = 11:20
#' )
#' makeColorMatrix("red", testdf)
#' makeColorMatrix("val", testdf)
#'
#' ## For formulaes
#' makeColorMatrix(~founded, breweries91)
#' makeColorMatrix(~founded + zipcode, breweries91)
#' makeColorMatrix(~vals, testdf)
#' makeColorMatrix(~vals1, testdf)
#'
#' ## For JSON
#' library(jsonify)
Expand Down Expand Up @@ -160,8 +162,9 @@ makeColorMatrix.POSIXlt <- makeColorMatrix.Date
#' @param x The color vector
#' @param data The dataset
checkDim <- function(x, data) {
if (is.null(data)) { return(x) }
if (inherits(data, "sfc")) nro_d = length(data) else nro_d = nrow(data)
if (length(grep("MULTI", sf::st_geometry_type(data))) > 0) {
if (inherits(data, c("sf","sfc")) && length(grep("MULTI", sf::st_geometry_type(data))) > 0) {
lnths = lengths(sf::st_geometry(data))
} else {
lnths = nro_d
Expand All @@ -172,11 +175,12 @@ checkDim <- function(x, data) {
" Just the first color is used.")
x <- x[1,,drop = FALSE]
}
} else {
}
else {
len_x <- length(x)
if ((length(x) != 1) && (len_x != nro_d)) {
warning("Length of color vector does not match number of data rows.\n",
" The vector is repeated to match the number of rows.")
if ((len_x != nro_d)) {
# warning("Length of color vector does not match number of data rows.\n",
# " The vector is repeated to match the number of rows.")
x <- rep(x, ceiling(nro_d / len_x))[1:nro_d]
}
if (any(lnths != 1) & length(lnths) == nro_d & length(x) != 1) {
Expand Down
5 changes: 3 additions & 2 deletions R/utils-popup.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ makePopup.default <- function(x, data) {
#' @param x The popup vector
#' @param data The dataset
checkDimPop <- function(x, data) {
if (is.null(data)) { return(x) }
if (inherits(data, "sfc")) nro_d = length(data) else nro_d = nrow(data)
len_x <- length(x)
if (len_x != nro_d) {
warning("Length of popups does not match number of data rows.\n",
" The vector is repeated to match the number of rows.")
# warning("Length of popups does not match number of data rows.\n",
# " The vector is repeated to match the number of rows.")
x <- rep(x, ceiling(nro_d / len_x))[1:nro_d]
}
return(x)
Expand Down
16 changes: 9 additions & 7 deletions man/makeColorMatrix.Rd

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

9 changes: 6 additions & 3 deletions tests/testthat/test-leafgl-addGlPoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ test_that("addGlPoints works", {
m = leaflet() %>%
addGlPoints(data = pts, layerId = "someid", src = TRUE)
expect_is(m, "leaflet")
expect_identical(m$dependencies[[length(m$dependencies)]]$name, paste0("someid","dat"))
expect_identical(m$dependencies[[length(m$dependencies)-1]]$name, paste0("someid","dat"))
expect_identical(m$dependencies[[length(m$dependencies)]]$name, paste0("someid","col"))

m = leaflet() %>%
addGlPoints(data = pts, group = NULL)
Expand All @@ -28,12 +29,14 @@ test_that("addGlPoints works", {
m = leaflet() %>%
addGlPoints(data = pts, layerId = NULL, group = NULL, src = TRUE)
expect_is(m, "leaflet")
expect_identical(m$dependencies[[length(m$dependencies)]]$name, "data-ptsdat")
expect_identical(m$dependencies[[length(m$dependencies)-1]]$name, "data-ptsdat")
expect_identical(m$dependencies[[length(m$dependencies)]]$name, "data-ptscol")

m = leaflet() %>%
addGlPoints(data = breweries91, src = TRUE)
expect_is(m, "leaflet")
expect_identical(m$dependencies[[length(m$dependencies)]]$name, "glpoints-ptsdat")
expect_identical(m$dependencies[[length(m$dependencies)-1]]$name, "glpoints-ptsdat")
expect_identical(m$dependencies[[length(m$dependencies)]]$name, "glpoints-ptscol")

m = leaflet() %>%
addGlPoints(data = breweries91, src = TRUE, radius = 5)
Expand Down
23 changes: 11 additions & 12 deletions tests/testthat/test-leafgl-colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test_that("Character as color", {

m <- leaflet() %>%
addGlPolygons(data = polys,
color = "NUTS_ID",
color = "NAME_1",
group = "lns");
expect_is(m, "leaflet")
expect_is(m$x$calls[[1]]$args[[2]], "json")
Expand Down Expand Up @@ -120,7 +120,7 @@ test_that("Character as color", {
## Character - Character Column Name #############
m <- leaflet() %>%
addGlPolylines(data = lines,
color = "FGN",
color = "Name",
group = "lns");
expect_is(m, "leaflet")
expect_is(m$x$calls[[1]]$args[[2]], "json")
Expand All @@ -130,7 +130,7 @@ test_that("Character as color", {
## Character - Character Column Name with Palette #############
m <- leaflet() %>%
addGlPolylines(data = lines,
color = "FGN",
color = "MaxWind",
palette = "magma",
group = "lns");
expect_is(m, "leaflet")
Expand Down Expand Up @@ -402,10 +402,10 @@ test_that("Numeric as color", {
expect_true(validate_json(m$x$calls[[1]]$args[[2]]))
rm(m)

m <- expect_warning(leaflet() %>%
m <- leaflet() %>%
addGlPoints(data = pts,
fillColor = as.factor(c("asd","bdc","fds")),
group = "pts"))
group = "pts")
expect_is(m, "leaflet")
expect_is(m$x$calls[[1]]$args[[2]], "json")
expect_true(validate_json(m$x$calls[[1]]$args[[2]]))
Expand Down Expand Up @@ -434,10 +434,10 @@ test_that("Numeric as color", {

test_that("List as color", {
## List ###################
m <- expect_warning(leaflet() %>%
m <- leaflet() %>%
addGlPoints(data = pts,
fillColor = list(1,2),
group = "pts"))
group = "pts")
expect_is(m, "leaflet")
expect_is(m$x$calls[[1]]$args[[2]], "json")
expect_true(validate_json(m$x$calls[[1]]$args[[2]]))
Expand Down Expand Up @@ -471,10 +471,10 @@ test_that("List as color", {
expect_true(validate_json(m$x$calls[[1]]$args[[2]]))
rm(m)

m <- expect_warning(leaflet() %>%
m <- leaflet() %>%
addGlPoints(data = pts,
fillColor = list(c(100,200), cbind(2,1)),
group = "pts"))
group = "pts")
expect_is(m, "leaflet")
expect_is(m$x$calls[[1]]$args[[2]], "json")
expect_true(validate_json(m$x$calls[[1]]$args[[2]]))
Expand Down Expand Up @@ -579,11 +579,10 @@ test_that("Warnings / Errors", {
expect_true(validate_json(m$x$calls[[1]]$args[[2]]))
rm(m)

## Warnings ###################
m <- expect_warning(leaflet() %>%
m <- leaflet() %>%
addGlPoints(data = pts,
fillColor = 1:33,
group = "pts"))
group = "pts")
expect_is(m, "leaflet")
expect_is(m$x$calls[[1]]$args[[2]], "json")
expect_true(validate_json(m$x$calls[[1]]$args[[2]]))
Expand Down
Loading

0 comments on commit 5eafebc

Please sign in to comment.