Skip to content

Commit

Permalink
improved v3 messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Apr 30, 2024
1 parent 6162aa3 commit b545397
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 36 deletions.
19 changes: 14 additions & 5 deletions R/messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@ message_reg = function(id) {
}

message_comp_scale = function() {
if (!message_thrown("comp_scale")) message("[plot mode] fit legend/component: Some legend items or map compoments do not fit well, and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable this.")
message_reg("comp_scale")
if (!message_thrown("comp_scale")) {
message("[plot mode] fit legend/component: Some legend items or map compoments do not fit well, and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable rescaling.")
message_reg("comp_scale")
}
NULL
}

message_comp_high_wide = function(stack) {
if (!message_thrown("comp_scale")) message("[plot mode] legend/component: Some components or legends are too ", ifelse(stack == "vertical", "high", "wide"), " and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable this.")
message_reg("comp_scale")
if (!message_thrown("comp_scale")) {
message("[plot mode] legend/component: Some components or legends are too ", ifelse(stack == "vertical", "high", "wide"), " and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable rescaling.")
message_reg("comp_scale")
}
NULL
}

message_c4a = function(old_palette_name, info) {
new1 = info$fullname
new2 = info$name
message(paste0("[cols4all] color palettes: use palettes from the R package cols4all. Run 'cols4all::c4a_gui()' to explore them. The old palette name \"", old_palette_name, "\" is named \"", new2, "\" (in long format \"", new1, "\")"))
mess = paste0("c4a_", old_palette_name)

if (!message_thrown(mess)) {
message(paste0("[cols4all] color palettes: use palettes from the R package cols4all. Run 'cols4all::c4a_gui()' to explore them. The old palette name \"", old_palette_name, "\" is named \"", new2, "\" (in long format \"", new1, "\")"))
message_reg(mess)
}
}
85 changes: 67 additions & 18 deletions R/messages_v4_v3.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,46 @@ v3_tm_scale_instead_of_style = function(style, scale_fun, vv, layer_fun, arg_lis
arg_list$new = c("style", arg_list$new)
}
if (length(arg_list$old)) {
x = if (arg_list$mult) {
paste0(". For small multiples, specify a 'tm_scale_' for each multiple, and put them in a list: '", vv, ".scale = list(<scale1>, <scale2>, ...)'")
} else {
""
}
al = do.call(paste, c(mapply(function(x,y) {
if (x == y) paste0("'", x, "'") else paste0("'", x, "' (rename to '", y, "')")
}, arg_list$old, arg_list$new, SIMPLIFY = FALSE), sep = ", "))
xtra = paste0(" and migrate the argument(s) ", al, " to 'tm_scale_", scale_fun, "(<HERE>)'")
} else {
xtra = ""
x = ""
}

message(paste0("[v3->v4] ", layer_fun, "(): instead of 'style = \"", style, "\"', use '", vv, ".scale = tm_scale_", scale_fun, "()'", xtra))
message(paste0("[v3->v4] ", layer_fun, "(): instead of 'style = \"", style, "\"', use '", vv, ".scale = tm_scale_", scale_fun, "()'", xtra, x))
}

v3_tm_scale = function(scale_fun, vv, layer_fun, arg_list) {
scale_fun = if (scale_fun == "") {
"tm_scale"
} else {
paste0("tm_scale_", scale_fun)
}
if (length(arg_list$old)) {
x = if (arg_list$mult) {
paste0(". For small multiples, specify a 'tm_scale_' for each multiple, and put them in a list: '", vv, ".scale = list(<scale1>, <scale2>, ...)'")
} else {
""
}

al = v3_list_text(olds = arg_list$old, news = arg_list$new)
message("[v3->v4] ", layer_fun, "(): migrate the argument(s) related to the scale of the visual variable '", vv, "', namely ", al, " to '", vv, ".scale = ", scale_fun, "(<HERE>)'", x)
}
NULL
}





