local_pattern() registers a pattern (see register_pattern())
until the current function (or test, see .local_envir) exits
and then restores the previous registration (if any).
Requires the suggested {withr} package.
Usage
local_pattern(
name,
fn,
kind = c("geometry", "array"),
defaults = list(),
unprefixed = character(),
env = caller_env(),
.local_envir = parent.frame()
)Arguments
- name
Pattern name. Must not contain
":".- fn
Pattern function. Its required signature depends on
kind:- geometry
function(params, boundary_df, aspect_ratio, legend)returning a grid grob that lies within the boundary (e.g. clipped to it).paramsis a list of (prefixed) pattern parameters e.g.params$pattern_fill,boundary_dfis a data frame withx,y, andidcolumns describing the boundary polygon(s) in “npc” coordinates,aspect_ratiois the (best guess) aspect ratio of the viewport, andlegendis whether the pattern is being drawn in a{ggpattern}legend key.- array
function(width, height, params, legend)returning a 3D array of RGBA values (all values in the range [0, 1]) which{gridpattern}will mask to the boundary.widthandheightare the dimensions (in pixels) of the boundary's bounding box andparamsandlegendare as for geometry patterns.
See the “Developing Patterns” vignette
vignette("developing-patterns", package = "gridpattern")for more details and examples.- kind
Either
"geometry"or"array".- defaults
A named list of pattern-specific default parameter values e.g.
list(pattern_type = "running", pattern_spacing = 0.1). Names are used as-is and must bepattern_prefixed unless listed inunprefixed. Values may be constants or functions of the formfunction(params, gp)whereparamsis the list of pattern parameters filled in so far andgpis the grob'sgrid::gpar()object e.g.list(pattern_amplitude = function(params, gp) 0.5 * params$pattern_spacing)orlist(pattern_size = function(params, gp) gp$fontsize %||% 12). These replace the generic defaults for parameters that weren't supplied. The remaining generic defaults are filled in first and then these in the order given so a function default may use any generic parameter and any earlier pattern-specific default. Note{ggpattern}supplies its own (generic) defaults for most of its pattern aesthetics so these will mainly be used withgrid.pattern()/patternGrob().- unprefixed
A character vector of parameter names without the
pattern_prefix that the pattern reads so thatdefaultsmay set them. The usual case is a plain{ggplot2}aesthetic that{ggpattern}passes to pattern functions alongside thepattern_aesthetics e.g.defaults = list(fill = "black"), unprefixed = "fill"lets a pattern that readsparams$fillunder{ggpattern}also work withgrid.pattern(). Since such patterns are rare any other unprefixed name (e.g.fillorspacinginstead ofpattern_fillorpattern_spacing) is an error of class"gridpattern_error_unprefixed_defaults".- env
The environment used to determine the “owner” of the pattern. If writing a function that registers patterns on behalf of its caller add an
env = rlang::caller_env()argument to that function and pass it on.- .local_envir
The environment whose exit should undo
local_pattern()'s registration.
Value
local_pattern() invisibly returns the
previous registration (a list) or NULL if there wasn't one.
See also
register_pattern() to (permanently) register patterns.
Examples
create_pattern_polygon <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
grid::polygonGrob(boundary_df$x, boundary_df$y, boundary_df$id,
gp = grid::gpar(fill = params$pattern_fill))
}
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))
draw_polygon <- function() {
local_pattern("polygon", create_pattern_polygon)
grid::grid.newpage()
grid.pattern("polygon", x_hex, y_hex, fill = "red")
}
# don't clobber (or be confused by) any pre-existing "polygon" pattern
if (requireNamespace("withr", quietly = TRUE) && !has_pattern("polygon")) {
draw_polygon()
}
print(has_pattern("polygon"))
#> [1] FALSE