Skip to content

Commit

Permalink
working on messaging, to do tm_lines v3-lwd, tm_raster
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Apr 26, 2024
1 parent 203bc2c commit 5e40dd7
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 73 deletions.
31 changes: 31 additions & 0 deletions R/messages.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
message_init = function() {
.TMAP$messages = character()
invisible(NULL)
}

message_thrown = function(id) {
id %in% .TMAP$messages
}

message_reg = function(id) {
.TMAP$messages = c(.TMAP$messages, id)
NULL
}

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")
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")
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, "\")"))
}
16 changes: 13 additions & 3 deletions R/v4_v3.R → R/messages_v4_v3.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,14 @@ 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 = tm_scale_", scale_fun, "(<HERE>)'"))
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
}
Expand All @@ -156,8 +161,8 @@ v3_message_fill_alpha = function(layer_fun = layer_fun) {
message(paste0("[v3->v4] ", layer_fun, "(): use 'fill_alpha' instead of 'alpha''"))
}

v3_message_col_alpha = function(layer_fun = layer_fun) {
message(paste0("[v3->v4] ", layer_fun, "(): use 'col_alpha' instead of 'border.alpha''"))
v3_message_col_alpha = function(layer_fun = layer_fun, orig = "border.alpha") {
message(paste0("[v3->v4] ", layer_fun, "(): use 'col_alpha' instead of '", orig, "''"))
}

v3_add_legend = function(type, args) {
Expand All @@ -170,3 +175,8 @@ v3_add_legend = function(type, args) {
message(paste0("[v3->v4] tm_add_legend(): use 'col' instead of 'border.col' for the outline color of ", newtype[type]))
}
}

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>)'")
}
1 change: 1 addition & 0 deletions R/step1_rearrange.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
step1_rearrange = function(tmel) {
message_init()
dev = getOption("tmap.devel.mode")

# get options (mode specific)
Expand Down
84 changes: 44 additions & 40 deletions R/tm_layers_lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,14 @@ tm_lines = function(col = tm_const(),
args = list(...)
args_called = as.list(match.call()[-1]) #lapply(as.list(match.call()[-1]), eval, envir = parent.frame())

v3 = c("alpha", "scale", "lwd.legend.labels", "lwd.legend.col", "n",
"style", "style.args", "as.count", "breaks", "interval.closure",
"palette", "labels", "drop.levels", "midpoint", "stretch.palette",
"contrast", "colorNA", "textNA", "showNA", "colorNULL", "title.col",
"title.lwd", "legend.col.show", "legend.lwd.show", "legend.format",
"legend.col.is.portrait", "legend.lwd.is.portrait", "legend.col.reverse",
"legend.lwd.reverse", "legend.hist", "legend.hist.title", "legend.col.z",
"legend.lwd.z", "legend.hist.z", "id", "interactive", "popup.vars",
"popup.format", "auto.palette.mapping", "max.categories")

if (any(v3 %in% names(args))) {
message("tm_lines: Deprecated tmap v3 code detected. Code translated to v4")
if (any(v3_only("tm_lines") %in% names(args))) {
layer_fun = paste0("tm_", {if ("called_from" %in% names(args)) {
args$called_from
} else {
"lines"
}})

v3_start_message()
if (!("style" %in% names(args))) {
if (!"breaks" %in% names(args)) {
style = "pretty"
Expand All @@ -113,33 +109,32 @@ tm_lines = function(col = tm_const(),
style = args$style
}

imp = function(name, value) {
if (name %in% names(args)) args[[name]] else value
}

v3_list_init()
# v3 visual variable: col
col.scale.args = list(n = imp("n", 5),
col.scale.args = list(n = v3_impute(args, "n", 5),
style = style,
style.args = imp("style.args", list()),
breaks = imp("breaks", NULL),
interval.closure = imp("interval.closure", "left"),
drop.levels = imp("drop.levels", FALSE),
midpoint = imp("midpoint", NULL),
as.count = imp("as.count", NA),
values = imp("palette", NA),
values.repeat = !imp("stretch.palette", TRUE),
values.range = imp("contrast", NA),
style.args = v3_impute(args, "style.args", list()),
breaks = v3_impute(args, "breaks", NULL),
interval.closure = v3_impute(args, "interval.closure", "left"),
drop.levels = v3_impute(args, "drop.levels", FALSE),
midpoint = v3_impute(args, "midpoint", NULL),
as.count = v3_impute(args, "as.count", NA),
values = v3_impute(args, "palette", NA, "values"),
values.repeat = !v3_impute(args, "stretch.palette", TRUE, "values.repeat"),
values.range = v3_impute(args, "contrast", NA, "values.range"),
values.scale = 1,
value.na = imp("colorNA", NA),
value.null = imp("colorNULL", NA),
value.na = v3_impute(args, "colorNA", NA, "value.na"),
value.null = v3_impute(args, "colorNULL", NA, "value.null"),
value.neutral = NA,
labels = imp("labels", NULL),
label.na = imp("textNA", NA),
labels = v3_impute(args, "labels", NULL),
label.na = v3_impute(args, "textNA", NA, "label.na"),
label.null = NA,
label.format = imp("legend.format", list()))
label.format = v3_impute(args, "legend.format", list(), "label.format"))
col.scale.args$fun_pref = if (style == "cat") {
"categorical"
} else if (style %in% c("fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust", "bclust", "fisher", "jenks", "dpih", "headtails", "log10_pretty")) {
} else if (style %in% c("fixed", "sd", "equal", "pretty", "quantile",
"kmeans", "hclust", "bclust", "fisher", "jenks",
"dpih", "headtails", "log10_pretty")) {
"intervals"
} else if (style == "cont") {
"continuous"
Expand All @@ -150,22 +145,31 @@ tm_lines = function(col = tm_const(),
} else {
stop("unknown style")
}

if ("style" %in% names(args)) {
v3_tm_scale_instead_of_style(style, scale_fun = col.scale.args$fun_pref, vv = "col", layer_fun = layer_fun, arg_list = v3_list_get())
} else {
v3_tm_scale(scale_fun = "", vv = "col", layer_fun = layer_fun, arg_list = v3_list_get())
}
col.scale = do.call("tm_scale", args = col.scale.args)



if ("alpha" %in% names(args)) {
col_alpha = args$alpha
v3_message_col_alpha(layer_fun = layer_fun, orig = "alpha")

}

col.legend.args = alist(title = imp("title.col", NA),
show = imp("legend.col.show", NULL),
na.show = imp("showNA", NA),
format = imp("legend.format", list()),
orientation = ifelse(imp("legend.col.is.portrait", TRUE), "portrait", "landscape"),
reverse = imp("legend.col.reverse", FALSE))

v3_list_init()
col.legend.args = list(title = v3_impute(args, "title.col", NA, "title"),
show = v3_impute(args, "legend.col.show", NULL, "show"),
na.show = v3_impute(args, "na.show", NA),
format = v3_impute(args, "legend.format", list(), "format"),
orientation = ifelse(v3_impute(args, "legend.col.is.portrait", TRUE, "orientation"), "portrait", "landscape"),
reverse = v3_impute(args, "legend.reverse", FALSE, "reverse"))
col.legend = do.call("tm_legend", col.legend.args)

v3_tm_legend(fun = layer_fun, vv = "col", arg_list = v3_list_get())

# v3 visual variable: lwd
lwd.scale.args = list(values = imp("lwd.legend", NA),
Expand Down
28 changes: 19 additions & 9 deletions R/tm_layers_polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ tm_polygons = function(fill = tm_const(),

if (any(v3_only("tm_polygons") %in% names(args))) {

layer_fun = paste0("tm", {if ("called_from" %in% names(args)) {
layer_fun = paste0("tm_", {if ("called_from" %in% names(args)) {
args$called_from
} else {
"polygons"
Expand All @@ -146,7 +146,8 @@ tm_polygons = function(fill = tm_const(),
style = args$style
}


v3_list_init()

fill.scale.args = list(n = v3_impute(args, "n", 5),
style = style,
style.args = v3_impute(args, "style.args", list()),
Expand Down Expand Up @@ -181,12 +182,19 @@ tm_polygons = function(fill = tm_const(),
stop("unknown style")
}

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())
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())
} else {
v3_tm_scale(scale_fun = "", vv = "fill", layer_fun = layer_fun, arg_list = v3_list_get())
}



fill.scale = do.call("tm_scale", args = fill.scale.args)

if ("convert2density" %in% names(args) && args$convert2density) {
fill.scale$convert2density = TRUE
v3_convert2density(layer_fun)
}

if ("col" %in% names(args_called)) {
Expand All @@ -204,14 +212,16 @@ tm_polygons = function(fill = tm_const(),
fill_alpha = args$alpha
}

fill.legend.args = list(title = v3_impute(args, "title", NA),
show = v3_impute(args, "legend.show", NULL),
na.show = v3_impute(args, "na.show", NA),
format = v3_impute(args, "legend.format", list()),
orientation = ifelse(v3_impute(args, "legend.is.portrait", TRUE), "portrait", "landscape"),
reverse = v3_impute(args, "legend.reverse", FALSE))

v3_list_init()
fill.legend.args = list(title = v3_impute(args, "title", NA),
show = v3_impute(args, "legend.show", NULL, "show"),
na.show = v3_impute(args, "showNA", NA, "na.show"),
format = v3_impute(args, "legend.format", list(), "format"),
orientation = ifelse(v3_impute(args, "legend.is.portrait", TRUE, "orientation"), "portrait", "landscape"),
reverse = v3_impute(args, "legend.reverse", FALSE, "reverse"))
fill.legend = do.call("tm_legend", fill.legend.args)
v3_tm_legend(fun = layer_fun, vv = "fill", arg_list = v3_list_get())

if ("legend.hist" %in% names(args) && args$legend.hist) {
fill.chart = tm_chart_histogram()
Expand Down
25 changes: 18 additions & 7 deletions R/tm_layers_symbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ tm_symbols = function(size = tm_const(),
}

v3_symbols = function(args, args_called) {
v3 = v3_only("tm_symbols")

if (any(v3 %in% names(args))) {
if (any(v3_only("tm_symbols") %in% names(args))) {
layer_fun = paste0("tm_", {if ("called_from" %in% names(args)) {
args$called_from
} else {
Expand Down Expand Up @@ -325,8 +323,11 @@ v3_symbols = function(args, args_called) {
} else {
stop("unknown style")
}

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())
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())
} else {
v3_tm_scale(scale_fun = "", vv = "fill", layer_fun = layer_fun, arg_list = v3_list_get())
}
fill.scale = do.call("tm_scale", args = fill.scale.args)

if ("col" %in% names(args_called)) {
Expand Down Expand Up @@ -372,12 +373,12 @@ v3_symbols = function(args, args_called) {
v3_list_init()
fill.legend.args = list(title = v3_impute(args, "title.col", NA, "title"),
show = v3_impute(args, "legend.show", NULL, "show"),
na.show = v3_impute(args, "na.show", NA),
na.show = v3_impute(args, "showNA", NA, "na.show"),
format = v3_impute(args, "legend.format", list(), "format"),
orientation = ifelse(v3_impute(args, "legend.is.portrait", TRUE, "orientation"), "portrait", "landscape"),
reverse = v3_impute(args, "legend.reverse", FALSE, "reverse"))
fill.legend = do.call("tm_legend", fill.legend.args)
v3_tm_legend(fun = layer_fun, vv = "col", arg_list = v3_list_get())
v3_tm_legend(fun = layer_fun, vv = "fill", arg_list = v3_list_get())


# v3 visual variable: size
Expand Down Expand Up @@ -458,6 +459,16 @@ v3_symbols = function(args, args_called) {
fill.chart = tm_chart_none()
}


v4_opt_args = c("icon.scale", "just", "grob.dim")
v3_opt_args = c("icon.scale", "just", "grob.dim")
osel = which(v3_opt_args %in% names(args))
if (length(osel)) {
o3 = v3_opt_args[osel]
o4 = v4_opt_args[osel]
v3_opt(o3, o4, layer_fun)
}

list(fill = fill,
col = col,
col_alpha = col_alpha,
Expand Down
28 changes: 18 additions & 10 deletions R/tm_layers_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,18 @@ tm_text = function(text = tm_const(),
stop("unknown style")
}

if ("style" %in% names(args)) v3_tm_scale_instead_of_style(style, scale_fun = col.scale.args$fun_pref, vv = "col", layer_fun = "tm_text", arg_list = v3_list_get())
if ("style" %in% names(args)) {
v3_tm_scale_instead_of_style(style, scale_fun = col.scale.args$fun_pref, vv = "col", layer_fun = "tm_text", arg_list = v3_list_get())
} else {
v3_tm_scale(scale_fun = "", vv = "col", layer_fun = "tm_text", arg_list = v3_list_get())
}

col.scale = do.call("tm_scale", args = col.scale.args)

v3_list_init()
col.legend.args = list(title = v3_impute(args, "title.col", NA, "title"),
show = v3_impute(args, "legend.col.show", NULL, "show"),
na.show = v3_impute(args, "na.show", NA),
na.show = v3_impute(args, "showNA", NA, "na.show"),
format = v3_impute(args, "legend.format", list(), "format"),
orientation = ifelse(v3_impute(args, "legend.col.is.portrait", TRUE, "orientation"), "portrait", "landscape"),
reverse = v3_impute(args, "legend.col.reverse", FALSE, "reverse"))
Expand All @@ -309,21 +313,21 @@ tm_text = function(text = tm_const(),
}

v3_list_init()
size.scale.args = list(values = tmap_seq(0, 1, power = 1/v3_impute(args, "root", 3, "values = tmap_seq(0, 1, power = 1/<root>)")),
size.scale.args = list(values = tmap_seq(v3_impute(args, "size.lowerbound", 0, "values = tmap_seq(<size.lowerbound>, 1)"), 1, power = 1/v3_impute(args, "root", 3, "values = tmap_seq(<size.lowerbound>, 1, power = 1/<root>)")),
values.scale = v3_impute(args, "scale", 1, "values.scale"),
limits = v3_impute(args, "size.lim", NULL),
outliers.trunc = c(v3_impute(args, "print.tiny", FALSE), TRUE),
ticks = v3_impute(args, "breaks", NULL),
limits = v3_impute(args, "size.lim", NULL, "limits"),
outliers.trunc = c(v3_impute(args, "print.tiny", FALSE, paste0("outliers.trunc = c(", {if ("print.tiny" %in% names(args)) !args$print.tiny else TRUE}, ", TRUE)") ), TRUE),
ticks = v3_impute(args, "breaks", NULL, "ticks"),
midpoint = v3_impute(args, "midpoint", NULL),
labels = v3_impute(args, "sizes.legend.labels", NULL))
labels = v3_impute(args, "sizes.legend.labels", NULL, "labels"))
v3_tm_scale(scale_fun = "continuous", vv = "size", layer_fun = "tm_text", arg_list = v3_list_get())
size.scale = do.call("tm_scale_continuous", size.scale.args)


v3_list_init()
size.legend.args = list(title = v3_impute(args, "title.size", NA, "title"),
show = v3_impute(args, "legend.size.show", NULL, "show"),
na.show = v3_impute(args, "na.show", NA),
na.show = v3_impute(args, "showNA", NA, "na.show"),
format = v3_impute(args, "legend.format", list(), "format"),
orientation = ifelse(v3_impute(args, "legend.size.is.portrait", TRUE, "orientation"), "portrait", "landscape"),
reverse = v3_impute(args, "legend.size.reverse", FALSE, "reverse"))
Expand All @@ -334,8 +338,12 @@ tm_text = function(text = tm_const(),

v4_opt_args = c("along.lines", "shadow", "just", "clustering", "point.label", "remove.overlap")
v3_opt_args = c("along.lines", "shadow", "just", "clustering", "auto.placement", "remove.overlap")

#if ()
osel = which(v3_opt_args %in% names(args))
if (length(osel)) {
o3 = v3_opt_args[osel]
o4 = v4_opt_args[osel]
v3_opt(o3, o4, "tm_text")
}


#}
Expand Down
5 changes: 3 additions & 2 deletions R/tmapGridLegend.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ tmapGridCompCorner = function(comp, o, stack, pos.h, pos.v, maxH, maxW, offsetIn
# because of legend frames (for which margins are added in tmapGridLegend), the scale may be a bit above 1, even though automatic layout is applied and there is enough space

if (o$component.autoscale) {
if (any(scaleW > 1.1) || any(scaleH > 1.1)) message("Some legend items or map compoments do not fit well (e.g. due to the specified font size). They are rescaled Set the tmap option component.autoscale to FALSE to disable this.", call. = FALSE)
if (any(scaleW > 1.1) || any(scaleH > 1.1)) message_comp_scale()
} else {
scaleW[scaleW > 1.1] = 1
scaleH[scaleH > 1.1] = 1
Expand Down Expand Up @@ -87,7 +87,8 @@ tmapGridCompCorner = function(comp, o, stack, pos.h, pos.v, maxH, maxW, offsetIn
if (o$component.autoscale) {
if (scaleS > 1.01) {
if (scaleS > 1.05) {
message("(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_comp_high_wide(stack)

}
legWin = legWin / scaleS
legHin = legHin / scaleS
Expand Down
Loading

0 comments on commit 5e40dd7

Please sign in to comment.