v3_instead_message = function(arg_old, arg_new, fun) {
v3_start_message()
message(paste0("[v3->v4] ", fun, "(): use '", arg_new, "' instead of '", arg_old, "'"))
Expand Down Expand Up @@ -89,22 +118,32 @@ v3_instead_value = function(args_called, old, new, fun, value_old, value_new) {
}

v3_list_init = function() {
.TMAP$v3_list = list(old = character(), new = character())
#.TMAP$v3_list = paste0(.TMAP$v3_list, v3_list_impute_item(name, value)
.TMAP$v3_list = list(old = character(), new = character(), mult = FALSE)
invisible(NULL)
}
v3_list_impute_item = function(name, new_name) {
v3_list_impute_item = function(name, new_name, mult = FALSE) {
.TMAP$v3_list$old = c(.TMAP$v3_list$old, name)
.TMAP$v3_list$new = c(.TMAP$v3_list$new, new_name)
.TMAP$v3_list$mult = .TMAP$v3_list$mult || mult
}
v3_list_get = function() {
.TMAP$v3_list
}

v3_impute = function(args, name, value, new_name = name) {
if (name %in% names(args)) {
v3_list_impute_item(name, new_name)
args[[name]]
res = args[[name]]
if (is.list(res) && length(grep("format", name, fixed =TRUE)) == 0) {
res = res[[1]]
mult = TRUE
} else if (new_name %in% c("title", "na.show", "orientation", "reverse", "interval.closure", "drop.levels", "midpoint", "as.count", "values.repeat", "values.scale", "value.na", "value.null", "value.neutral", "label.na", "label.null")) {
res = res[1]
mult = TRUE
} else {
mult = FALSE
}
v3_list_impute_item(name, new_name, mult)
res
} else value
}

Expand All @@ -128,6 +167,19 @@ v3_tm_legend = function(fun, vv, arg_list) {
NULL
}

v3_tm_facets = function(arg_list) {
if (length(arg_list$old)) {
al = v3_list_text(olds = arg_list$old, news = arg_list$new)
message(paste0("[v3->v4] tm_facets(): rename the following argument(s): ", al))
}
NULL
}

v3_tm_facets_free_scales = function() {
message(paste0("[v3->v4] tm_facets(): migrate each 'free.scales.<X>' argument to the argument '<X>.free' of the corresponding layer function"))
}



v3_tm_legend_hide = function(fun, arg, vv) {
message("[v3->v4] ", fun, "(): use '", vv, ".legend = tm_legend_hide()' instead of '", arg, " = FALSE")
Expand All @@ -141,18 +193,7 @@ v3_tm_legend_general = function(fun) {
}


v3_tm_scale = function(scale_fun, vv, layer_fun, arg_list) {
scale_fun = if (scale_fun == "") {
"tm_scale"
} else {
paste0("tm_scale_", scale_fun)
}
if (length(arg_list$old)) {
al = v3_list_text(olds = arg_list$old, news = arg_list$new)
message(paste0("[v3->v4] ", layer_fun, "(): migrate the argument(s) related to the scale of the visual variable '", vv, "', namely ", al, " to '", vv, ".scale = ", scale_fun, "(<HERE>)'"))
}
NULL
}


v3_tm_chart_hist = function(layer_fun, vv, arg) {
message(paste0("[v3->v4] ", layer_fun, "(): use '", vv, ".chart = tm_chart_histogram()' instead of '", arg, " = TRUE'"))
Expand Down Expand Up @@ -191,3 +232,11 @@ v3_opt = function(olds, news, layer_fun) {
x = v3_list_text(olds, news)
message("[v3->v4] ", layer_fun, "(): migrate the layer options ", x, " to 'options = opt_", layer_fun, "(<HERE>)'")
}

# v3_multiple = function(layer_fun, vv) {
# if (!message_thrown("multiple_args")) {
# message("[v3->v4] ", layer_fun, "(): use '", vv, ".scale = list(<scale1>, <scale2>, ...)' to specify small multiples")
# message_reg("multiple_args")
# }
#
# }
2 changes: 1 addition & 1 deletion R/process_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ process_meta = function(o, d, cdt, aux) {
ifelse("right" %in% panel.xtab.pos, panel.label.height * lineW, 0))
} else c(0, 0, 0, 0)

panel.margin = get_option_class(panel.margin, panel.type)
panel.margin = get_option_class(panel.margin, panel.type, spatial_class = FALSE)

panel.xtab.margin = if (panel.type == "xtab") {
c(ifelse("bottom" %in% panel.xtab.pos, panel.margin * lineH, 0),
Expand Down
1 change: 1 addition & 0 deletions R/step1_helper_facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ step1_rearrange_facets = function(tmo, o) {
if (length(byvars)) add_used_vars(byvars)
}
if (!all(bys %in% c("VARS__", smeta$vars, smeta$dims))) stop("unknown facet variables", call. = FALSE)
if (is.na(na.text)) na.text = o$label.na
})

smeta$vars = get("used_vars", envir = .TMAP)
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
31 changes: 26 additions & 5 deletions R/tm_facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#' @param type `"grid"`, `"wrap"` or `"stack"`
#' @param along deprecated Please use `tm_facets_page()`
#' @param free.scales deprecated. Please use the `.free` arguments in the layer functions, e.g. `fill.free` in `tm_polygons`.
#' @param ... used to catch deprecated arguments
#' @export
#' @rdname tm_facets
#' @name tm_facets
Expand All @@ -68,15 +69,36 @@ tm_facets = function(by = NULL,
drop.empty.facets = TRUE,
drop.NA.facets = FALSE,
sync = TRUE,
showNA = NA,
textNA = "Mssing",
na.text = NA,
scale.factor=2,
type = NA, # grid, wrap or stack
along = NULL,
free.scales = NULL) {
free.scales = NULL,
...) {

args = list(...)
calls <- names(match.call(expand.dots = TRUE)[-1])

if (any(v3_only("tm_facets") %in% names(args))) {
layer_fun = "facets"
v3_start_message()

v3_list_init()
drop.empty.facets = v3_impute(args, "showNA", NA, paste0("drop.empty.facets = ", !args$showNA))
na.text = v3_impute(args, "textNA", NA, "na.text")
ncols = v3_impute(args, "ncol", NA, "ncols")
nrows = v3_impute(args, "nrow", NA, "nrows")
drop.units = v3_impute(args, "drop.shapes", NA, "drop.units")
v3_tm_facets(arg_list = v3_list_get())

if (any(substr(names(args), 1, 12) == "free.scales.")) {
v3_tm_facets_free_scales()
}

}



if (!is.null(along)) {
warning("The 'along' argument of 'tm_facets()' is deprecated as of tmap 4.0. Please use 'pages' instead.", call. = FALSE)
pages = along
Expand Down Expand Up @@ -110,8 +132,7 @@ tm_facets = function(by = NULL,
drop.empty.facets = drop.empty.facets,
drop.NA.facets = drop.NA.facets,
sync = sync,
showNA = showNA,
textNA = textNA,
na.text = na.text,
scale.factor = scale.factor,
calls = calls,
subclass = "tm_facets"))
Expand Down
4 changes: 4 additions & 0 deletions R/tm_layers_lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ tm_lines = function(col = tm_const(),
}

v3_list_init()
if (length(style) > 1) {
style = style[1]
.TMAP$v3_list$mult = TRUE
}
# v3 visual variable: col
col.scale.args = list(n = v3_impute(args, "n", 5),
style = style,
Expand Down
14 changes: 11 additions & 3 deletions R/tm_layers_polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ tm_polygons = function(fill = tm_const(),
}})

v3_start_message()


if (!("style" %in% names(args))) {
if (!"breaks" %in% names(args)) {
style = "pretty"
Expand All @@ -146,7 +148,12 @@ tm_polygons = function(fill = tm_const(),
style = args$style
}


v3_list_init()
if (length(style) > 1) {
style = style[1]
.TMAP$v3_list$mult = TRUE
}

fill.scale.args = list(n = v3_impute(args, "n", 5),
style = style,
Expand All @@ -168,7 +175,8 @@ tm_polygons = function(fill = tm_const(),
label.null = NA,
label.format = v3_impute(args, "legend.format", list(), "label.format"))

fill.scale.args$fun_pref = if (style == "cat") {

fill.scale.args$fun_pref = if (style[1] == "cat") {
"categorical"
} else if (style %in% c("fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust", "bclust", "fisher", "jenks", "dpih", "headtails", "log10_pretty")) {
"intervals"
Expand All @@ -183,7 +191,7 @@ tm_polygons = function(fill = tm_const(),
}

if ("style" %in% names(args)) {
v3_tm_scale_instead_of_style(style, scale_fun = fill.scale.args$fun_pref, vv = "fill", layer_fun = layer_fun, arg_list = v3_list_get())
v3_tm_scale_instead_of_style(style[1], scale_fun = fill.scale.args$fun_pref, vv = "fill", layer_fun = layer_fun, arg_list = v3_list_get())
} else {
v3_tm_scale(scale_fun = "", vv = "fill", layer_fun = layer_fun, arg_list = v3_list_get())
}
Expand All @@ -197,7 +205,7 @@ tm_polygons = function(fill = tm_const(),
v3_convert2density(layer_fun)
}

if ("col" %in% names(args_called)) {
if ("col" %in% names(args_called) && (args$called_from != "fill")) {
fill = col
col = tm_const()
v3_message_col_fill(layer_fun = layer_fun)
Expand Down
4 changes: 4 additions & 0 deletions R/tm_layers_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ tm_raster = function(col = tm_shape_vars(),
}

v3_list_init()
if (length(style) > 1) {
style = style[1]
.TMAP$v3_list$mult = TRUE
}
# v3 visual variable: col
col.scale.args = list(n = v3_impute(args, "n", 5),
style = style,
Expand Down
5 changes: 5 additions & 0 deletions R/tm_layers_symbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ v3_symbols = function(args, args_called) {
}

v3_list_init()
if (length(style) > 1) {
style = style[1]
.TMAP$v3_list$mult = TRUE
}

# v3 visual variable: fill
fill.scale.args = list(n = v3_impute(args, "n", 5),
style = style,
Expand Down
4 changes: 4 additions & 0 deletions R/tm_layers_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ tm_text = function(text = tm_const(),
}

v3_list_init()
if (length(style) > 1) {
style = style[1]
.TMAP$v3_list$mult = TRUE
}
col.scale.args = list(n = v3_impute(args, "n", 5),
style = style,
style.args = v3_impute(args, "style.args", list()),
Expand Down
6 changes: 3 additions & 3 deletions R/tmapGetShapeMeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ get_fact_levels_na = function(x, o) {

if (!o$drop.NA.facets && anyna) {
showNA = TRUE
levs = c(levs, o$label.na)
levs = c(levs, o$na.text)
} else if (!o$drop.NA.facets && any(is.na(levs))) {
showNA = TRUE
levs[is.na(levs)] = o$label.na
levs[is.na(levs)] = o$na.text
} else if (o$drop.NA.facets && any(is.na(levs))) {
showNA = FALSE
levs = levs[!is.na(levs)]
Expand All @@ -214,7 +214,7 @@ get_fact_levels_na = function(x, o) {
levs = as.character(sort(u))
if (!o$drop.NA.facets && any(is.na(u))) {
showNA = TRUE
levs = c(levs, o$label.na)
levs = c(levs, o$na.text)
} else {
showNA = FALSE
}
Expand Down
2 changes: 1 addition & 1 deletion R/tmap_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ tmapMode = function(id, name, ...) {
meta.margins = NA,
meta.auto.margins = c(0.4, 0.4, 0.4, 0.4),
between.margin = 0.5,
panel.margin = c(xtab = 0.4, wrap = 0),
panel.margin = c(xtab = 0.4, 0),
component.offset = c(inside = 0.75, INSIDE = 0, outside = 0, OUTSIDE = 0),
component.stack.margin = 0,
grid.mark.height = 2,
Expand Down

0 comments on commit b545397

Please sign in to comment.