File: make_constructor.Rd

package info (click to toggle)
r-cran-ggplot2 4.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,084 kB
  • sloc: sh: 15; makefile: 5
file content (85 lines) | stat: -rw-r--r-- 2,225 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/make-constructor.R
\name{make_constructor}
\alias{make_constructor}
\alias{make_constructor.Geom}
\alias{make_constructor.Stat}
\title{Produce boilerplate constructors}
\usage{
make_constructor(x, ...)

\method{make_constructor}{Geom}(
  x,
  ...,
  checks = exprs(),
  omit = character(),
  env = caller_env()
)

\method{make_constructor}{Stat}(
  x,
  ...,
  checks = exprs(),
  omit = character(),
  env = caller_env()
)
}
\arguments{
\item{x}{An object to setup a constructor for.}

\item{...}{Name-value pairs to use as additional arguments in the
constructor. For layers, these are passed on to \code{\link[=layer]{layer(params)}}.}

\item{checks}{A list of calls to be evaluated before construction of the
object, such as one constructed with \code{\link[rlang:defusing-advanced]{exprs()}}.}

\item{omit}{A character vector of automatically retrieved argument names
that should not be converted to user-facing arguments. Useful for
internally computed variables.}

\item{env}{An environment to search for the object.}
}
\value{
A function
}
\description{
The \code{make_constructor()} functions sets up a user-facing constructor for
ggproto classes. Currently, \code{make_constructor()} is implemented for
\code{Geom} classes.
}
\examples{
# 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"))
}
\keyword{internal}