Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the package version to 0.4.1, removes the cli dependency, and replaces cli::cli_abort() and cli::cli_warn() calls with custom error and warning functions (abort(), warn(), info()) across several files. It also introduces a new internal utility function lighten() to lighten colors and updates plot.SupervisedSession.R to fetch colors dynamically from the rtemis namespace. Feedback on the changes suggests caching the rtemis namespace environment instead of repeatedly calling getFromNamespace(), ensuring that lighten() preserves the names of its input vector, and adding a unit test to verify this name preservation.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| kind_colors <- c( | ||
| train = "#607D8B", | ||
| outer_fold = "#8E63CE", | ||
| tune = "#BE2E5F", | ||
| grid_cell = "#466D96", | ||
| train_alg = "#4F662A", | ||
| predict = "#6CA3A0", | ||
| varimp = "#F08904", | ||
| metrics = "#FDB808" | ||
| train = "#808080", | ||
| outer_fold = getFromNamespace("col_outer", "rtemis"), | ||
| tune = getFromNamespace("col_tuner", "rtemis"), | ||
| grid_cell = lighten(getFromNamespace("col_tuner", "rtemis"), 0.5), | ||
| preprocess = getFromNamespace("col_preprocessor", "rtemis"), | ||
| decompose = getFromNamespace("col_decom", "rtemis"), | ||
| train_alg = getFromNamespace("highlight_col", "rtemis"), | ||
| predict = rtemis.core::rtemis_colors[["blue"]], | ||
| varimp = rtemis.core::rtemis_colors[["light_blue"]], | ||
| metrics = rtemis.core::rtemis_colors[["orange"]] | ||
| ) |
There was a problem hiding this comment.
Repeatedly calling getFromNamespace() for multiple lookups in the same namespace is inefficient because each call performs package loading checks and environment lookups. Caching the namespace environment once using asNamespace("rtemis") and accessing the variables directly is much cleaner and more efficient.
rtemis_ns <- asNamespace("rtemis")
kind_colors <- c(
train = "#808080",
outer_fold = rtemis_ns$col_outer,
tune = rtemis_ns$col_tuner,
grid_cell = lighten(rtemis_ns$col_tuner, 0.5),
preprocess = rtemis_ns$col_preprocessor,
decompose = rtemis_ns$col_decom,
train_alg = rtemis_ns$highlight_col,
predict = rtemis.core::rtemis_colors[["blue"]],
varimp = rtemis.core::rtemis_colors[["light_blue"]],
metrics = rtemis.core::rtemis_colors[["orange"]]
)| lighten <- function(x, amount = 0.1) { | ||
| if ( | ||
| !is.numeric(amount) || | ||
| length(amount) != 1L || | ||
| is.na(amount) || | ||
| amount < 0 || | ||
| amount > 1 | ||
| ) { | ||
| abort( | ||
| "`amount` must be a single numeric value in [0, 1].", | ||
| class = c("rtemis_value_error", "rtemis_input_error") | ||
| ) | ||
| } | ||
| # Work in [0, 1] | ||
| rgba <- grDevices::col2rgb(x, alpha = TRUE) / 255 | ||
| # Move each RGB channel `amount` of the way toward 1; leave alpha as is. | ||
| rgba[1:3, ] <- rgba[1:3, ] + (1 - rgba[1:3, ]) * amount | ||
| out <- character(ncol(rgba)) | ||
| opaque <- rgba[4L, ] == 1 | ||
| out[opaque] <- grDevices::rgb( | ||
| rgba[1L, opaque], | ||
| rgba[2L, opaque], | ||
| rgba[3L, opaque] | ||
| ) | ||
| out[!opaque] <- grDevices::rgb( | ||
| rgba[1L, !opaque], | ||
| rgba[2L, !opaque], | ||
| rgba[3L, !opaque], | ||
| alpha = rgba[4L, !opaque] | ||
| ) | ||
| out | ||
| } |
There was a problem hiding this comment.
The lighten() function does not preserve the names of the input vector x because grDevices::rgb() drops them. In R, vector functions should generally preserve the names of their inputs to prevent breaking downstream code that relies on named vectors. This can be fixed by assigning names(x) to the output vector before returning it.
lighten <- function(x, amount = 0.1) {
if (
!is.numeric(amount) ||
length(amount) != 1L ||
is.na(amount) ||
amount < 0 ||
amount > 1
) {
abort(
"`amount` must be a single numeric value in [0, 1].",
class = c("rtemis_value_error", "rtemis_input_error")
)
}
# Work in [0, 1]
rgba <- grDevices::col2rgb(x, alpha = TRUE) / 255
# Move each RGB channel `amount` of the way toward 1; leave alpha as is.
rgba[1:3, ] <- rgba[1:3, ] + (1 - rgba[1:3, ]) * amount
out <- character(ncol(rgba))
opaque <- rgba[4L, ] == 1
out[opaque] <- grDevices::rgb(
rgba[1L, opaque],
rgba[2L, opaque],
rgba[3L, opaque]
)
out[!opaque] <- grDevices::rgb(
rgba[1L, !opaque],
rgba[2L, !opaque],
rgba[3L, !opaque],
alpha = rgba[4L, !opaque]
)
names(out) <- names(x)
out
}| test_that("lighten accepts named colors and is vectorized", { | ||
| expect_equal( | ||
| rtemis.draw:::lighten(c("red", "#000000"), 0.5), | ||
| c("#FF8080", "#808080") | ||
| ) | ||
| }) |
There was a problem hiding this comment.
Add a test case to verify that lighten() correctly preserves the names of the input vector.
test_that("lighten accepts named colors and is vectorized", {
expect_equal(
rtemis.draw:::lighten(c("red", "#000000"), 0.5),
c("#FF8080", "#808080")
)
# Test name preservation
expect_equal(
rtemis.draw:::lighten(c(a = "#000000", b = "#FFFFFF"), 0.5),
c(a = "#808080", b = "#FFFFFF")
)
})There was a problem hiding this comment.
Pull request overview
This PR migrates rtemis.draw away from {cli} condition helpers toward {rtemis.core}’s condition/message utilities, while also adding an internal lighten() helper to support consistent color derivations in plotting.
Changes:
- Replaced many
cli::cli_abort()/cli::cli_warn()calls withabort()/warn()(and added structuredrtemis_*error classes). - Added internal
lighten()utility and corresponding unit tests; updatedplot(SupervisedSession)colors to use shared palette constants. - Updated tests to reflect the new condition classes and expected signaling behavior; bumped package version and
rtemis.coreminimum version.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| r/tests/testthat/test-utils.R | Adds unit tests for new internal lighten() helper. |
| r/tests/testthat/test-network.R | Updates expectation for static-export “not supported” signaling. |
| r/tests/testthat/test-map.R | Updates expectation for static-export “not supported” signaling. |
| r/tests/testthat/test-draw_spectrogram.R | Updates expected error classes for validation failures. |
| r/tests/testthat/test-draw_a3.R | Updates expected error classes for validation failures. |
| r/R/utils.R | Converts validation errors to abort() and introduces internal lighten(). |
| r/R/plot.SupervisedSession.R | Switches event kind palette to shared colors and uses lighten() for derived colors. |
| r/R/option.R | Converts data_zoom validation error to abort(). |
| r/R/network.R | Converts warnings/errors to warn()/abort() and assigns rtemis_* classes. |
| r/R/map.R | Converts warnings/errors to warn()/abort() and assigns rtemis_* classes. |
| r/R/draw.R | Converts various argument validation errors to abort() with rtemis_* classes. |
| r/R/draw_spectrogram.R | Converts validation errors and performance warnings to abort()/warn()/info(). |
| r/R/draw_gantt.R | Converts input validation errors to abort() with rtemis_* classes. |
| r/R/draw_a3.R | Converts input validation errors to abort() with rtemis_* classes. |
| r/DESCRIPTION | Bumps version/date, removes {cli} import, raises minimum {rtemis.core} version. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| info( | ||
| "Consider reducing 'n_fft' (currently ", | ||
| n_fft, | ||
| ")", | ||
| " or the signal length." | ||
| ) |
No description provided.