Skip to content

Commit

Permalink
v3 messaging sort-of done
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Apr 30, 2024
1 parent b545397 commit 0aff40b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 52 deletions.
68 changes: 39 additions & 29 deletions R/messages_v4_v3.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,53 @@ v3_convert2density = function(layer_fun) {
}

v3_tm_scale_instead_of_style = function(style, scale_fun, vv, layer_fun, arg_list) {
if (scale_fun == "intervals") {
arg_list$old = c("style", arg_list$old)
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>, ...)'")
m = paste0("scale_", scale_fun, "_vv_", vv, "style_", style)
if (!message_thrown(m)) {
if (scale_fun == "intervals") {
arg_list$old = c("style", arg_list$old)
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 = ""
}
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, x))
message_reg(m)
}

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

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>, ...)'")
m = paste0("scale_", scale_fun, "_vv_", vv)

if (!message_thrown(m)) {
scale_fun = if (scale_fun == "") {
"tm_scale"
} else {
""
paste0("tm_scale_", scale_fun)
}

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)
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)
}
message_reg(m)
}
NULL
}
Expand Down
6 changes: 5 additions & 1 deletion R/qtm.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ qtm <- function(shp,
if (!"shape" %in% called) args_f$shape = NULL
if (!"col" %in% called) args_f$col = NULL
}
if (f == "tm_lines") {
if (!"col" %in% called) args_f$col = NULL
}

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
Expand All @@ -162,7 +166,7 @@ qtm <- function(shp,
args_f$fill = NULL
}
}

args_f$called_from = "qtm"
options = opt_tm_sf()[[c(tm_polygons = "polygons", tm_lines = "lines", tm_symbols = "points")[f]]]
g = g + do.call(f, c(args_f, args_other, list(options = options)))
}
Expand Down
15 changes: 9 additions & 6 deletions R/tm_layers_lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ tm_lines = function(col = tm_const(),
args_called = as.list(match.call()[-1]) #lapply(as.list(match.call()[-1]), eval, envir = parent.frame())

if (any(v3_only("tm_lines") %in% names(args))) {
layer_fun = paste0("tm_", {if ("called_from" %in% names(args)) {
layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"lines"
}})
}

v3_start_message()
if (!("style" %in% names(args))) {
Expand Down Expand Up @@ -149,11 +149,14 @@ 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())
if (!(args$called_from == "qtm" && (!"col" %in% names(args)))) {
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)


Expand Down
14 changes: 6 additions & 8 deletions R/tm_layers_polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ 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 = if ("called_from" %in% names(args)) {
args$called_from
} else {
"polygons"
}})
}

v3_start_message()

Expand Down Expand Up @@ -196,16 +196,14 @@ tm_polygons = function(fill = tm_const(),
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) && (args$called_from != "fill")) {
if ("col" %in% names(args_called) && (!args$called_from %in% c("tm_fill", "qtm"))) {
fill = col
col = tm_const()
v3_message_col_fill(layer_fun = layer_fun)
Expand All @@ -217,7 +215,7 @@ tm_polygons = function(fill = tm_const(),
col = args$border.col
if (!("col" %in% names(args_called))) v3_message_col_fill(layer_fun = layer_fun)
}
if (identical(args$called_from, "borders")) {
if (identical(args$called_from, "tm_borders")) {
fill = NA
}

Expand Down Expand Up @@ -332,7 +330,7 @@ tm_fill = function(...) {
if (!("col" %in% names(args))) {
args$col = NA
}
args$called_from = "fill"
args$called_from = "tm_fill"
do.call(tm_polygons, args)
}

Expand All @@ -344,6 +342,6 @@ tm_borders = function(col = tm_const(), ...) {
if (!("fill" %in% names(args))) {
args$fill = NA
}
args$called_from = "borders"
args$called_from = "tm_borders"
do.call(tm_polygons, c(list(col = col), args))
}
6 changes: 3 additions & 3 deletions R/tm_layers_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ tm_raster = function(col = tm_shape_vars(),


if (any(v3_only("tm_raster") %in% names(args))) {
layer_fun = paste0("tm_", {if ("called_from" %in% names(args)) {
layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"raster"
}})
"tm_raster"
}

v3_start_message()
if (!("style" %in% names(args))) {
Expand Down
11 changes: 6 additions & 5 deletions R/tm_layers_symbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ tm_symbols = function(size = tm_const(),

v3_symbols = function(args, args_called) {
if (any(v3_only("tm_symbols") %in% names(args))) {
layer_fun = paste0("tm_", {if ("called_from" %in% names(args)) {
layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"symbols"
}})
}

v3_start_message()
if (!("style" %in% names(args))) {
Expand Down Expand Up @@ -328,6 +328,7 @@ 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())
} else {
Expand Down Expand Up @@ -536,7 +537,7 @@ tm_dots = function(fill = tm_const(),
...) {

args = c(as.list(environment()), list(...))
args$called_from = "dots"
args$called_from = "tm_dots"

tm = do.call(tm_symbols, args)
tm[[1]]$layer = c("dots", "symbols")
Expand Down Expand Up @@ -585,7 +586,7 @@ tm_bubbles = function(size = tm_const(),
...) {

args = c(as.list(environment()), list(...))
args$called_from = "bubbles"
args$called_from = "tm_bubbles"

tm = do.call(tm_symbols, args)
tm[[1]]$layer = c("bubbles", "symbols")
Expand Down Expand Up @@ -632,7 +633,7 @@ tm_squares = function(size = tm_const(),
...) {

args = c(as.list(environment()), list(...))
args$called_from = "squares"
args$called_from = "tm_squares"
tm = do.call(tm_symbols, args)
tm[[1]]$layer = c("squares", "symbols")
tm
Expand Down

0 comments on commit 0aff40b

Please sign in to comment.