View source: R/make-constructor.R
make_constructor | R Documentation |
The make_constructor()
functions sets up a user-facing constructor for
ggproto classes. Currently, make_constructor()
is implemented for
Geom
classes.
make_constructor(x, ...)
## S3 method for class 'Geom'
make_constructor(
x,
...,
checks = exprs(),
omit = character(),
env = caller_env()
)
## S3 method for class 'Stat'
make_constructor(
x,
...,
checks = exprs(),
omit = character(),
env = caller_env()
)
x |
An object to setup a constructor for. |
... |
Name-value pairs to use as additional arguments in the
constructor. For layers, these are passed on to |
checks |
A list of calls to be evaluated before construction of the
object, such as one constructed with |
omit |
A character vector of automatically retrieved argument names that should not be converted to user-facing arguments. Useful for internally computed variables. |
env |
An environment to search for the object. |
A function
# For testing purposes, a geom that returns grobs
GeomTest <- ggproto(
"GeomTest", Geom,
draw_group = function(..., grob = grid::pointsGrob()) {
return(grob)
}
)
# Creating a constructor
geom_test <- make_constructor(GeomTest)
# Note that `grob` is automatically an argument to the function
names(formals(geom_test))
# Use in a plot
set.seed(1234)
p <- ggplot(mtcars, aes(disp, mpg))
p + geom_test()
p + geom_test(grob = grid::circleGrob())
# The `checks` argument can be used to evaluate arbitrary expressions in
# the constructor before building the layer.
geom_path2 <- make_constructor(
GeomPath, checks = rlang::exprs(
match.arg(lineend, c("butt", "round", "square")),
match.arg(linejoin, c("round", "mitre", "bevel"))
)
)
# Note the inclusion of the expressions
print(geom_path2)
# Argument mismatch is detected
try(geom_path2(linejoin = "foo"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.