Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Dec 17, 2024
1 parent d55bf22 commit f90db40
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 26 deletions.
6 changes: 6 additions & 0 deletions R/messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ message_qtm_empty = function() {
"{.field [qtm]} nothing to show. Either specify {.arg shp} or {.arg basemap}. Alternatively, switch to view mode with {.code tmap_mode(\"view\")}"
)
}

message_layer_unused_args = function(layer_fun, args) {
cli::cli_inform(c(
"{.field [{layer_fun}]} Argument{ifelse(length(args)>1, 's', '')} {.arg {args}} unknown."
))
}
18 changes: 13 additions & 5 deletions R/tm_layers_lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ tm_lines = function(col = tm_const(),
args_called = names(rlang::call_match()[-1])
args = list(...)

layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"tm_lines"
}

if (any(v3_only("tm_lines") %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 @@ -215,6 +216,13 @@ tm_lines = function(col = tm_const(),
}
}

# unused arguments: typos?
unused = setdiff(names(args), v3_only("tm_lines"))

if (length(unused)) {
message_layer_unused_args(layer_fun, unused)
}



tm_element_list(tm_element(
Expand Down
19 changes: 14 additions & 5 deletions R/tm_layers_polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ tm_polygons = function(fill = tm_const(),
args_called = names(rlang::call_match()[-1])
args = list(...)

layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"tm_polygons"
}

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

layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"tm_polygons"
}


v3_start_message()

Expand Down Expand Up @@ -257,6 +259,13 @@ tm_polygons = function(fill = tm_const(),

}

# unused arguments: typos?
unused = setdiff(names(args), v3_only("tm_polygons"))

if (length(unused)) {
message_layer_unused_args(layer_fun, unused)
}


tm_element_list(tm_element(
layer = "polygons",
Expand Down
20 changes: 15 additions & 5 deletions R/tm_layers_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ tm_raster = function(col = tm_vars(),
args_called = names(rlang::call_match()[-1])
args = list(...)

layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"tm_raster"
}

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


v3_start_message()
if (!("style" %in% names(args))) {
Expand Down Expand Up @@ -164,6 +165,15 @@ tm_raster = function(col = tm_vars(),
}
}

# unused arguments: typos?
unused = setdiff(names(args), v3_only("tm_raster"))

if (length(unused)) {
message_layer_unused_args(layer_fun, unused)
}



# needed for color maps without categories (then tm_scale_categorical is used without legend, unless called)
col.legend$called = "col.legend" %in% args_called

Expand Down
20 changes: 15 additions & 5 deletions R/tm_layers_rgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ tm_rgb = function(col = tm_vars(n = 3, multivariate = TRUE),
...) {
args = list(...)

layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"tm_rgb"
}

if (any(v3_only("tm_rgb") %in% names(args)) || is.numeric(col.scale)) {
v3_start_message()

# second condition needed to catch tm_rgb(1, 2, 3)
layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"tm_rgb"
}


if (all(c("r", "g", "b") %in% names(args))) {
v3_tm_rgb(args$r, args$g, args$b)
Expand All @@ -44,6 +46,14 @@ tm_rgb = function(col = tm_vars(n = 3, multivariate = TRUE),

}

# unused arguments: typos?
unused = setdiff(names(args), v3_only("tm_rgb"))

if (length(unused)) {
message_layer_unused_args(layer_fun, unused)
}



do.call(tm_raster, args = list(col = col, col.scale = col.scale, col.legend = col.legend, col.chart = col.chart, col.free = col.free, options = options))
}
Expand Down
17 changes: 17 additions & 0 deletions R/tm_layers_symbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ tm_symbols = function(size = tm_const(),
fill.chart = res$fill.chart
}

# unused arguments: typos?
unused = setdiff(names(args), v3_only("tm_symbols"))

if (length(unused)) {
layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"tm_symbols"
}

message_layer_unused_args(layer_fun, unused)
}





tm_element_list(tm_element(
layer = "symbols",
trans.fun = tmapTransCentroid,
Expand Down
24 changes: 18 additions & 6 deletions R/tm_layers_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ tm_text = function(text = tm_const(),
#if (FALSE) {
args = list(...)

layer_fun = if ("called_from" %in% names(args)) {
args$called_from
} else {
"tm_text"
}

if (any(v3_only("tm_text") %in% names(args))) {
v3_start_message()
Expand Down Expand Up @@ -178,9 +183,9 @@ tm_text = function(text = tm_const(),
}

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())
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 = "tm_text", arg_list = v3_list_get())
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 All @@ -193,16 +198,16 @@ tm_text = function(text = tm_const(),
orientation = ifelse(v3_impute(args, "legend.col.is.portrait", TRUE, "orientation"), "portrait", "landscape"),
reverse = v3_impute(args, "legend.col.reverse", FALSE, "reverse"))

v3_tm_legend(fun = "tm_text", vv = "col", arg_list = v3_list_get())
v3_tm_legend(fun = layer_fun, vv = "col", arg_list = v3_list_get())
col.legend = do.call("tm_legend", col.legend.args)

v3_list_init()
text.scale = tm_scale_asis(value.neutral = v3_impute(args, "sizes.legend.text", NA, "value.neutral"))
v3_tm_scale(scale_fun = "asis", vv = "text", layer_fun = "tm_text", arg_list = v3_list_get())
v3_tm_scale(scale_fun = "asis", vv = "text", layer_fun = layer_fun, arg_list = v3_list_get())

if ("legend.hist" %in% names(args) && args$legend.hist) {
col.chart = tm_chart_histogram()
v3_tm_chart_hist(layer_fun = "tm_text", vv = "col", arg = "legend.hist")
v3_tm_chart_hist(layer_fun = layer_fun, vv = "col", arg = "legend.hist")
# to do: histogram title
}

Expand All @@ -214,7 +219,7 @@ tm_text = function(text = tm_const(),
ticks = v3_impute(args, "breaks", NULL, "ticks"),
midpoint = v3_impute(args, "midpoint", 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())
v3_tm_scale(scale_fun = "continuous", vv = "size", layer_fun = layer_fun, arg_list = v3_list_get())
size.scale = do.call("tm_scale_continuous", size.scale.args)


Expand Down Expand Up @@ -243,6 +248,13 @@ tm_text = function(text = tm_const(),
#}
}

# unused arguments: typos?
unused = setdiff(names(args), v3_only("tm_text"))

if (length(unused)) {
message_layer_unused_args(layer_fun, unused)
}

tm_element_list(tm_element(
layer = "text",
trans.fun = tmapTransCentroid,
Expand Down

0 comments on commit f90db40

Please sign in to comment.