diff --git a/R/utils-color.R b/R/utils-color.R index 3e24f21..0cec6d3 100644 --- a/R/utils-color.R +++ b/R/utils-color.R @@ -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) @@ -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 @@ -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) { diff --git a/R/utils-popup.R b/R/utils-popup.R index 481f2f2..d907a0a 100644 --- a/R/utils-popup.R +++ b/R/utils-popup.R @@ -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) diff --git a/man/makeColorMatrix.Rd b/man/makeColorMatrix.Rd index 8573981..1a646b2 100644 --- a/man/makeColorMatrix.Rd +++ b/man/makeColorMatrix.Rd @@ -43,15 +43,17 @@ makeColorMatrix(matrix(1:99, ncol = 3, byrow = TRUE), data.frame(x=c(1:33))) 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) diff --git a/tests/testthat/test-leafgl-addGlPoints.R b/tests/testthat/test-leafgl-addGlPoints.R index 8fdbdc3..46cef1a 100644 --- a/tests/testthat/test-leafgl-addGlPoints.R +++ b/tests/testthat/test-leafgl-addGlPoints.R @@ -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) @@ -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) diff --git a/tests/testthat/test-leafgl-colors.R b/tests/testthat/test-leafgl-colors.R index 2ecf245..e1368f8 100644 --- a/tests/testthat/test-leafgl-colors.R +++ b/tests/testthat/test-leafgl-colors.R @@ -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") @@ -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") @@ -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") @@ -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]])) @@ -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]])) @@ -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]])) @@ -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]])) diff --git a/tests/testthat/test-leafgl-popup.R b/tests/testthat/test-leafgl-popup.R index 9d84782..9a9e77d 100644 --- a/tests/testthat/test-leafgl-popup.R +++ b/tests/testthat/test-leafgl-popup.R @@ -46,20 +46,20 @@ test_that("popup-points-character", { rm(m) ## Single Random Character ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup = "Text 1", - group = "grp")) + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) expect_identical(from_json(m$x$calls[[2]]$args[[3]]), rep("Text 1", nrow(breweries91))) rm(m) ## Multiple Random Characters - (wrong length) ############## - m <- expect_warning(leaflet() %>% addTiles() %>% - addGlPoints(data = breweries91, - popup = c("Text 1", "Text 2"), - group = "grp")) + m <- leaflet() %>% addTiles() %>% + addGlPoints(data = breweries91, + popup = c("Text 1", "Text 2"), + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) expect_identical(from_json(m$x$calls[[2]]$args[[3]]), @@ -93,10 +93,10 @@ test_that("popup-points-table", { expect_is(m, "leaflet") ## Data.frame - wrong length ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup = as.data.frame(breweries91)[1:4,], - group = "grp")) + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) @@ -161,21 +161,21 @@ test_that("popup-points-formula", { test_that("popup-points-list", { ## List with length 1 ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup = as.list(data.frame(city="Berlin", district=5029, stringsAsFactors = F)), - group = "grp")) + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) ## List with length >1 ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup=as.list(data.frame(city=c("Vienna","Berlin"), district=c(1010,40302), stringsAsFactors = F)), - group = "grp")) + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) @@ -214,10 +214,10 @@ test_that("popup-points-json", { expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup = jsonify::to_json(as.data.frame(breweries91[1,])), - group = "grp")) + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) @@ -228,7 +228,6 @@ test_that("popup-points-json", { group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) - }) test_that("popup-points-logical", { @@ -269,17 +268,17 @@ test_that("popup-points-logical", { test_that("popup-points-shiny.tag", { library(shiny) ## Shiny.Tag - icon - Length 1 ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup = shiny::icon("car"), - group = "grp")) + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup = shiny::icon("car"), - group = "grp", src = TRUE)) + group = "grp", src = TRUE) expect_is(m, "leaflet") ## Shiny.Tag - icon - Length >1 ############## @@ -292,15 +291,14 @@ test_that("popup-points-shiny.tag", { # group = "grp")) # expect_is(m, "leaflet") # expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) - }) test_that("popup-points-default", { ## POSIX* ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup = Sys.time(), - group = "grp")) + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) @@ -314,10 +312,10 @@ test_that("popup-points-default", { expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) ## Date ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPoints(data = breweries91, popup = Sys.Date(), - group = "grp")) + group = "grp") expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) }) @@ -358,20 +356,20 @@ test_that("popup-lines-character", { rm(m) ## Single Random Character ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPolylines(data = storms, popup = "Text 1", - opacity = 1)) + opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) expect_identical(from_json(m$x$calls[[2]]$args[[3]]), rep("Text 1", nrow(storms))) rm(m) ## Multiple Random Characters - (wrong length) ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPolylines(data = storms, popup = c("Text 1", "Text 2"), - opacity = 1)) + opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) expect_identical(from_json(m$x$calls[[2]]$args[[3]]), @@ -398,10 +396,10 @@ test_that("popup-lines-table", { expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) ## Data.frame - wrong length ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPolylines(data = storms, popup = as.data.frame(storms)[1:4,], - opacity = 1)) + opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) @@ -412,7 +410,6 @@ test_that("popup-lines-table", { opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) - }) test_that("popup-lines-spatial", { @@ -512,20 +509,20 @@ test_that("popup-polygon-character", { rm(m) ## Single Random Character ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPolygons(data = gadm, popup = "Text 1", - opacity = 1)) + opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) expect_identical(from_json(m$x$calls[[2]]$args[[3]]), rep("Text 1", nrow(gadm))) rm(m) ## Multiple Random Characters - (wrong length) ############## - m <- expect_warning(leaflet() %>% addTiles() %>% + m <- leaflet() %>% addTiles() %>% addGlPolygons(data = gadm, popup = c("Text 1", "Text 2"), - opacity = 1)) + opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) expect_identical(from_json(m$x$calls[[2]]$args[[3]]), @@ -546,23 +543,23 @@ test_that("popup-polygon-table", { ## Data.frame ############## m <- leaflet() %>% addTiles() %>% addGlPolygons(data = gadm, - popup = as.data.frame(gadm), + popup = as.data.frame(gadm)[1:6], opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) ## Data.frame - wrong length ############## - m <- expect_warning(leaflet() %>% addTiles() %>% - addGlPolygons(data = gadm, - popup = as.data.frame(gadm)[1:4,], - opacity = 1)) + m <- leaflet() %>% addTiles() %>% + addGlPolygons(data = gadm, + popup = as.data.frame(gadm)[1, 1:4], + opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]])) ## Matrix ############## m <- leaflet() %>% addTiles() %>% addGlPolygons(data = gadm, - popup = as.matrix(as.data.frame(gadm)), + popup = as.matrix(as.data.frame(gadm)[1:6]), opacity = 1) expect_is(m, "leaflet") expect_true(jsonify::validate_json(m$x$calls[[2]]$args[[3]]))