这是indexloc提供的服务,不要输入任何密码
Skip to content

move some messages to cli #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions R/check_fix.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@
sf::st_make_valid(sfc)
}, error = function(e) {
suppressMessages(sf::sf_use_s2(s2))
warning("Unable to make ", shp_name, " valid. Invalid geometries are left out", call. = FALSE)
cli::cli_warn(c(
"Unable to make {.code {shp_name}} valid.",
i = "Invalid geometries are left out"
))

Check warning on line 26 in R/check_fix.R

View check run for this annotation

Codecov / codecov/patch

R/check_fix.R#L23-L26

Added lines #L23 - L26 were not covered by tests
isv = which(sf::st_is_valid(sfc))
sfc = sfc[isv]
attr(sfc, "is_valid") = isv
sfc
})
if (messages) message("Shape ", shp_name, " has been fixed with s2 = ", !s2, ". If the map doesn't look correct, please run sf::sf_use_s2(", !s2, ") before running the tmap code again.")
if (messages) {
cli::cli_inform(c(
"Shape {shp_name} has been fixed with {.code s2 = {!s2}.",
"If the map doesn't look correct, please run {.code sf::sf_use_s2({!s2})} before running the tmap code again."
))
}

Check warning on line 37 in R/check_fix.R

View check run for this annotation

Codecov / codecov/patch

R/check_fix.R#L32-L37

Added lines #L32 - L37 were not covered by tests

}
}
Expand Down
10 changes: 5 additions & 5 deletions R/messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
if (!message_thrown(mess)) {
if (fullname) {
cli::cli_inform(
"{.field [cols4all]} color palettes: use palettes from the R package cols4all. Run {.code cols4all::c4a_gui()} to explore them. The old palette name {.str {old_palette_name}} is named {.str {new1}}",
"{.field [cols4all]} color palettes: use palettes from the R package cols4all. Run {.run cols4all::c4a_gui()} to explore them. The old palette name {.str {old_palette_name}} is named {.val {new1}}",

Check warning on line 49 in R/messages.R

View check run for this annotation

Codecov / codecov/patch

R/messages.R#L49

Added line #L49 was not covered by tests
.frequency_id = "cols4all"
)
} else {
cli::cli_inform(
"{.field [cols4all]} color palettes: use palettes from the R package cols4all. Run {.code cols4all::c4a_gui()} to explore them. The old palette name {.str {old_palette_name}} is named {.str {new2}} (in long format {.str {new1}})",
"{.field [cols4all]} color palettes: use palettes from the R package cols4all. Run {.run cols4all::c4a_gui()} to explore them. The old palette name {.str {old_palette_name}} is named {.str {new2}} (in long format {.str {new1}})",

Check warning on line 54 in R/messages.R

View check run for this annotation

Codecov / codecov/patch

R/messages.R#L54

Added line #L54 was not covered by tests
.frequency_id = "cols4all"
)

Expand Down Expand Up @@ -82,9 +82,9 @@

message_pos_auto = function(type) {
if (!message_thrown("pos_auto")) {
fun = if (type == "autoout") "tm_pos_auto_out()" else "tm_pos_auto_in()"
fun2 = if (type == "autoout") "tm_pos_out()" else "tm_pos_in()"
cli::cli_inform("{.field [position]} use {.val {fun2}} instead of {.val {fun}}. The latter should be used with {.fn tmap_options}.")
fun = if (type == "autoout") "tm_pos_auto_out" else "tm_pos_auto_in"
fun2 = if (type == "autoout") "tm_pos_out" else "tm_pos_in"
cli::cli_inform("{.field [position]} use {.fn {fun2}} instead of {.fn {fun}}. The latter should be used with {.fn tmap_options}.")

Check warning on line 87 in R/messages.R

View check run for this annotation

Codecov / codecov/patch

R/messages.R#L85-L87

Added lines #L85 - L87 were not covered by tests
message_reg("pos_auto")
}
NULL
Expand Down
18 changes: 11 additions & 7 deletions R/misc_other.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
} else if (!is.null(x$proj4def)) {
sf::st_crs(x$proj4def)
} else {
stop("Unable to extract crs from leafletCRS object")
cli::cli_abort("Unable to extract crs from leafletCRS object.")

Check warning on line 178 in R/misc_other.R

View check run for this annotation

Codecov / codecov/patch

R/misc_other.R#L178

Added line #L178 was not covered by tests
}
}

Expand Down Expand Up @@ -226,7 +226,7 @@

nonempty_text = function(txt) {
if (is.character(txt)) {
txt!=""
nzchar(txt)
} else rep(TRUE, length(txt))
}

Expand Down Expand Up @@ -261,7 +261,7 @@
isnum = is_num_string(just)

if (!all(isnum | (just %in% c("left", "right", "top", "bottom", "center", "centre"))) && show.warnings) {
warning("wrong specification of argument just", call. = FALSE)
cli::cli_warn("{.arg just} is not correctly specified. ")

Check warning on line 264 in R/misc_other.R

View check run for this annotation

Codecov / codecov/patch

R/misc_other.R#L264

Added line #L264 was not covered by tests
}

just[just == "centre"] = "center"
Expand All @@ -277,7 +277,7 @@
if (show.messages) message("In interactive mode, just cannot be a numeric value. Therefore, ", justnum, " has been cenverted to \"", just, "\".")
}
} else {
if (n > 2 && show.warnings) warning("The just argument should be a single value or a vector of 2 values.", call. = FALSE)
if (n > 2 && show.warnings) cli::cli_warn("{.arg just} should be a single value or a vector of 2 values.")

Check warning on line 280 in R/misc_other.R

View check run for this annotation

Codecov / codecov/patch

R/misc_other.R#L280

Added line #L280 was not covered by tests
if (n == 1) {
if (just %in% c("top", "bottom")) {
just = c("center", just)
Expand All @@ -293,7 +293,9 @@
ifelse(just[1] == "right", 1,
ifelse(just[1] == "center", .5, NA))))
if (is.na(x)) {
if (show.warnings) warning("wrong specification of argument just", call. = FALSE)
if (show.warnings) {
cli::cli_warn("{.arg just} is not correctly specified. ")
}

Check warning on line 298 in R/misc_other.R

View check run for this annotation

Codecov / codecov/patch

R/misc_other.R#L296-L298

Added lines #L296 - L298 were not covered by tests
x = 0.5
}

Expand All @@ -302,7 +304,9 @@
ifelse(just[2] == "top", 1,
ifelse(just[2] == "center", .5, NA))))
if (is.na(y)) {
if (show.warnings) warning("wrong specification of argument just", call. = FALSE)
if (show.warnings) {
cli::cli_warn("{.arg just} is not correctly specified. ")
}

Check warning on line 309 in R/misc_other.R

View check run for this annotation

Codecov / codecov/patch

R/misc_other.R#L307-L309

Added lines #L307 - L309 were not covered by tests
y = 0.5
}
just = c(x, y)
Expand Down Expand Up @@ -444,7 +448,7 @@
z2 <- grDevices::xy.coords(xs2, ys2, recycle = TRUE)
xy2 <- toUserCoords(z2)

