Skip to content

Commit 8fde6e8

Browse files
committed
collect classes in one place
1 parent f8ed252 commit 8fde6e8

File tree

11 files changed

+166
-167
lines changed

11 files changed

+166
-167
lines changed

R/aes.R

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,6 @@ aes <- function(x, y, ...) {
108108
class_mapping(rename_aes(args), env = parent.frame())
109109
}
110110

111-
#' The mapping class
112-
#'
113-
#' The mapping class holds a list of quoted expressions
114-
#' ([quosures][rlang::topic-quosure]) or constants. An object is typically
115-
#' constructed using the [`aes()`] function.
116-
#'
117-
#' @param x A list of quosures and constants.
118-
#' @param env An environment for symbols that are not quosures or constants.
119-
#'
120-
#' @export
121-
class_mapping <- S7::new_class(
122-
"mapping", parent = S7::new_S3_class("gg"),
123-
constructor = function(x, env = globalenv()) {
124-
check_object(x, is.list, "a {.cls list}")
125-
x <- lapply(x, new_aesthetic, env = env)
126-
S7::new_object(x)
127-
}
128-
)
129-
130111
#' @export
131112
#' @rdname is_tests
132113
is.mapping <- function(x) S7::S7_inherits(x, class_mapping)

R/all-classes.R

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
class_gg <- S7::new_class("gg", abstract = TRUE)
3+
class_S3_gg <- S7::new_S3_class("gg")
34
class_scale <- S7::new_S3_class("Scale")
45
class_guides <- S7::new_S3_class("Guides")
56
class_coord <- S7::new_S3_class("Coord")
@@ -9,3 +10,163 @@ class_layout <- S7::new_S3_class("Layout")
910
class_scales_list <- S7::new_S3_class("ScalesList")
1011
class_ggproto <- S7::new_S3_class("ggproto")
1112
class_gtable <- S7::new_S3_class("gtable")
13+
14+
#' The theme class
15+
#'
16+
#' The theme class holds information on how non-data elements of the plot
17+
#' should be rendered. The preferred way to construct an object of this class
18+
#' is through the [`theme()`] function.
19+
#'
20+
#' @param elements A named list containing theme elements.
21+
#' @param complete A boolean value stating whether a theme is complete.
22+
#' @param validate A boolean value stating whether a theme should still be
23+
#' validated.
24+
#'
25+
#' @export
26+
class_theme <- S7::new_class(
27+
"theme", class_S3_gg,
28+
properties = list(
29+
complete = S7::class_logical,
30+
validate = S7::class_logical
31+
),
32+
constructor = function(elements, complete, validate) {
33+
S7::new_object(
34+
elements,
35+
complete = complete,
36+
validate = validate
37+
)
38+
}
39+
)
40+
41+
#' The labels class
42+
#'
43+
#' The labels class holds a list with label information to display as titles
44+
#' of plot components. The preferred way to construct an object of the labels
45+
#' class is to use the [`labs()`] function.
46+
#'
47+
#' @param labels A named list.
48+
#'
49+
#' @export
50+
class_labels <- S7::new_class(
51+
"labels", parent = class_S3_gg,
52+
constructor = function(labels) S7::new_object(labels),
53+
validator = function(self) {
54+
if (!is.list(self)) {
55+
return("labels must be a list.")
56+
}
57+
if (!is_named2(self)) {
58+
return("every label must be named.")
59+
}
60+
dups <- unique(names(self)[duplicated(names(self))])
61+
if (length(dups) > 0) {
62+
dups <- oxford_comma(dups, final = "and")
63+
return(paste0("labels cannot contain duplicate names (", dups, ")."))
64+
}
65+
return(NULL)
66+
}
67+
)
68+
69+
#' The mapping class
70+
#'
71+
#' The mapping class holds a list of quoted expressions
72+
#' ([quosures][rlang::topic-quosure]) or constants. An object is typically
73+
#' constructed using the [`aes()`] function.
74+
#'
75+
#' @param x A list of quosures and constants.
76+
#' @param env An environment for symbols that are not quosures or constants.
77+
#'
78+
#' @export
79+
class_mapping <- S7::new_class(
80+
"mapping", parent = class_S3_gg,
81+
constructor = function(x, env = globalenv()) {
82+
check_object(x, is.list, "a {.cls list}")
83+
x <- lapply(x, new_aesthetic, env = env)
84+
S7::new_object(x)
85+
}
86+
)
87+
88+
#' The ggplot class
89+
#'
90+
#' The ggplot class collects the needed information to render a plot.
91+
#' This class can be constructed using the [`ggplot()`] function.
92+
#'
93+
#' @param data A property containing any data coerced by [`fortify()`].
94+
#' @param layers A list of layer instances created by [`layer()`].
95+
#' @param scales A ScalesList ggproto object.
96+
#' @param guides A Guides ggproto object created by [`guides()`].
97+
#' @param mapping A mapping class object created by [`aes()`].
98+
#' @param theme A theme class object created by [`theme()`].
99+
#' @param coordinates A Coord ggproto object created by `coord_*()` family of
100+
#' functions.
101+
#' @param facet A Facet ggproto object created by `facet_*()` family of
102+
#' functions.
103+
#' @param layout A Layout ggproto object.
104+
#' @param labels A labels object created by [`labs()`].
105+
#' @param plot_env An environment.
106+
#'
107+
#' @export
108+
class_ggplot <- S7::new_class(
109+
name = "ggplot", parent = class_gg,
110+
properties = list(
111+
data = S7::class_any,
112+
layers = S7::class_list,
113+
scales = class_scales_list,
114+
guides = class_guides,
115+
mapping = class_mapping,
116+
theme = class_theme,
117+
coordinates = class_coord,
118+
facet = class_facet,
119+
layout = class_layout,
120+
labels = class_labels,
121+
plot_env = S7::class_environment
122+
),
123+
constructor = function(data = waiver(), layers = list(), scales = NULL,
124+
guides = NULL, mapping = aes(), theme = NULL,
125+
coordinates = coord_cartesian(default = TRUE),
126+
facet = facet_null(), layout = NULL,
127+
labels = labs(), plot_env = parent.frame()) {
128+
S7::new_object(
129+
S7::S7_object(),
130+
data = data, layers = layers,
131+
scales = scales %||% scales_list(),
132+
guides = guides %||% guides_list(),
133+
mapping = mapping, theme = theme %||% theme(),
134+
coordinates = coordinates, facet = facet,
135+
layout = layout %||% ggproto(NULL, Layout),
136+
labels = labels, plot_env = plot_env
137+
)
138+
}
139+
)
140+
141+
#' The ggplot built class
142+
#'
143+
#' The ggplot built class is an intermediate class and represents a processed
144+
#' ggplot object ready for rendering. It is constructed by calling
145+
#' [`ggplot_build()`] on a [ggplot][class_ggplot] object and is not meant to be
146+
#' instantiated directly. The class can be rendered to a gtable object by
147+
#' calling the [`ggplot_gtable()`] function on a ggplot built class object.
148+
#'
149+
#' @param data A list of plain data frames; one for each layer.
150+
#' @param layout A Layout ggproto object.
151+
#' @param plot A completed ggplot class object.
152+
#'
153+
#' @export
154+
class_ggplot_built <- S7::new_class(
155+
"ggplot_built",
156+
properties = list(
157+
data = S7::class_list,
158+
layout = class_layout,
159+
plot = class_ggplot
160+
),
161+
constructor = function(data = NULL, layout = NULL, plot = NULL) {
162+
if (is.null(data) || is.null(layout) || is.null(plot)) {
163+
cli::cli_abort(
164+
"The {.cls ggplot_built} class should be constructed by {.fn ggplot_build}."
165+
)
166+
}
167+
S7::new_object(
168+
S7::S7_object(),
169+
data = data, layout = layout, plot = plot
170+
)
171+
}
172+
)

