Skip to content

Commit

Permalink
feat: 'reset_image_cache()'
Browse files Browse the repository at this point in the history
* `reset_image_cache()` resets the image cache used by `grid.pattern_image()`
   and `grid.pattern_placeholder()` to store images (#63).

closes #25, closes #63
  • Loading branch information
trevorld committed Oct 3, 2023
1 parent 9b36264 commit ad61790
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 22 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Package: gridpattern
Type: Package
Title: 'grid' Pattern Grobs
Version: 1.0.4
Version: 1.1.0
Authors@R: c(person("Mike", "FC", role = "aut", comment = "Code/docs adapted from ggpattern"),
person("Trevor L.", "Davis", role=c("aut", "cre"), email="[email protected]",
person("Trevor L", "Davis", role=c("aut", "cre"), email="[email protected]",
comment = c(ORCID = "0000-0001-6341-4639")),
person("Thomas Lin", "Pedersen", role = "ctb", comment = "new_data_frame() copied from ggplot2"))
Description: Provides 'grid' grobs that fill in a user-defined area with various patterns. Includes enhanced versions of the geometric and image-based patterns originally contained in the 'ggpattern' package as well as original 'pch', 'polygon_tiling', 'regular_polygon', 'rose', 'text', 'wave', and 'weave' patterns plus support for custom user-defined patterns.
Expand All @@ -27,7 +27,7 @@ Imports:
Suggests:
ambient,
knitr,
magick,
magick (>= 2.7.4),
ragg (>= 1.2.0),
rmarkdown,
svglite (>= 2.1.0),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export(patternGrob)
export(pattern_hex)
export(pattern_square)
export(pattern_weave)
export(reset_image_cache)
export(star_angle)
export(star_scale)
import(grid)
Expand Down
12 changes: 9 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
gridpattern v1.0.4
gridpattern v1.1.0
==================

Minor improvements
------------------
New Features
------------

* `reset_image_cache()` resets the image cache used by `grid.pattern_image()`
and `grid.pattern_placeholder()` to store images (#63).

Bug fixes and minor improvements
--------------------------------

* The function `guess_has_R4.1_features()` is now available as a "standalone" file.
You may copy its source under the permissive [MIT No Attribution (MIT-0)](https://spdx.org/licenses/MIT-0.html)
Expand Down
6 changes: 5 additions & 1 deletion R/pattern-array-image.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#' @param filter Filter to use when scaling. `magick::filter_types()` returns a vector of supported values.
#' @return A grid grob object invisibly. If `draw` is `TRUE` then also draws to the graphic device as a side effect.
#' @examples
#' if (require("magick")) {
#' \donttest{# May emit a "CPU time > 2.5 times elapsed time" NOTE in a CRAN check
#' if (requireNamespace("magick")) {
#' x_hex <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
#' y_hex <- 0.5 + 0.5 * sin(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
#' logo_filename <- system.file("img", "Rlogo.png" , package = "png")
Expand All @@ -33,8 +34,11 @@
#' try(grid.pattern_image(x_hex, y_hex, filename = logo_filename,
#' type = "tile"))
#' }
#' }
#' @seealso [grid.pattern_placeholder()] is an image pattern that uses images
#' downloaded from the internet.
#' [reset_image_cache()] resets the image cache used by `grid.pattern_image()`
#' and [grid.pattern_placeholder()].
#' @export
grid.pattern_image <- function(x = c(0, 0, 1, 1), y = c(1, 0, 0, 1), id = 1L, ...,
filename = "", type = "fit", scale = 1,
Expand Down
2 changes: 1 addition & 1 deletion R/pattern-array-magick.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param fill Fill colour
#' @return A grid grob object invisibly. If `draw` is `TRUE` then also draws to the graphic device as a side effect.
#' @examples
#' if (require("magick")) {
#' if (requireNamespace("magick")) {
#' x_hex <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
#' y_hex <- 0.5 + 0.5 * sin(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
#' grid.pattern_magick(x_hex, y_hex, type="octagons", fill="blue", scale=2)
Expand Down
3 changes: 2 additions & 1 deletion R/pattern-array-placeholder.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
#' If you would like only greyscale images append `bw` to the name.
#' @return A grid grob object invisibly. If `draw` is `TRUE` then also draws to the graphic device as a side effect.
#' @examples
#' if (require("magick")) {
#' if (requireNamespace("magick")) {
#' x_hex <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
#' y_hex <- 0.5 + 0.5 * sin(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
#' # requires internet connection to download from placeholder image websites
#' try(grid.pattern_placeholder(x_hex, y_hex, type="bear"))
#' }
#'
#' print(names_placeholder)
#' @seealso [reset_image_cache()] resets the image cache used by [grid.pattern_image()] and `grid.pattern_placeholder()`.
#' @export
grid.pattern_placeholder <- function(x = c(0, 0, 1, 1), y = c(1, 0, 0, 1), id = 1L, ...,
type = "kitten", alpha = gp$alpha %||% NA_real_,
Expand Down
3 changes: 2 additions & 1 deletion R/pattern-array-plasma.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#' @param scale Extra scaling
#' @return A grid grob object invisibly. If `draw` is `TRUE` then also draws to the graphic device as a side effect.
#' @examples
#' if (require("magick")) {
#' if (requireNamespace("magick")) {
#' x_hex <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
#' y_hex <- 0.5 + 0.5 * sin(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
#' grid.pattern_plasma(x_hex, y_hex, fill = "green")
#' }
#' @seealso [grid.pattern_ambient()] provides a noise pattern using the `ambient` package.
#' Pseudorandom seeds for the plasma pattern may be set via [magick::magick_set_seed()].
#' @export
grid.pattern_plasma <- function(x = c(0, 0, 1, 1), y = c(1, 0, 0, 1), id = 1L, ...,
fill = gp$fill %||% "grey80", scale = 1, alpha = gp$alpha %||% NA_real_,
Expand Down
9 changes: 9 additions & 0 deletions R/reset_image_cache.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#' Reset 'gridpattern' image cache
#'
#' [grid.pattern_image()] and [grid.pattern_placeholder()] store images in a cache
#' (so we won't download image URLs over and over).
#' `reset_image_cache()` resets this cache.
#' @export
reset_image_cache <- function() {
memoise::forget(img_read_memoised)
}
3 changes: 3 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* Example that triggered a "Examples with CPU time > 2.5 times elapsed time" NOTE
is now wrapped in `\donttest{}`.

## Test environments

* local (linux, R 4.3.1)
Expand Down
6 changes: 5 additions & 1 deletion man/grid.pattern_image.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/grid.pattern_magick.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/grid.pattern_placeholder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/grid.pattern_plasma.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/gridpattern-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions man/reset_image_cache.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added tests/figs/array/plasma.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions tests/testthat/test_array.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test_that("array patterns works as expected", {
skip_on_ci()
skip_on_cran()
skip_if_not(capabilities("cairo"))
skip_if_not_installed("magick")
skip_if_not_installed("magick", "2.7.4")
skip_if_not_installed("ragg")

x <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6))
Expand Down Expand Up @@ -71,6 +71,12 @@ test_that("array patterns works as expected", {
function() grid.pattern_plasma(x = c(0.5, 0.5, 0.5, 0.5),
y = c(0, 1, 1, 0), fill = "green"))

test_raster("plasma.png",
function() {
magick::magick_set_seed(42) # introduced in v2.7.4
grid.pattern_plasma(x, y, fill = "green")
})

playing_card_symbols <- c("\u2660", "\u2665", "\u2666", "\u2663")
test_raster("text.png",
function() grid.pattern_text(x, y, shape = playing_card_symbols,
Expand All @@ -85,12 +91,6 @@ test_that("array patterns works as expected", {
use_R4.1_masks = NULL,
frequency = 1:4, gp = gp))

# plasma images are random and doesn't seem to be a way to set a seed
tmpfile <- tempfile(fileext = ".png")
grob <- my_png(tmpfile, function() grid.pattern_plasma(fill="green"))
unlink(tmpfile)
expect_true(inherits(grob, "pattern"))

create_pattern_simple <- function(width, height, params, legend) {
choice <- params$pattern_type
if (is.null(choice) || is.na(choice) || !is.character(choice)) {
Expand Down

0 comments on commit ad61790

Please sign in to comment.