list(poly=polygonGrob(unit(xy2$x, "native"), grid::unit(xy2$y, "native"), id=id, gp=rg$gp))
list(poly = polygonGrob(unit(xy2$x, "native"), grid::unit(xy2$y, "native"), id = id, gp = rg$gp))

Check warning on line 451 in R/misc_other.R

View check run for this annotation

Codecov / codecov/patch

R/misc_other.R#L451

Added line #L451 was not covered by tests
#list(poly=rectGrob(unit(x, "native"), unit(y, "native"), width = unit(w, "native"), height=unit(h, "native"), gp = rg$gp))
}

Expand Down
4 changes: 2 additions & 2 deletions R/misc_stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@
stars::st_warp(x, crs = crs)
}
}, error = function(e) {
warning("Unable to warp stars. Stars will be transformed now (which will take some time).", call. = FALSE)
cli::cli_warn(c("!" = "Unable to warp stars. Stars will be transformed now (which will take some time)."))

Check warning on line 138 in R/misc_stars.R

View check run for this annotation

Codecov / codecov/patch

R/misc_stars.R#L138

Added line #L138 was not covered by tests
tryCatch({
sf::st_transform(x, crs = crs)
}, error = function(e) {
stop("Also unable to transform stars", call. = FALSE)
cli::cli_abort("Also unable to transform stars", call = NULL)

Check warning on line 142 in R/misc_stars.R

View check run for this annotation

Codecov / codecov/patch

R/misc_stars.R#L142

Added line #L142 was not covered by tests
})
})
} else {
Expand Down
21 changes: 13 additions & 8 deletions R/misc_symbols.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pchs = stats::setNames(c(seq(0L, 25L, 1L), seq(100L, 109L, 1L)),
c(c('open-rect', 'open-circle', 'open-triangle', 'simple-plus',
'simple-cross', 'open-diamond', 'open-down-triangle', 'cross-rect',
'simple-star', 'plus-diamond', 'plus-circle', 'hexagram', 'plus-rect',
'cross-circle', 'triangle-rect', 'solid-rect', 'solid-circle-md',
'solid-triangle', 'solid-diamond', 'solid-circle-bg', 'solid-circle-sm', 'circle',
c(c('open-rect', 'open-circle', 'open-triangle', 'simple-plus',
'simple-cross', 'open-diamond', 'open-down-triangle', 'cross-rect',
'simple-star', 'plus-diamond', 'plus-circle', 'hexagram', 'plus-rect',
'cross-circle', 'triangle-rect', 'solid-rect', 'solid-circle-md',
'solid-triangle', 'solid-diamond', 'solid-circle-bg', 'solid-circle-sm', 'circle',
'rect', 'diamond', 'triangle', 'down-triangle'
),
c('rect', 'circle', 'triangle', 'plus', 'cross', 'diamond', 'star', 'stadium', 'line', 'polygon')
Expand All @@ -12,16 +12,21 @@

get_pch_names = function(x) {
if (is.numeric(x)) {
if (!(all(x %in% pchs | x > 999))) stop("Unknown symbol values", call. = FALSE)
if (!(all(x %in% pchs | x > 999))) {
cli::cli_abort("Unknown symbol values")

Check warning on line 16 in R/misc_symbols.R

View check run for this annotation

Codecov / codecov/patch

R/misc_symbols.R#L16

Added line #L16 was not covered by tests
}
y = names(pchs)[match(x, pchs)]
y[x > 999] = x[x>999]
y
} else {
if (!all(x %in% names(pchs))) stop("Unknown symbol values", call. = FALSE)
if (!all(x %in% names(pchs))) {
unknown <- unique(x[!x %in% names(pchs)])
cli::cli_abort("Unknown symbol values: {.val {unknown}}")
}

Check warning on line 25 in R/misc_symbols.R

View check run for this annotation

Codecov / codecov/patch

R/misc_symbols.R#L22-L25

Added lines #L22 - L25 were not covered by tests
x
}
}
#
#
# get_pch_number = function(x) {
# if (is.numeric(x)) {
# if (!(all(x %in% pchs | x > 999))) stop("Unknown symbol values", call. = FALSE)
Expand Down
16 changes: 10 additions & 6 deletions R/process_breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,13 @@
}

q <- tryCatch({
suppressWarnings(do.call(classInt::classIntervals, c(list(x, n, style= style, intervalClosure=interval.closure), args)))
suppressWarnings(do.call(classInt::classIntervals, c(list(x, n, style = style, intervalClosure = interval.closure), args)))

Check warning on line 215 in R/process_breaks.R

View check run for this annotation

Codecov / codecov/patch

R/process_breaks.R#L215

Added line #L215 was not covered by tests
}, error = function(e) {
stop("Calculating interval classes failed for the variable ", var, " with style = '", style, "'. The error message from classInt::classIntervals: ", e$message, call. = FALSE)
cli::cli_abort(c(
"Calculating interval classes failed for the variable {.var {var}} with {.code style = {.val {style}}}."
),
parent = e
)

Check warning on line 221 in R/process_breaks.R

View check run for this annotation

Codecov / codecov/patch

R/process_breaks.R#L217-L221

Added lines #L217 - L221 were not covered by tests
})


Expand All @@ -224,18 +228,18 @@
}

if (approx && style != "fixed") {
if (n >= length(unique(x)) && style=="equal") {
if (n >= length(unique(x)) && style == "equal") {

Check warning on line 231 in R/process_breaks.R

View check run for this annotation

Codecov / codecov/patch

R/process_breaks.R#L231

Added line #L231 was not covered by tests
# to prevent classIntervals to set style to "unique"
q <- list(var = x, brks = seq(min(x, na.rm=TRUE), max(x, na.rm=TRUE), length.out=n))
q <- list(var = x, brks = seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = n))

Check warning on line 233 in R/process_breaks.R

View check run for this annotation

Codecov / codecov/patch

R/process_breaks.R#L233

Added line #L233 was not covered by tests
attr(q, "intervalClosure") <- interval.closure
class(q) <- "classIntervals"
} else {
brks <- q$brks

# to prevent ugly rounded breaks such as -.5, .5, ..., 100.5 for n=101
qm1 <- suppressWarnings(do.call(classInt::classIntervals, c(list(x, n-1, style= style, intervalClosure=interval.closure), args)))
qm1 <- suppressWarnings(do.call(classInt::classIntervals, c(list(x, n - 1, style = style, intervalClosure = interval.closure), args)))

Check warning on line 240 in R/process_breaks.R

View check run for this annotation

Codecov / codecov/patch

R/process_breaks.R#L240

Added line #L240 was not covered by tests
brksm1 <- qm1$brks
qp1 <- suppressWarnings(do.call(classInt::classIntervals, c(list(x, n+1, style= style, intervalClosure=interval.closure), args)))
qp1 <- suppressWarnings(do.call(classInt::classIntervals, c(list(x, n + 1, style = style, intervalClosure = interval.closure), args)))

Check warning on line 242 in R/process_breaks.R

View check run for this annotation

Codecov / codecov/patch

R/process_breaks.R#L242

Added line #L242 was not covered by tests
brksp1 <- qp1$brks
if (min(brksm1) > min(brks) && max(brksm1) < max(brks)) {
q <- qm1
Expand Down
6 changes: 3 additions & 3 deletions R/step1_helper_facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
split_stars_dim = get_split_stars_dim(mapping.aes)

if (length(hover) > 1) {
stop("hover should have length <= 1", call. = FALSE)
cli::cli_abort("hover should have length <= 1, not {length(hover)}.", call = NULL)

Check warning on line 254 in R/step1_helper_facets.R

View check run for this annotation

Codecov / codecov/patch

R/step1_helper_facets.R#L254

Added line #L254 was not covered by tests
}

if (is.na(hover)) {
Expand Down Expand Up @@ -389,7 +389,7 @@


if (nrd > 3L) {
if (nrsd > 3L) stop("The shape object has more than 3 dimensions, so even tm_facets_grid cannot be used.", call. = FALSE)
if (nrsd > 3L) cli::cli_abort("The shape object has more than 3 dimensions, so even {.fn tm_facets_grid} cannot be used.")

Check warning on line 392 in R/step1_helper_facets.R

View check run for this annotation

Codecov / codecov/patch

R/step1_helper_facets.R#L392

Added line #L392 was not covered by tests
nrvd = 0L
nrd = 3L
limitvars = TRUE
Expand Down Expand Up @@ -443,7 +443,7 @@
for (v in convert2density) {
sunit = tmg$tms$unit
if (is.null(sunit)) sunit = o$unit
shape.unit <- ifelse(sunit=="metric", "km", ifelse(sunit=="imperial", "mi", sunit))
shape.unit <- ifelse(sunit == "metric", "km", ifelse(sunit == "imperial", "mi", sunit))

Check warning on line 446 in R/step1_helper_facets.R

View check run for this annotation

Codecov / codecov/patch

R/step1_helper_facets.R#L446

Added line #L446 was not covered by tests
u = paste(shape.unit, shape.unit)
if (is.numeric(shp[[v]])) shp[[v]] = shp[[v]] / units::set_units(shp$AREA, u, mode = "standard")
}
Expand Down
16 changes: 8 additions & 8 deletions R/step2_helper_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -419,23 +419,23 @@
} else if (islistof(aes$scale, "tm_scale")) {
scale = rep(aes$scale, length.out = nvars)
} else {
stop("incorrect scale specification")
cli::cli_abort("incorrect scale specification")

Check warning on line 422 in R/step2_helper_data.R

View check run for this annotation

Codecov / codecov/patch

R/step2_helper_data.R#L422

Added line #L422 was not covered by tests
}

if (inherits(aes$legend, "tm_legend")) {
legend = rep(list(aes$legend), length.out = nvars)
} else if (islistof(aes$legend, "tm_legend")) {
legend = rep(aes$legend, length.out = nvars)
} else {
stop("incorrect legend specification")
cli::cli_abort("incorrect legend specification")

Check warning on line 430 in R/step2_helper_data.R

View check run for this annotation

Codecov / codecov/patch

R/step2_helper_data.R#L430

Added line #L430 was not covered by tests
}

if (inherits(aes$chart, "tm_chart")) {
crt = rep(list(aes$chart), length.out = nvars)
} else if (islistof(aes$chart, "tm_chart")) {
crt = rep(aes$chart, length.out = nvars)
} else {
stop("incorrect chart specification")
cli::cli_abort("incorrect chart specification")

Check warning on line 438 in R/step2_helper_data.R

View check run for this annotation

Codecov / codecov/patch

R/step2_helper_data.R#L438

Added line #L438 was not covered by tests
}


Expand Down Expand Up @@ -470,27 +470,27 @@
} else if (islistof(aes$scale, "tm_scale")) {
s = aes$scale[[1]]
} else {
stop("incorrect scale specification")
cli::cli_abort("incorrect scale specification")

Check warning on line 473 in R/step2_helper_data.R

View check run for this annotation

Codecov / codecov/patch

R/step2_helper_data.R#L473

Added line #L473 was not covered by tests
}

if (length(s) == 0) stop("mapping not implemented for aesthetic ", unm, call. = FALSE)

if (inherits(aes$legend, "tm_legend")) {
l = aes$legend
} else if (islistof(aes$legend, "tm_legend")) {
warning("multiple legends are specified, while only one is required; the first will be used")
cli::cli_warn("multiple legends are specified, while only one is required; the first will be used")

Check warning on line 481 in R/step2_helper_data.R

View check run for this annotation

Codecov / codecov/patch

R/step2_helper_data.R#L481

Added line #L481 was not covered by tests
l = aes$legend[[1]]
} else {
stop("incorrect legend specification")
cli::cli_abort("incorrect legend specification")

Check warning on line 484 in R/step2_helper_data.R

View check run for this annotation

Codecov / codecov/patch

R/step2_helper_data.R#L484

Added line #L484 was not covered by tests
}

if (inherits(aes$chart, "tm_chart")) {
crt = aes$chart
} else if (islistof(aes$chart, "tm_chart")) {
warning("multiple charts are specified, while only one is required; the first will be used")
cli::cli_warn("multiple charts are specified, while only one is required; the first will be used")

Check warning on line 490 in R/step2_helper_data.R

View check run for this annotation

Codecov / codecov/patch

R/step2_helper_data.R#L490

Added line #L490 was not covered by tests
crt = aes$chart[[1]]
} else {
stop("incorrect chart specification")
cli::cli_abort("incorrect chart specification")

Check warning on line 493 in R/step2_helper_data.R

View check run for this annotation

Codecov / codecov/patch

R/step2_helper_data.R#L493

Added line #L493 was not covered by tests
}

dtl = apply_scale(s, l, crt, val, unm, nm__ord, "legnr", "crtnr", sortRev, bypass_ord)
Expand Down
2 changes: 1 addition & 1 deletion R/step4_helper_legends.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
k = length(l$vvalues)
l$clones = lapply(clns, function(cl) {
vv = cl$vvalues
if (k != length(vv)) stop("legends could not be shared; the number of legend items is different", call. = FALSE)
if (k != length(vv)) cli::cli_abort("legends could not be shared; the number of legend items is different")

Check warning on line 86 in R/step4_helper_legends.R

View check run for this annotation

Codecov / codecov/patch

R/step4_helper_legends.R#L86

Added line #L86 was not covered by tests
vv
})
names(l$clones) = names(clones[w])
Expand Down
6 changes: 3 additions & 3 deletions R/tmapScale_defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
isnum = is.numeric(x)
if (isnum) {
structure(FALSE,
info = {if (is_var) "Variable should be data varible name or color name" else " Values should be numeric (between -50 and 50)."}
info = {if (is_var) "Variable should be data variable name or color name" else " Values should be numeric (between -50 and 50)."}

Check warning on line 7 in R/tmapScale_defaults.R

View check run for this annotation

Codecov / codecov/patch

R/tmapScale_defaults.R#L7

Added line #L7 was not covered by tests
)
} else {
is_c4a = !is.null(getPalMeta(x[1])) && length(x) == 1L && !valid_colors(x[1])
if (is_c4a) {
if (is_var) {
structure(FALSE,
info = " Variable should be a data variable name or a single color (not a color palette).")
info = "Variable should be a data variable name or a single color (not a color palette).")

Check warning on line 14 in R/tmapScale_defaults.R

View check run for this annotation

Codecov / codecov/patch

R/tmapScale_defaults.R#L14

Added line #L14 was not covered by tests
} else {
TRUE
}
} else {
all_cols = all(valid_colors(x))
if (!all_cols) {
structure(FALSE,
info = if (is_var) " Variable should a data variable name or a single color." else " Values should be color names or a color palette (run cols4all::c4a_palettes() for available ones.")
info = if (is_var) "Variable should a data variable name or a single color." else "Values should be color names or a color palette (run {.run cols4all::c4a_palettes()} for available ones.")
} else {
TRUE
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/terra-stars.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
Condition
Error in `tm_raster()`:
! Visual values used for the variable "col" are incorrect.
i Variable should a data variable name or a single color.
i Variable should a data variable name or a single color.