Skip to content

Commit

Permalink
Add messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Nov 19, 2024
1 parent bb924cb commit 846b94c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# isopleuros 1.2.0.9000
## Enhancements
* Allow to center and scale label positions.
* Allow to center and scale label coordinates.
* Generate diagnostic messages if coordinates are not centered/scaled when they should be.

# isopleuros 1.2.0
## Enhancements
Expand Down
2 changes: 2 additions & 0 deletions R/coordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ setMethod(
n <- length(x)
assert_length(y, n)
assert_length(z, n)
assert_center(center)
assert_scale(scale)

## Missing values
if (missing) {
Expand Down
18 changes: 18 additions & 0 deletions R/isopleuros-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ assert_length <- function(x, expected) {
}
invisible(x)
}

assert_center <- function(x, current = getOption("isopleuros.center")) {
ok <- isTRUE(x) || is.numeric(x)
if (!ok && is.numeric(current) && !all(current == 1)) {
msg <- "The current plot has been centered, but your data doesn't seem to be."
message(msg)
}
invisible(x)
}

assert_scale <- function(x, current = getOption("isopleuros.scale")) {
ok <- isTRUE(x) || is.numeric(x)
if (!ok && is.numeric(current) && !all(current == 1)) {
msg <- "The current plot has been scaled, but your data doesn't seem to be."
message(msg)
}
invisible(x)
}
4 changes: 2 additions & 2 deletions R/ternary_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ ternary_grid <- function(primary = NULL, secondary = NULL,
for (i in x) {
start <- matrix(data = c(i, 0, 1 - i, 1 - i, i, 0, 0, 1 - i, i), ncol = 3)
end <- matrix(data = c(i, 1 - i, 0, 0, i, 1 - i, 1 - i, 0, i), ncol = 3)
start <- coordinates_ternary(start)
end <- coordinates_ternary(end)
start <- list(x = start[, 2] + start[, 3] / 2, y = start[, 3] * sqrt(3) / 2)
end <- list(x = end[, 2] + end[, 3] / 2, y = end[, 3] * sqrt(3) / 2)

mapply(
FUN = function(x_from, x_to, y_from, y_to, n, center, scale) {
Expand Down
4 changes: 4 additions & 0 deletions R/ternary_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ setMethod(
}
graphics::plot.window(xlim = lim$x, ylim = lim$y, asp = 1)

## Reset center and scale
options(isopleuros.center = NULL)
options(isopleuros.scale = NULL)

## Compute ternary coordinates
pt <- coordinates_ternary(x = x, y = y, z = z, center = center, scale = scale)

Expand Down
12 changes: 12 additions & 0 deletions inst/tinytest/test_scale.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
ternary_plot(lava, center = TRUE, scale = FALSE)
expect_message(ternary_points(lava), "The current plot has been centered")

ternary_plot(lava, center = FALSE, scale = TRUE)
expect_message(ternary_points(lava), "The current plot has been scaled")

ternary_plot(lava, center = TRUE, scale = TRUE)
expect_message(ternary_points(lava))

ternary_plot(lava, center = FALSE, scale = FALSE)
expect_silent(ternary_points(lava))

if (at_home()) {
using("tinysnapshot")
options(tinysnapshot_device = "svglite")
Expand Down

0 comments on commit 846b94c

Please sign in to comment.