| prefer_pattern | R Documentation |
prefer_pattern() sets which pattern a bare pattern name resolves to,
taking priority over all other patterns with that name.
unprefer_pattern() removes such preferences.
conflicting_patterns() returns the available patterns whose bare name is shared
by more than one pattern (see register_pattern() for how bare names are resolved).
Using such a bare name throws an error unless a preference has been set.
prefer_pattern(pattern)
unprefer_pattern(pattern)
conflicting_patterns()
pattern |
A character vector.
For |
prefer_pattern() and unprefer_pattern() are vectorized and return the previous preferences (if any)
so they may be used to temporarily change preferences:
old <- prefer_pattern(new) # ... use patterns ... unprefer_pattern(new) prefer_pattern(old) # does nothing if `old` is empty
Preferences only last for the current R session.
Users may put prefer_pattern() calls in their .Rprofile (or scripts)
to make them persistent.
Packages should not call prefer_pattern() and should instead
refer to patterns by their qualified name "pkg::name".
prefer_pattern() and unprefer_pattern() invisibly return a character vector of the
qualified names of the previous preferences (for the bare names in pattern)
that existed (character(0) if there weren't any).
conflicting_patterns() returns (for just the conflicting patterns)
a data frame with columns
The pattern name.
The package that registered the pattern or "R_GlobalEnv" for the user.
Either "geometry" or "array".
One of "builtin", "register_pattern", or "options".
Whether the bare name currently resolves to this pattern.
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 user "stripe" pattern
run_example <- !has_pattern("R_GlobalEnv::stripe")
if (run_example) {
# temporarily remove any pre-existing "stripe" preference
old <- unprefer_pattern("stripe")
register_pattern("stripe", create_pattern_polygon)
}
if (run_example) {
print(conflicting_patterns())
}
if (run_example) {
# bare "stripe" is now ambiguous
grid::grid.newpage()
try(grid.pattern("stripe", x_hex, y_hex))
}
if (run_example) {
# prefer the user's "stripe" pattern over the builtin "stripe" pattern
prefer_pattern("R_GlobalEnv::stripe")
grid::grid.newpage()
grid.pattern("stripe", x_hex, y_hex, fill = "blue")
}
if (run_example) {
print(conflicting_patterns())
}
if (run_example) {
# remove the preference
unprefer_pattern("stripe")
# remove "R_GlobalEnv::stripe" (not "gridpattern::stripe")
unregister_pattern("stripe")
# restore any pre-existing preference
prefer_pattern(old)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.