Skip to content

Commit

Permalink
format.marker, tests, ASCII fix 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
rsh52 committed Oct 28, 2024
1 parent 49d4735 commit f6d4b54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(format,marker)
export("%+%")
export(.default_colours)
export(.default_glyphs)
Expand Down
26 changes: 24 additions & 2 deletions R/scale_marker.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pal_markers <- function(glyphs = NULL, colours = NULL, n_values = NULL) {
# Define colour and glyph lengths via markers supplied or default values
n_values <- n_values %||% max(length(glyphs), length(colours))
if (n_values == 0) n_values <- length(.default_glyphs)

# Create a vctrs list to store colour and glyph values
markers <- vctrs::new_rcrd(
list(
Expand Down Expand Up @@ -99,7 +98,15 @@ pal_markers <- function(glyphs = NULL, colours = NULL, n_values = NULL) {
#'
#' @export
# Set default glyphs and colours
.default_glyphs <- c("", "", "", "", "", "", "", "", "")
.default_glyphs <- c("\u25CF", # ● BLACK CIRCLE
"\u25A0", # ■ BLACK SQUARE
"\u25B2", # ▲ BLACK UP-POINTING TRIANGLE
"\u2B25", # ⬥ BLACK DIAMOND
"\u25BC", # ▼ BLACK DOWN-POINTING TRIANGLE
"\u25A2", # ▢ WHITE SQUARE WITH ROUNDED CORNERS
"\u25A1", # □ WHITE SQUARE
"\u25B3", # △ WHITE UP-POINTING TRIANGLE
"\u25C7") # ◇ WHITE DIAMOND

#' @rdname dot-default_glyphs
#' @export
Expand All @@ -108,3 +115,18 @@ pal_markers <- function(glyphs = NULL, colours = NULL, n_values = NULL) {
#' @rdname dot-default_glyphs
#' @export
.default_limits <- c("val1", "val2", "val3", "val4", "val5", "val6", "val7", "val8", "val9")

#' @noRd
#' @export
# Define the format method for marker class
format.marker <- function(x, ...) {
# Extract the colour and glyph components
colours <- vctrs::field(x, "colour")
glyphs <- vctrs::field(x, "glyphs")

# Create a formatted string for each marker
formatted_markers <- paste0("Glyph: ", glyphs, ", Colour: ", colours)

# Return the formatted markers as a character vector
return(formatted_markers)
}
8 changes: 8 additions & 0 deletions tests/testthat/test-scale_marker.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ test_that("scale_marker_discrete uses default colours when missing", {
expect_equal(length(scale$palette(3)), 3)
expect_equal(vctrs::field(scale$palette(3), "colour"), .default_colours[1:3]) # First 3 default glyphs
})

test_that("format.marker works", {
out <- format.marker(x = vctrs::new_rcrd(list(colour = "red", glyphs = "test"),
class = "marker"))

expect_equal(out, "Glyph: test, Colour: red")
expect_equal(class(out), "character")
})

0 comments on commit f6d4b54

Please sign in to comment.