81
81
82
82
# ' @export
83
83
# ' @rdname element
84
- element_blank <- function () {
85
- structure(
86
- list (),
87
- class = c(" element_blank" , " element" )
88
- )
89
- }
84
+ element <- S7 :: new_class(" element" , abstract = TRUE )
90
85
91
86
# ' @export
92
87
# ' @rdname element
93
- element_rect <- function (fill = NULL , colour = NULL , linewidth = NULL ,
94
- linetype = NULL , color = NULL , inherit.blank = FALSE , size = deprecated()) {
88
+ element_blank <- S7 :: new_class(" element_blank" , parent = element )
95
89
96
- if (lifecycle :: is_present(size )) {
97
- deprecate_soft0(" 3.4.0" , " element_rect(size)" , " element_rect(linewidth)" )
98
- linewidth <- size
99
- }
90
+ # ' @include properties.R
100
91
101
- if (! is.null(color )) colour <- color
102
- structure(
103
- list (fill = fill , colour = colour , linewidth = linewidth , linetype = linetype ,
104
- inherit.blank = inherit.blank ),
105
- class = c(" element_rect" , " element" )
106
- )
107
- }
92
+ # ' @export
93
+ # ' @rdname element
94
+ element_rect <- S7 :: new_class(
95
+ " element_rect" , parent = element ,
96
+ properties = element_props [c(" fill" , " colour" , " linewidth" , " linetype" , " inherit.blank" )],
97
+ constructor = function (fill = NULL , colour = NULL , linewidth = NULL ,
98
+ linetype = NULL , color = NULL , inherit.blank = FALSE ,
99
+ size = deprecated()){
100
+ if (lifecycle :: is_present(size )) {
101
+ deprecate_soft0(" 3.4.0" , " element_rect(size)" , " element_rect(linewidth)" )
102
+ linewidth <- size
103
+ }
104
+ S7 :: new_object(
105
+ S7 :: S7_object(),
106
+ fill = fill , colour = color %|| % colour ,
107
+ linewidth = linewidth , linetype = linetype ,
108
+ inherit.blank = inherit.blank
109
+ )
110
+ }
111
+ )
108
112
109
113
# ' @export
110
114
# ' @rdname element
111
115
# ' @param lineend Line end Line end style (round, butt, square)
112
116
# ' @param arrow Arrow specification, as created by [grid::arrow()]
113
- element_line <- function (colour = NULL , linewidth = NULL , linetype = NULL ,
114
- lineend = NULL , color = NULL , arrow = NULL , arrow.fill = NULL ,
115
- inherit.blank = FALSE , size = deprecated()) {
116
-
117
- if (lifecycle :: is_present(size )) {
118
- deprecate_soft0(" 3.4.0" , " element_line(size)" , " element_line(linewidth)" )
119
- linewidth <- size
117
+ element_line <- S7 :: new_class(
118
+ " element_line" , parent = element ,
119
+ properties = element_props [c(
120
+ " colour" , " linewidth" , " linetype" , " lineend" , " arrow" , " arrow.fill" ,
121
+ " inherit.blank"
122
+ )],
123
+ constructor = function (colour = NULL , linewidth = NULL , linetype = NULL ,
124
+ lineend = NULL , color = NULL , arrow = NULL ,
125
+ arrow.fill = NULL , inherit.blank = FALSE , size = deprecated()) {
126
+ if (lifecycle :: is_present(size )) {
127
+ deprecate_soft0(" 3.4.0" , " element_line(size)" , " element_line(linewidth)" )
128
+ linewidth <- size
129
+ }
130
+ colour <- color %|| % colour
131
+ S7 :: new_object(
132
+ S7 :: S7_object(),
133
+ colour = colour ,
134
+ linewidth = linewidth , linetype = linetype , lineend = lineend ,
135
+ arrow = arrow %|| % FALSE ,
136
+ arrow.fill = arrow.fill %|| % colour ,
137
+ inherit.blank = inherit.blank
138
+ )
120
139
}
121
-
122
- colour <- color %|| % colour
123
- arrow.fill <- arrow.fill %|| % colour
124
- arrow <- arrow %|| % FALSE
125
-
126
- structure(
127
- list (colour = colour , linewidth = linewidth , linetype = linetype , lineend = lineend ,
128
- arrow = arrow , arrow.fill = arrow.fill , inherit.blank = inherit.blank ),
129
- class = c(" element_line" , " element" )
130
- )
131
- }
132
-
140
+ )
133
141
134
142
# ' @param family Font family
135
143
# ' @param face Font face ("plain", "italic", "bold", "bold.italic")
@@ -145,93 +153,116 @@ element_line <- function(colour = NULL, linewidth = NULL, linetype = NULL,
145
153
# ' is anchored.
146
154
# ' @export
147
155
# ' @rdname element
148
- element_text <- function (family = NULL , face = NULL , colour = NULL ,
149
- size = NULL , hjust = NULL , vjust = NULL , angle = NULL , lineheight = NULL ,
150
- color = NULL , margin = NULL , debug = NULL , inherit.blank = FALSE ) {
151
-
152
- if (! is.null(color )) colour <- color
153
-
154
- n <- max(
155
- length(family ), length(face ), length(colour ), length(size ),
156
- length(hjust ), length(vjust ), length(angle ), length(lineheight )
157
- )
158
- if (n > 1 ) {
159
- cli :: cli_warn(c(
160
- " Vectorized input to {.fn element_text} is not officially supported." ,
161
- " i" = " Results may be unexpected or may change in future versions of ggplot2."
162
- ))
163
- }
164
-
165
-
166
- structure(
167
- list (family = family , face = face , colour = colour , size = size ,
156
+ element_text <- S7 :: new_class(
157
+ " element_text" , parent = element ,
158
+ properties = element_props [c(
159
+ " family" , " face" , " colour" , " size" , " hjust" , " vjust" , " angle" , " lineheight" ,
160
+ " margin" , " debug" , " inherit.blank"
161
+ )],
162
+ constructor = function (family = NULL , face = NULL , colour = NULL ,
163
+ size = NULL , hjust = NULL , vjust = NULL , angle = NULL ,
164
+ lineheight = NULL , color = NULL , margin = NULL ,
165
+ debug = NULL , inherit.blank = FALSE ) {
166
+ n <- max(
167
+ length(family ), length(face ), length(colour ), length(size ),
168
+ length(hjust ), length(vjust ), length(angle ), length(lineheight )
169
+ )
170
+ if (n > 1 ) {
171
+ cli :: cli_warn(c(
172
+ " Vectorized input to {.fn element_text} is not officially supported." ,
173
+ " i" = " Results may be unexpected or may change in future versions of ggplot2."
174
+ ))
175
+ }
176
+
177
+ colour <- color %|| % colour
178
+ S7 :: new_object(
179
+ S7 :: S7_object(),
180
+ family = family , face = face , colour = colour , size = size ,
168
181
hjust = hjust , vjust = vjust , angle = angle , lineheight = lineheight ,
169
- margin = margin , debug = debug , inherit.blank = inherit.blank ),
170
- class = c( " element_text " , " element " )
171
- )
172
- }
182
+ margin = margin , debug = debug , inherit.blank = inherit.blank
183
+ )
184
+ }
185
+ )
173
186
174
187
# ' @export
175
188
# ' @rdname element
176
- element_polygon <- function (fill = NULL , colour = NULL , linewidth = NULL ,
177
- linetype = NULL , color = NULL ,
178
- inherit.blank = FALSE ) {
179
- structure(
180
- list (
189
+ element_polygon <- S7 :: new_class(
190
+ " element_polygon" , parent = element ,
191
+ properties = element_props [c(
192
+ " fill" , " colour" , " linewidth" , " linetype" , " inherit.blank"
193
+ )],
194
+ constructor = function (fill = NULL , colour = NULL , linewidth = NULL ,
195
+ linetype = NULL , color = NULL , inherit.blank = FALSE ) {
196
+ colour <- color %|| % colour
197
+ S7 :: new_object(
198
+ S7 :: S7_object(),
181
199
fill = fill , colour = color %|| % colour , linewidth = linewidth ,
182
200
linetype = linetype , inherit.blank = inherit.blank
183
- ),
184
- class = c(" element_polygon" , " element" )
185
- )
186
- }
201
+ )
202
+ }
203
+ )
187
204
188
205
# ' @export
189
206
# ' @rdname element
190
- element_point <- function (colour = NULL , shape = NULL , size = NULL , fill = NULL ,
191
- stroke = NULL , color = NULL , inherit.blank = FALSE ) {
192
- structure(
193
- list (
207
+ element_point <- S7 :: new_class(
208
+ " element_point" , parent = element ,
209
+ properties = rename(
210
+ element_props [c(
211
+ " colour" , " shape" , " size" , " fill" , " linewidth" , " inherit.blank"
212
+ )],
213
+ c(" linewidth" = " stroke" )
214
+ ),
215
+ constructor = function (colour = NULL , shape = NULL , size = NULL , fill = NULL ,
216
+ stroke = NULL , color = NULL , inherit.blank = FALSE ) {
217
+ S7 :: new_object(
218
+ S7 :: S7_object(),
194
219
colour = color %|| % colour , fill = fill , shape = shape , size = size ,
195
220
stroke = stroke , inherit.blank = inherit.blank
196
- ),
197
- class = c(" element_point" , " element" )
198
- )
199
- }
221
+ )
222
+ }
223
+ )
200
224
201
225
# ' @param ink Foreground colour.
202
226
# ' @param paper Background colour.
203
227
# ' @param accent Accent colour.
204
228
# ' @export
205
229
# ' @rdname element
206
- element_geom <- function (
207
- # colours
208
- ink = NULL , paper = NULL , accent = NULL ,
209
- # linewidth
210
- linewidth = NULL , borderwidth = NULL ,
211
- # linetype
212
- linetype = NULL , bordertype = NULL ,
213
- # text
214
- family = NULL , fontsize = NULL ,
215
- # points
216
- pointsize = NULL , pointshape = NULL ) {
217
-
218
- if (! is.null(fontsize )) {
219
- fontsize <- fontsize / .pt
220
- }
221
-
222
- structure(
223
- list (
224
- ink = ink ,
225
- paper = paper ,
226
- accent = accent ,
230
+ element_geom <- S7 :: new_class(
231
+ " element_geom" , parent = element ,
232
+ properties = list (
233
+ ink = element_props $ colour ,
234
+ paper = element_props $ colour ,
235
+ accent = element_props $ colour ,
236
+ linewidth = element_props $ linewidth ,
237
+ borderwidth = element_props $ linewidth ,
238
+ linetype = element_props $ linetype ,
239
+ bordertype = element_props $ linetype ,
240
+ family = element_props $ family ,
241
+ fontsize = element_props $ size ,
242
+ pointsize = element_props $ size ,
243
+ pointshape = element_props $ shape
244
+ ),
245
+ constructor = function (
246
+ ink = NULL , paper = NULL , accent = NULL ,
247
+ linewidth = NULL , borderwidth = NULL ,
248
+ linetype = NULL , bordertype = NULL ,
249
+ family = NULL , fontsize = NULL ,
250
+ pointsize = NULL , pointshape = NULL ) {
251
+
252
+ if (! is.null(fontsize )) {
253
+ fontsize <- fontsize / .pt
254
+ }
255
+
256
+ S7 :: new_object(
257
+ S7 :: S7_object(),
258
+ ink = ink , paper = paper , accent = accent ,
227
259
linewidth = linewidth , borderwidth = borderwidth ,
228
260
linetype = linetype , bordertype = bordertype ,
229
261
family = family , fontsize = fontsize ,
230
262
pointsize = pointsize , pointshape = pointshape
231
- ),
232
- class = c(" element_geom" , " element" )
233
- )
234
- }
263
+ )
264
+ }
265
+ )
235
266
236
267
.default_geom_element <- element_geom(
237
268
ink = " black" , paper = " white" , accent = " #3366FF" ,
@@ -243,11 +274,7 @@ element_geom <- function(
243
274
244
275
# ' @export
245
276
# ' @rdname is_tests
246
- is.theme_element <- function (x ) inherits(x , " element" )
247
-
248
- # ' @export
249
- print.element <- function (x , ... ) utils :: str(x )
250
-
277
+ is.theme_element <- function (x ) S7 :: S7_inherits(x , element )
251
278
252
279
# ' @param x A single number specifying size relative to parent element.
253
280
# ' @rdname element
0 commit comments