| local_pattern | R Documentation |
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.
local_pattern(
name,
fn,
kind = c("geometry", "array"),
defaults = list(),
unprefixed = character(),
env = caller_env(),
.local_envir = parent.frame()
)
name |
Pattern name. Must not contain |
fn |
Pattern function. Its required signature depends on
See the “Developing Patterns” vignette
|
kind |
Either |
defaults |
A named list of pattern-specific default parameter values
e.g. |
unprefixed |
A character vector of parameter names without the |
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 |
.local_envir |
The environment whose exit should undo |
local_pattern() invisibly returns the
previous registration (a list) or NULL if there wasn't one.
register_pattern() to (permanently) register patterns.
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"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.