diff --git a/R/misc_v3_comp.R b/R/misc_v3_comp.R new file mode 100644 index 00000000..ab032af4 --- /dev/null +++ b/R/misc_v3_comp.R @@ -0,0 +1,53 @@ +v3_only = function(fun) { + setdiff(funs_v3[[fun]], funs_v4[[fun]]) +} + +v3_start_message = function() { + if (!.TMAP$v3) { + message("tmap v3 code detected:") + .TMAPv3 = TRUE + } + invisible(NULL) +} + +v3_reset_flag = function() { + .TMAPv3 = FALSE + invisible(NULL) +} + + +v3_instead = function(args_called, old, new, fun, extra_called = character()) { + args = args_called$args + called = args_called$called + + if (old %in% called) { + args[[new]] = args[[old]] + args[[old]] = NULL + + # may be needed to trigger something (e.g. "shape" to use symbols instead of dots) + if (length(extra_called)) { + called = unique(c(called, extra_called)) + } + message(paste0(fun, ": (v3->v4), use '", new, "' instead of '", old, "'")) + } + list(args = args, called = called) +} + +v3_instead_value = function(args_called, old, new, fun, value_old, value_new) { + args = args_called$args + called = args_called$called + + if (old %in% called) { + if (identical(args[[old]], value_old)) { + args[[old]] = NULL + args[[new]] = value_new + if (is.null(value_old)) value_old = "NULL" + message(paste0(fun, ": (v3->v4), use '", new, " = ", value_new, "' instead of '", old, " = ", value_old, "'")) + list(args = args, called = called) + } else { + v3_instead(args_called, old, new, fun) + } + } else { + list(args = args, called = called) + } +} diff --git a/R/onLoad.R b/R/onLoad.R index 87880d41..c9c196fd 100644 --- a/R/onLoad.R +++ b/R/onLoad.R @@ -18,6 +18,10 @@ assign("last_map_new", NULL, envir = .TMAP) + # flag for old v3 code + assign("v3", FALSE, envir = .TMAP) + + assign("tmapStyles", .defaultTmapStyles, envir = .TMAP) assign("tmapFormats", .defaultTmapFormats, envir = .TMAP) diff --git a/R/print.R b/R/print.R index 9b090bda..ca2a1a96 100644 --- a/R/print.R +++ b/R/print.R @@ -21,6 +21,9 @@ print.tmap = function(x, return.asp = FALSE, show = TRUE, vp = NULL, knit = FALS res = step4_plot(x4, vp = vp, return.asp = return.asp, show = show, knit = knit, args) if (dev) timing_add("step 4") if (dev) timing_eval() + + v3_reset_flag() + #if (return.asp) return(asp) else invisible(NULL) if (knit && tmap_graphics_name() == "Leaflet") { kp = get("knit_print", asNamespace("knitr")) diff --git a/R/qtm.R b/R/qtm.R index 4f247410..84f2eccb 100644 --- a/R/qtm.R +++ b/R/qtm.R @@ -90,106 +90,47 @@ qtm <- function(shp, style=NULL, format=NULL, ...) { + args <- c(as.list(environment()), list(...)) shp_name <- deparse(substitute(shp))[1] called <- names(match.call(expand.dots = TRUE)[-1]) - vv_col = "col" - vv_text = c("text", "text_col", "text_size") - vv_oher = c("fill", "size", "shape", "lwd", "lty", "fill_alpha", "col_alpha") - - vv_attr = c(".scale", ".legend", ".free", ".chart") - - - v3 = c("symbols.size", "symbols.col", "symbols.shape", "dots.col", - "text.size", "text.col", "lines.lwd", "lines.col", "raster", - "borders", "projection") - - if (any(v3 %in% names(args))) { - mes = "tmap v3 code detected" - - if ("symbols.size" %in% names(args)) { - args$size = args$symbols.size - args$symbols.size = NULL - called = unique(c(called, "shape")) - mes = paste0(mes, "; use 'size' instead of 'symbols.size'") - } - if ("symbols.col" %in% names(args)) { - args$fill = args$symbols.col - args$symbols.col = NULL - called = unique(c(called, "shape")) - mes = paste0(mes, "; use 'fill' instead of 'symbols.col'") - } else if ("dots.col" %in% names(args)) { - args$fill = args$dots.col - args$dots.col = NULL - mes = paste0(mes, "; use 'fill' instead of 'dots.col'") - } - - if ("lines.lwd" %in% names(args)) { - args$lwd = args$lines.lwd - args$lines.lwd = NULL - mes = paste0(mes, "; use 'lwd' instead of 'lines.lwd'") - } - if ("lines.col" %in% names(args)) { - args$col = args$lines.col - args$lines.col = NULL - mes = paste0(mes, "; use 'col' instead of 'lines.col'") - } - if ("raster" %in% names(args)) { - args$col = args$raster - args$raster = NULL - mes = paste0(mes, "; use 'col' instead of 'raster'") - } - if ("borders" %in% names(args)) { - if (is.null(args$borders)) { - args$borders = NULL - args$col = NA - mes = paste0(mes, "; use 'col = NA' instead of 'borders = NULL'") - } else { - args$col = args$borders - args$borders = NULL - mes = paste0(mes, "; use 'col' instead of 'borders'") - } - } - - if ("text.size" %in% names(args)) { - args$text_size = args$text.size - args$text.size = NULL - mes = paste0(mes, "; use 'text_size' instead of 'text.size'") - } - if ("text.col" %in% names(args)) { - args$text_col = args$text.col - args$text.col = NULL - mes = paste0(mes, "; use 'text_col' instead of 'text.col'") - } - - message(mes) - + if (any(v3_only("qtm") %in% names(args))) { + v3_start_message() + args_called = list(args = args, called = called) |> + v3_instead("symbols.size", "size", "qtm", extra_called = "shape") |> + v3_instead("symbols.col", "size", "qtm", extra_called = "shape") |> + v3_instead("dots.col", "fill", "qtm") |> + v3_instead("lines.lwd", "lwd", "qtm") |> + v3_instead("lines.col", "col", "qtm") |> + v3_instead("raster", "col", "qtm") |> + v3_instead_value("borders", "col", "qtm", value_old = NULL, value_new = NA) |> + v3_instead("text.size", "text_size", "qtm") |> + v3_instead("text.col", "text_col", "qtm") |> + v3_instead("projection", "crs", "qtm") + args = args_called$args + called = args_called$called } tmapOptions <- tmap_options_mode() show.warnings = tmapOptions$show.warnings if (missing(shp) || is.character(shp)) { - viewargs <- args[intersect(names(args), names(formals(tm_view)))] - if (!missing(shp)) viewargs$bbox <- shp - g <- c(tm_basemap(basemaps), tm_tiles(overlays), do.call("tm_view", viewargs)) attr(g, "qtm_shortcut") <- TRUE class(g) <- "tmap" return(g) } - nms_shp = intersect(names(args), names(formals(tm_shape))) g = do.call(tm_shape, args[nms_shp]) is_rst = inherits(shp, c("stars", "SpatRaster")) if (is_rst) { - nms_rst = intersect(names(args), names(formals(tm_raster))) + nms_rst = intersect(names(args), funs_v4$tm_raster) args_rst = args[nms_rst] if (!any(c("col", "raster") %in% called)) { @@ -202,11 +143,8 @@ qtm <- function(shp, g = g + do.call(tm_raster, c(args_rst, args_rst_v3)) } else { - nms_all = intersect(names(args), names(formals(tm_sf))) - args_all = args[nms_all] - for (f in c("tm_polygons", "tm_lines", "tm_symbols")) { - nms_f = intersect(names(args), names(formals(f))) + nms_f = intersect(names(args), funs_v4[[f]]) args_f = args[nms_f] nms_other = intersect(setdiff(names(args), c(nms_f, nms_shp, "basemaps", "overlays", "style", "format")), called) args_other = args[nms_other] @@ -218,7 +156,9 @@ qtm <- function(shp, if (f == "tm_polygons") { if (length(args_other)) { # v3 confusion: tm_polygons used col while qtm used fill, therefore, tm_polygons will be called will col - args_f$col = args_f$fill + args_f$border.col = args_f$col + args_f$col = args$fill + args_f$fill = NULL } } @@ -230,7 +170,7 @@ qtm <- function(shp, text_ = substr(names(args), 1, 5) == "text_" names(args)[text_] = substr(names(args)[text_], 6, nchar(names(args)[text_])) - nms_f = intersect(names(args), names(formals(tm_text))) + nms_f = intersect(names(args), funs_v4$tm_text) args_f = args[nms_f] nms_other = intersect(setdiff(names(args), c(nms_f, nms_shp, "basemaps", "overlays", "style", "format")), called) args_other = args[nms_other] @@ -240,6 +180,18 @@ qtm <- function(shp, } + nms_fct = intersect(names(args), names(formals(tm_facets))) + if (length(nms_fct)) { + g = g + do.call(tm_facets, args[nms_fct]) + } + + if (!is.null(args$basemaps)) { + g = g + do.call(tm_basemap, list(server = args$basemaps)) + } + if (!is.null(args$overlays)) { + g = g + do.call(tm_tiles, list(server = args$overlays)) + } + assign("last_map_new", match.call(), envir = .TMAP) attr(g, "qtm_shortcut") <- FALSE diff --git a/R/step1_rearrange.R b/R/step1_rearrange.R index acbaae7d..fdc94db3 100644 --- a/R/step1_rearrange.R +++ b/R/step1_rearrange.R @@ -8,7 +8,6 @@ step1_rearrange = function(tmel) { assign("shapeLib", list(), envir = .TMAP) assign("justLib", list(), envir = .TMAP) - # find shape, (aesthetic) layer, facet, and other elements is_tms = vapply(tmel, inherits, "tm_shape", FUN.VALUE = logical(1)) is_tml = vapply(tmel, inherits, "tm_layer", FUN.VALUE = logical(1)) diff --git a/R/sysdata.rda b/R/sysdata.rda index befce25a..5839ef58 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/R/tm_layers_polygons.R b/R/tm_layers_polygons.R index 9475ea8b..12bc67b2 100644 --- a/R/tm_layers_polygons.R +++ b/R/tm_layers_polygons.R @@ -127,17 +127,8 @@ tm_polygons = function(fill = tm_const(), args = list(...) args_called = as.list(match.call()[-1]) #lapply(as.list(match.call()[-1]), eval, envir = parent.frame()) - v3 = c("alpha", "palette", "convert2density", "area", "n", - "style", "style.args", "as.count", "breaks", "interval.closure", - "labels", "drop.levels", "midpoint", "stretch.palette", "contrast", - "colorNA", "textNA", "showNA", "colorNULL", "thres.poly", "title", - "legend.show", "legend.format", "legend.is.portrait", "legend.reverse", - "legend.hist", "legend.hist.title", "legend.z", "legend.hist.z", - "interactive", "auto.palette.mapping", "max.categories") - - - if (any(v3 %in% names(args))) { - message("tm_polygons: Deprecated tmap v3 code detected. Code translated to v4") + if (any(v3_only("tm_polygons") %in% names(args))) { + v3_start_message() if (!("style" %in% names(args))) { if (!"breaks" %in% names(args)) { style = "pretty" diff --git a/data-raw/sysdata.R b/data-raw/sysdata.R new file mode 100644 index 00000000..21eafd15 --- /dev/null +++ b/data-raw/sysdata.R @@ -0,0 +1,41 @@ +## v3 functions and arguments +library(remotes) +install_github("r-tmap/tmap@v3") + +library(tmap) + +get_tmap_funs = function() { + nms = getNamespaceExports("tmap") + funs = lapply(nms, function(f) { + g = get(f) + if (is.function(g)) { + x = setdiff(names(do.call(formals, list(g))), "...") + if (is.null(x)) x = character() + x + } else { + NULL + } + }) + names(funs) = nms + funs[!sapply(funs, is.null)] +} + +funs_v3 = get_tmap_funs() + +funs_v3$tm_polygons = unique(c(funs_v3$tm_fill, funs_v3$tm_polygons, funs_v3$tm_borders)) + + +## v4 functions and arguments +devtools::load_all() + +funs_v4 = get_tmap_funs() +usethis::use_data(funs_v3, funs_v4, internal = TRUE, overwrite = TRUE) + + + + + + + + + diff --git a/data-raw/tmap.pal.info.R b/data-raw/tmap.pal.info.R deleted file mode 100644 index 14d4d816..00000000 --- a/data-raw/tmap.pal.info.R +++ /dev/null @@ -1,137 +0,0 @@ -library(pals) -library(rcartocolor) -library(grid) - -tmap_pal_name_compress = function(x) tolower(gsub("[-, \\,, (, ), \\ ]", "", x)) - - -hcl = lapply(c("qualitative", "sequential", "diverging", "divergingx"), hcl.pals) -# merge diverging and divergingx -hcl[[3]] = c(hcl[[3]], hcl[[4]]) -hcl[[4]] = NULL - -hcl_labels = unlist(hcl) -hcl_names = tmap_pal_name_compress(hcl_labels) -hcl_n = rep(Inf, length(hcl_labels)) -hcl_type = unlist(mapply(rep, c("cat", "seq", "div"), sapply(hcl, length), USE.NAMES = FALSE, SIMPLIFY = TRUE)) -hcl_series = rep("hcl", length(hcl_labels)) -hcl_package = rep("grDevices", length(hcl_labels)) -hcl_fun = rep("hcl.colors", length(hcl_labels)) -hcl_fun_type_arg = rep("palette", length(hcl_labels)) - -palette_hex = grDevices:::.palette_colors_hex -palette_labels = names(palette_hex) -palette_names = tmap_pal_name_compress(palette_labels) -palette_n = unname(sapply(palette_hex, length, USE.NAMES = FALSE)) -palette_type = rep("cat", length(palette_hex)) -palette_series = rep("palette", length(palette_hex)) -palette_package = rep("grDevices", length(palette_hex)) -palette_fun = rep("palette.colors", length(palette_hex)) -palette_fun_type_arg = rep("palette", length(palette_hex)) - -pals_syspals = pals:::syspals -pals_syspals = pals_syspals[names(pals_syspals) %in% ls(asNamespace("pals"))] - -pals_functions = ls(asNamespace("pals")) - -pals_names = names(pals_syspals) -pals_labels = pals_names -pals_n = unname(sapply(pals_names, function(b) { - length(pals_syspals[[b]]) -})) -pals_series = ifelse(substr(pals_names, 1, 6) == "brewer", "brewer", - ifelse(substr(pals_names, 1, 5) == "ocean", "ocean", - ifelse(substr(pals_names, 1, 6) == "kovesi", "kovesi", - ifelse(pals_names %in% c("magma", "inferno", "plasma", "viridis"), "viridis", "misc")))) -pals_type = ifelse(pals_names %in% c("alphabet", "alphabet2", "cols25", "glasbey", "kelly", "polychrome", "stepped", "stepped2", "stepped3", "okabe", "tableau20", "tol", "tol.groundcover", "watlington", - "brewer.set1", "brewer.set2", "brewer.set3", "brewer.pastel1", - "brewer.pastel2", "brewer.dark2", "brewer.paired"), "cat", - ifelse(pals_names %in% c("coolwarm", "cubehelix", "brewer.brbg", "brewer.piyg", "brewer.prgn", "brewer.puor", "brewer.rdbu", "brewer.rdgy", "brewer.rdylbu", "brewer.rdylgn", "brewer.spectral", "ocean.balance", "ocean.delta", "ocean.curl", pals_names[substr(pals_names, 1, 16) == "kovesi.diverging"]), "div", ifelse(substr(pals_names, 1, 13) == "kovesi.cyclic", "cyc", "seq"))) - -pals_labels[pals_series == "brewer"] = substr(pals_labels[pals_series == "brewer"], 8, nchar(pals_labels[pals_series == "brewer"])) -pals_labels[pals_series == "ocean"] = substr(pals_labels[pals_series == "ocean"], 7, nchar(pals_labels[pals_series == "ocean"])) -pals_labels[pals_series == "kovesi" & pals_type == "cyc"] = substr(pals_labels[pals_series == "kovesi" & pals_type == "cyc"], 15, nchar(pals_labels[pals_series == "kovesi" & pals_type == "cyc"])) -pals_labels[pals_series == "kovesi" & pals_type == "div"] = substr(pals_labels[pals_series == "kovesi" & pals_type == "div"], 18, nchar(pals_labels[pals_series == "kovesi" & pals_type == "div"])) -pals_labels[pals_series == "kovesi" & pals_type == "seq"] = substr(pals_labels[pals_series == "kovesi" & pals_type == "seq"], 8, nchar(pals_labels[pals_series == "kovesi" & pals_type == "seq"])) - - -pals_n[pals_type != "cat"] = Inf - - -pals_package = rep("pals", length(pals_names)) -pals_fun = pals_names -pals_fun_type_arg = rep("", length(pals_names)) - -biv_names = c( - "arc.bluepink", - "brewer.qualbin", - "brewer.divbin", - "brewer.divseq", - "brewer.qualseq", - "brewer.divdiv", - "brewer.seqseq1", - "brewer.seqseq2", - "census.blueyellow", - "tolochko.redblue", - "stevens.pinkgreen", - "stevens.bluered", - "stevens.pinkblue", - "stevens.greenblue", - "stevens.purplegold") -biv_n = c(16, 6, 6, rep(9, 12)) -biv_labels = biv_names -biv_series = rep("bivariate", length(biv_names)) -biv_package = rep("pals", length(biv_names)) -biv_fun = biv_names -biv_fun_type_arg = rep("", length(biv_names)) -biv_type = rep("biv", length(biv_names)) - - -carto_names = metacartocolors$Name -carto_labels = carto_names -carto_n = metacartocolors$Max_n -carto_series = rep("carto", length(carto_names)) -carto_package = rep("rcartocolor", length(carto_names)) -carto_fun = rep("carto_pal", length(carto_names)) -carto_fun_type_arg = rep("name", length(carto_names)) -carto_type = ifelse(metacartocolors$Type == "diverging", "div", ifelse(metacartocolors$Type == "qualitative", "cat", "seq")) - - -gn = function(x, name) get(paste(x, name, sep = "_")) - -.tmap_pals = do.call(rbind, lapply(c("hcl", "palette", "pals", "biv", "carto"), function(x) { - data.frame(name = gn(x, "names"), - label = gn(x, "labels"), - package = gn(x, "package"), - type = gn(x, "type"), - series = gn(x, "series"), - maxn = gn(x, "n"), - fun = gn(x, "fun"), - fun_type_args = gn(x, "fun_type_arg")) -})) - - -.tmap_pals$fullname = paste(.tmap_pals$series, .tmap_pals$name, sep = "__") - - -usethis::use_data(.tmap_pals, internal = TRUE, overwrite = TRUE) - - - -tmap_pals = local({ - fnm = split(.tmap_pals$label, list(.tmap_pals$package, .tmap_pals$series)) - nm = split(.tmap_pals$name, list(.tmap_pals$package, .tmap_pals$series)) - - fnm = fnm[vapply(fnm, FUN = function(x) length(x) > 0, FUN.VALUE = logical(1))] - nm = nm[vapply(nm, FUN = function(x) length(x) > 0, FUN.VALUE = logical(1))] - - mapply(function(fnmi, nmi) { - names(fnmi) = nmi - as.list(fnmi) - }, fnm, nm) - -}) - -usethis::use_data(tmap.pal.info, internal = FALSE) - -