R/labels.R

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -191,36 +191,6 @@ labs <- function(..., title = waiver(), subtitle = waiver(),
191191
class_labels(args)
192192
}
193193

194-
#' The labels class
195-
#'
196-
#' The labels class holds a list with label information to display as titles
197-
#' of plot components. The preferred way to construct an object of the labels
198-
#' class is to use the [`labs()`] function.
199-
#'
200-
#' @param labels A named list.
201-
#'
202-
#' @export
203-
class_labels <- S7::new_class(
204-
"labels", parent = S7::new_S3_class("gg"),
205-
constructor = function(labels) {
206-
S7::new_object(labels)
207-
},
208-
validator = function(self) {
209-
if (!is.list(self)) {
210-
return("labels must be a list.")
211-
}
212-
if (!is_named2(self)) {
213-
return("every label must be named.")
214-
}
215-
dups <- unique(names(self)[duplicated(names(self))])
216-
if (length(dups) > 0) {
217-
dups <- oxford_comma(dups, final = "and")
218-
return(paste0("labels cannot contain duplicate names (", dups, ")."))
219-
}
220-
return(NULL)
221-
}
222-
)
223-
224194
#' @rdname labs
225195
#' @export
226196
xlab <- function(label) {

R/plot-build.R

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,6 @@
11
#' @include plot.R
22
NULL
33

4-
#' The ggplot built class
5-
#'
6-
#' The ggplot built class is an intermediate class and represents a processed
7-
#' ggplot object ready for rendering. It is constructed by calling
8-
#' [`ggplot_build()`] on a [ggplot][class_ggplot] object and is not meant to be
9-
#' instantiated directly. The class can be rendered to a gtable object by
10-
#' calling the [`ggplot_gtable()`] function on a ggplot built class object.
11-
#'
12-
#' @param data A list of plain data frames; one for each layer.
13-
#' @param layout A Layout ggproto object.
14-
#' @param plot A completed ggplot class object.
15-
#'
16-
#' @export
17-
class_ggplot_built <- S7::new_class(
18-
"ggplot_built",
19-
properties = list(
20-
data = S7::class_list,
21-
layout = class_layout,
22-
plot = class_ggplot
23-
),
24-
constructor = function(data = NULL, layout = NULL, plot = NULL) {
25-
if (is.null(data) || is.null(layout) || is.null(plot)) {
26-
cli::cli_abort(
27-
"The {.cls ggplot_built} class should be constructed by {.fn ggplot_build}."
28-
)
29-
}
30-
S7::new_object(
31-
S7::S7_object(),
32-
data = data, layout = layout, plot = plot
33-
)
34-
}
35-
)
36-
374
#' Build ggplot for rendering.
385
#'
396
#' `ggplot_build()` takes the plot object, and performs all steps necessary

R/plot.R

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,6 @@
22
#' @include theme.R
33
NULL
44

5-
#' The ggplot class
6-
#'
7-
#' The ggplot class collects the needed information to render a plot.
8-
#' This class can be constructed using the [`ggplot()`] function.
9-
#'
10-
#' @param data A property containing any data coerced by [`fortify()`].
11-
#' @param layers A list of layer instances created by [`layer()`].
12-
#' @param scales A ScalesList ggproto object.
13-
#' @param guides A Guides ggproto object created by [`guides()`].
14-
#' @param mapping A mapping class object created by [`aes()`].
15-
#' @param theme A theme class object created by [`theme()`].
16-
#' @param coordinates A Coord ggproto object created by `coord_*()` family of
17-
#' functions.
18-
#' @param facet A Facet ggproto object created by `facet_*()` family of
19-
#' functions.
20-
#' @param layout A Layout ggproto object.
21-
#' @param labels A labels object created by [`labs()`].
22-
#' @param plot_env An environment.
23-
#'
24-
#' @export
25-
class_ggplot <- S7::new_class(
26-
name = "ggplot", parent = class_gg,
27-
properties = list(
28-
data = S7::class_any,
29-
layers = S7::class_list,
30-
scales = class_scales_list,
31-
guides = class_guides,
32-
mapping = class_mapping,
33-
theme = class_theme,
34-
coordinates = class_coord,
35-
facet = class_facet,
36-
layout = class_layout,
37-
labels = class_labels,
38-
plot_env = S7::class_environment
39-
),
40-
constructor = function(data = waiver(), layers = list(), scales = NULL,
41-
guides = NULL, mapping = aes(), theme = NULL,
42-
coordinates = coord_cartesian(default = TRUE),
43-
facet = facet_null(), layout = NULL,
44-
labels = labs(), plot_env = parent.frame()) {
45-
S7::new_object(
46-
S7::S7_object(),
47-
data = data, layers = layers,
48-
scales = scales %||% scales_list(),
49-
guides = guides %||% guides_list(),
50-
mapping = mapping, theme = theme %||% theme(),
51-
coordinates = coordinates, facet = facet,
52-
layout = layout %||% ggproto(NULL, Layout),
53-
labels = labels, plot_env = plot_env
54-
)
55-
}
56-
)
57-
585
#' Create a new ggplot
596
#'
607
#' `ggplot()` initializes a ggplot object. It can be used to

R/theme.R

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -552,33 +552,6 @@ theme <- function(...,
552552
class_theme(elements, complete = complete, validate = validate)
553553
}
554554

555-
#' The theme class
556-
#'
557-
#' The theme class holds information on how non-data elements of the plot
558-
#' should be rendered. The preferred way to construct an object of this class
559-
#' is through the [`theme()`] function.
560-
#'
561-
#' @param elements A named list containing theme elements.
562-
#' @param complete A boolean value stating whether a theme is complete.
563-
#' @param validate A boolean value stating whether a theme should still be
564-
#' validated.
565-
#'
566-
#' @export
567-
class_theme <- S7::new_class(
568-
"theme", S7::new_S3_class("gg"),
569-
properties = list(
570-
complete = S7::class_logical,
571-
validate = S7::class_logical
572-
),
573-
constructor = function(elements, complete, validate) {
574-
S7::new_object(
575-
elements,
576-
complete = complete,
577-
validate = validate
578-
)
579-
}
580-
)
581-
582555
#' @export
583556
#' @rdname is_tests
584557
is.theme <- function(x) S7::S7_inherits(x, class_theme)

man/class_ggplot.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/class_ggplot_built.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/class_labels.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/class_mapping.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)