| register_pattern | R Documentation |
register_pattern() registers a pattern so it can be used by
grid.pattern() / patternGrob() (and {ggpattern}).
unregister_pattern() removes a pattern previously registered by the same owner
(as determined by env); it never removes patterns registered by other packages
or the builtin {gridpattern} patterns.
register_pattern(
name,
fn,
kind = c("geometry", "array"),
defaults = list(),
unprefixed = character(),
env = caller_env()
)
unregister_pattern(name, env = caller_env())
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 |
Each pattern is “owned” by either the package that registered it
or by the user (if registered from the global environment e.g. in a script or at the console).
Patterns may always be referred to by their qualified name
"owner::name" e.g. "gridpattern::stripe", "pkg::name", or "R_GlobalEnv::name".
A bare pattern name like "stripe" is resolved as follows:
If the user has set a preference with prefer_pattern() then the preferred pattern.
If exactly one owner (the user, {gridpattern}, or a loaded package) has a pattern with that name
then that pattern.
Patterns set by the user with the (legacy) ggpattern_geometry_funcs / ggpattern_array_funcs options
are owned by the user.
If the user has more than one pattern with that name (from register_pattern() and/or the options)
an error is thrown asking them to remove all but one of them
(since they share the same qualified name "R_GlobalEnv::name" a preference can't choose between them).
Otherwise there is a conflict and an error is thrown, asking for either a qualified "owner::name"
or a prefer_pattern() preference.
In particular, a user pattern with the same name as a builtin {gridpattern} pattern
does not mask the builtin pattern without a preference.
conflicting_patterns() lists the patterns whose bare names are shared by more than one pattern.
patternGrob() resolves the pattern name when the grob is created (if the pattern is registered)
so redrawing the grob (e.g. when resizing an interactive graphics device)
still works even if the pattern is later unregistered or conflicted
(e.g. by local_pattern() going out of scope).
Changing the grob's pattern with grid::editGrob() resolves the new pattern name when drawn.
Packages should register their patterns in their .onLoad() function.
We recommend wrapping the registration in rlang::on_package_load()
so the pattern is registered whenever {gridpattern} is loaded
(allowing {gridpattern} to be in Suggests and surviving {gridpattern} being reloaded).
Since a Suggests version requirement isn't enforced when installing or loading packages,
also check that the installed {gridpattern} has register_pattern()
(and otherwise fall back to setting the legacy ggpattern_geometry_funcs option):
.onLoad <- function(libname, pkgname) {
rlang::on_package_load("gridpattern", {
if (exists("register_pattern", envir = asNamespace("gridpattern"), inherits = FALSE)) {
gridpattern::register_pattern("brick", create_pattern_brick)
} else {
funcs <- getOption("ggpattern_geometry_funcs", list())
funcs[["brick"]] <- create_pattern_brick
options(ggpattern_geometry_funcs = funcs)
}
})
}
Package code that relies on a particular pattern should use its qualified name
(e.g. "gridpattern::stripe") since other packages (or users) may register patterns
with the same bare name.
register_pattern() and unregister_pattern() invisibly return the
previous registration (a list) or NULL if there wasn't one.
registered_patterns() and has_pattern() to list available patterns.
local_pattern() to temporarily register a pattern.
prefer_pattern() and conflicting_patterns() to resolve pattern name conflicts.
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))
# don't clobber any pre-existing "polygon" pattern
if (!has_pattern("polygon")) {
register_pattern("polygon", create_pattern_polygon)
grid::grid.newpage()
grid.pattern("polygon", x_hex, y_hex, fill = "blue")
unregister_pattern("polygon")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.