Skip to content

Commit a7f9152

Browse files
committed
Use S7::method(print) for S7 classes
1 parent 2a672a1 commit a7f9152

File tree

6 files changed

+50
-57
lines changed

6 files changed

+50
-57
lines changed

NAMESPACE

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ S3method(predictdf,default)
7272
S3method(predictdf,glm)
7373
S3method(predictdf,locfit)
7474
S3method(predictdf,loess)
75-
S3method(print,"ggplot2::ggplot")
76-
S3method(print,"ggplot2::mapping")
77-
S3method(print,"ggplot2::theme")
78-
S3method(print,element)
7975
S3method(print,ggplot2_bins)
8076
S3method(print,ggproto)
8177
S3method(print,ggproto_method)

R/aes.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,22 @@ new_aesthetic <- function(x, env = globalenv()) {
131131
}
132132

133133
#' @export
134-
# TODO: should convert to proper S7 method once bug in S7 is resolved
135-
`print.ggplot2::mapping` <- function(x, ...) {
136-
cat("Aesthetic mapping: \n")
137-
138-
if (length(x) == 0) {
139-
cat("<empty>\n")
140-
} else {
141-
values <- vapply(x, quo_label, character(1))
142-
bullets <- paste0("* ", format(paste0("`", names(x), "`")), " -> ", values, "\n")
134+
local({
135+
S7::method(print, class_mapping) <- function(x, ...) {
136+
cat("Aesthetic mapping: \n")
143137

144-
cat(bullets, sep = "")
145-
}
138+
if (length(x) == 0) {
139+
cat("<empty>\n")
140+
} else {
141+
values <- vapply(x, quo_label, character(1))
142+
bullets <- paste0("* ", format(paste0("`", names(x), "`")), " -> ", values, "\n")
146143

147-
invisible(x)
148-
}
144+
cat(bullets, sep = "")
145+
}
149146

147+
invisible(x)
148+
}
149+
})
150150

151151
local({
152152
S7::method(`[`, class_mapping) <- function(x, i, ...) {

R/plot.R

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ is.ggplot <- function(x) {
179179
#' @param ... other arguments not used by this method
180180
#' @keywords hplot
181181
#' @return Invisibly returns the original plot.
182-
#' @export
183-
#' @method print ggplot2::ggplot
184182
#' @name print.ggplot
185-
#' @aliases print.ggplot2::ggplot plot.ggplot2::ggplot
183+
#' @aliases plot.ggplot
186184
#' @examples
187185
#' colours <- c("class", "drv", "fl")
188186
#'
@@ -196,38 +194,38 @@ is.ggplot <- function(x) {
196194
#' print(ggplot(mpg, aes(displ, hwy, colour = .data[[colour]])) +
197195
#' geom_point())
198196
#' }
199-
# TODO: should convert to proper S7 method once bug in S7 is resolved
200-
`print.ggplot2::ggplot` <- function(x, newpage = is.null(vp), vp = NULL, ...) {
201-
set_last_plot(x)
202-
if (newpage) grid.newpage()
203-
204-
# Record dependency on 'ggplot2' on the display list
205-
# (AFTER grid.newpage())
206-
grDevices::recordGraphics(
207-
requireNamespace("ggplot2", quietly = TRUE),
208-
list(),
209-
getNamespace("ggplot2")
210-
)
197+
local({
198+
S7::method(print, class_ggplot) <- S7::method(plot, class_ggplot) <-
199+
function(x, newpage = is.null(vp), vp = NULL, ...) {
200+
set_last_plot(x)
201+
if (newpage) grid.newpage()
211202

212-
data <- ggplot_build(x)
203+
# Record dependency on 'ggplot2' on the display list
204+
# (AFTER grid.newpage())
205+
grDevices::recordGraphics(
206+
requireNamespace("ggplot2", quietly = TRUE),
207+
list(),
208+
getNamespace("ggplot2")
209+
)
213210

214-
gtable <- ggplot_gtable(data)
215-
if (is.null(vp)) {
216-
grid.draw(gtable)
217-
} else {
218-
if (is.character(vp)) seekViewport(vp) else pushViewport(vp)
219-
grid.draw(gtable)
220-
upViewport()
221-
}
211+
data <- ggplot_build(x)
222212

223-
if (isTRUE(getOption("BrailleR.VI")) && rlang::is_installed("BrailleR")) {
224-
print(asNamespace("BrailleR")$VI(x))
225-
}
213+
gtable <- ggplot_gtable(data)
214+
if (is.null(vp)) {
215+
grid.draw(gtable)
216+
} else {
217+
if (is.character(vp)) seekViewport(vp) else pushViewport(vp)
218+
grid.draw(gtable)
219+
upViewport()
220+
}
226221

227-
invisible(x)
228-
}
222+
if (isTRUE(getOption("BrailleR.VI")) && rlang::is_installed("BrailleR")) {
223+
print(asNamespace("BrailleR")$VI(x))
224+
}
229225

230-
S7::method(plot, class_ggplot) <- `print.ggplot2::ggplot`
226+
invisible(x)
227+
}
228+
})
231229

232230
# The following extractors and subassignment operators are for a smooth
233231
# transition and should be deprecated in the release cycle after 4.0.0

R/theme-elements.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,11 @@ element_geom <- S7::new_class(
354354
fill = NULL, colour = NULL
355355
)
356356

357-
#' @export
358-
print.element <- function(x, ...) utils::str(x)
357+
local({
358+
S7::method(print, element) <- function(x, ...) {
359+
utils::str(x)
360+
}
361+
})
359362

360363
#' @export
361364
#' @param type For testing elements: the type of element to expect. One of

R/theme.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ local({
10541054
S7::method(`$`, class_theme) <- function(x, ...) {
10551055
.subset2(x, ...)
10561056
}
1057+
S7::method(print, class_theme) <- function(x, ...) {
1058+
utils::str(x)
1059+
}
10571060
})
1058-
1059-
#' @export
1060-
`print.ggplot2::theme` <- function(x, ...) utils::str(x)

man/print.ggplot.Rd

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

0 commit comments

Comments
 (0)