Skip to content

Commit b47faa0

Browse files
committed
Use S7::method() for gg getters/setters
1 parent 6603eb5 commit b47faa0

File tree

2 files changed

+32
-46
lines changed

2 files changed

+32
-46
lines changed

NAMESPACE

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
S3method("$","ggplot2::gg")
43
S3method("$","ggplot2::theme")
54
S3method("$",ggproto)
65
S3method("$",ggproto_parent)
7-
S3method("$<-","ggplot2::gg")
86
S3method("$<-","ggplot2::mapping")
9-
S3method("[","ggplot2::gg")
107
S3method("[","ggplot2::mapping")
118
S3method("[",mapped_discrete)
12-
S3method("[<-","ggplot2::gg")
139
S3method("[<-","ggplot2::mapping")
1410
S3method("[<-",mapped_discrete)
15-
S3method("[[","ggplot2::gg")
1611
S3method("[[",ggproto)
17-
S3method("[[<-","ggplot2::gg")
1812
S3method("[[<-","ggplot2::mapping")
1913
S3method(.DollarNames,ggproto)
2014
S3method(as.data.frame,mapped_discrete)

R/plot.R

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -231,49 +231,41 @@ S7::method(plot, class_ggplot) <- `print.ggplot2::ggplot`
231231

232232
# The following extractors and subassignment operators are for a smooth
233233
# transition and should be deprecated in the release cycle after 4.0.0
234-
# TODO: should convert to proper S7 method once bug in S7 is resolved
235-
236-
#' @export
237-
`$.ggplot2::gg` <- function(x, i) {
238-
if (!S7::prop_exists(x, i) && S7::prop_exists(x, "meta")) {
239-
# This is a trick to bridge a gap between S3 and S7. We're allowing
240-
# for arbitrary fields by reading/writing to the 'meta' field when the
241-
# index does not point to an actual property.
242-
# The proper way to go about this is to implement new fields as properties
243-
# of a ggplot subclass.
244-
S7::prop(x, "meta")[[i]]
245-
} else {
246-
`[[`(S7::props(x), i)
234+
local({
235+
S7::method(`[[`, class_gg) <- S7::method(`$`, class_gg) <-
236+
function(x, i) {
237+
if (!S7::prop_exists(x, i) && S7::prop_exists(x, "meta")) {
238+
# This is a trick to bridge a gap between S3 and S7. We're allowing
239+
# for arbitrary fields by reading/writing to the 'meta' field when the
240+
# index does not point to an actual property.
241+
# The proper way to go about this is to implement new fields as properties
242+
# of a ggplot subclass.
243+
S7::prop(x, "meta")[[i]]
244+
} else {
245+
`[[`(S7::props(x), i)
246+
}
247+
}
248+
S7::method(`[`, class_gg) <- function(x, i) {
249+
`[`(S7::props(x), i)
247250
}
248-
}
251+
})
249252

250-
#' @export
251-
`$<-.ggplot2::gg` <- function(x, i, value) {
252-
if (!S7::prop_exists(x, i) && S7::prop_exists(x, "meta")) {
253-
# See explanation in `$.ggplot2::gg`
254-
S7::prop(x, "meta")[[i]] <- value
255-
} else {
256-
S7::props(x) <- `[[<-`(S7::props(x), i, value)
253+
local({
254+
S7::method(`$<-`, class_gg) <- S7::method(`[[<-`, class_gg) <-
255+
function(x, i, value) {
256+
if (!S7::prop_exists(x, i) && S7::prop_exists(x, "meta")) {
257+
# See explanation in `$.ggplot2::gg`
258+
S7::prop(x, "meta")[[i]] <- value
259+
} else {
260+
S7::props(x) <- `[[<-`(S7::props(x), i, value)
261+
}
262+
x
263+
}
264+
S7::method(`[<-`, class_gg) <- function(x, i, value) {
265+
S7::props(x) <- `[<-`(S7::props(x), i, value)
266+
x
257267
}
258-
x
259-
}
260-
261-
#' @export
262-
`[.ggplot2::gg` <- function(x, i) {
263-
`[`(S7::props(x), i)
264-
}
265-
266-
#' @export
267-
`[<-.ggplot2::gg` <- function(x, i, value) {
268-
S7::props(x) <- `[<-`(S7::props(x), i, value)
269-
x
270-
}
271-
272-
#' @export
273-
`[[.ggplot2::gg` <- `$.ggplot2::gg`
274-
275-
#' @export
276-
`[[<-.ggplot2::gg` <- `$<-.ggplot2::gg`
268+
})
277269

278270
#' @importFrom S7 convert
279271
# S7 currently attaches the S3 method to the calling environment which gives `ggplot2:::as.list`

0 commit comments

Comments
 (0)