Skip to content

Commit 887ee9e

Browse files
committed
Doc tweaks for programming/extending
1 parent a7d8b3d commit 887ee9e

File tree

11 files changed

+173
-87
lines changed

11 files changed

+173
-87
lines changed

R/aes.r

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,25 @@ is_position_aes <- function(vars) {
103103
aes_to_scale(vars) %in% c("x", "y")
104104
}
105105

106-
#' Define aesthetic mappings from strings, or quoted calls and formulas.
106+
#' Define aesthetic mappings programatically
107107
#'
108108
#' Aesthetic mappings describe how variables in the data are mapped to visual
109109
#' properties (aesthetics) of geoms. \code{\link{aes}} uses non-standard
110110
#' evaluation to capture the variable names. \code{aes_} and \code{aes_string}
111111
#' require you to explicitly quote the inputs either with \code{""} for
112112
#' \code{aes_string()}, or with \code{quote} or \code{~} for \code{aes_()}.
113-
#' (\code{aes_q} is an alias to \code{aes_})
114-
#'
115-
#' It's better to use \code{aes_q()}, because there's no easy way to create the
116-
#' equivalent to \code{aes(colour = "my colour")} or \code{aes{x = `X$1`}}
117-
#' with \code{aes_string()}.
113+
#' (\code{aes_q} is an alias to \code{aes_}). This makes \code{aes_} and
114+
#' \code{aes_string} easy to program with.
118115
#'
119116
#' \code{aes_string} and \code{aes_} are particularly useful when writing
120117
#' functions that create plots because you can use strings or quoted
121118
#' names/calls to define the aesthetic mappings, rather than having to use
122119
#' \code{\link{substitute}} to generate a call to \code{aes()}.
123120
#'
121+
#' I recommend using \code{aes_()}, because creating the equivalents of
122+
#' \code{aes(colour = "my colour")} or \code{aes{x = `X$1`}}
123+
#' with \code{aes_string()} is quite clunky.
124+
#'
124125
#' @param x,y,... List of name value pairs. Elements must be either
125126
#' quoted calls, strings, one-sided formulas or constants.
126127
#' @seealso \code{\link{aes}}

R/ggproto.r

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#' Create a new ggproto object
22
#'
3-
#' ggproto is inspired by the proto package, but it has some important
4-
#' differences. Notably, it cleanly supports cross-package inheritance, and has
5-
#' faster performance.
3+
#' Construct a new object with \code{ggproto}, test with \code{is.proto},
4+
#' and access parent methods/fields with \code{ggproto_parent}.
65
#'
7-
#' @section Calling ggproto methods:
6+
#' ggproto implements a protype based OO system which blurs the lines between
7+
#' classes and instances. It is inspired by the proto package, but it has some
8+
#' important differences. Notably, it cleanly supports cross-package
9+
#' inheritance, and has faster performance.
810
#'
11+
#' In most cases, creating a new OO system to be used by a single package is
12+
#' not a good idea. However, it was the least-bad solution for ggplot2 because
13+
#' it required the fewest changes to an already complex code base.
14+
#'
15+
#' @section Calling methods:
916
#' ggproto methods can take an optional \code{self} argument: if it is present,
1017
#' it is a regular method; if it's absent, it's a "static" method (i.e. it
1118
#' doesn't use any fields).
@@ -17,18 +24,36 @@
1724
#' in the function signature, although customarily it comes first.
1825
#'
1926
#' @section Calling methods in a parent:
20-
#'
2127
#' To explicitly call a methods in a parent, use
2228
#' \code{ggproto_parent(Parent, self)}.
2329
#'
2430
#' @param _class Class name to assign to the object. This is stored as the class
25-
#' attribute of the object. If \code{NULL} (the default), no class name will
26-
#' be added to the object.
27-
#' @param _inherit ggproto object to inherit from. If \code{NULL}, don't inherit
28-
#' from any object.
29-
#' @param parent,self Access parent class \code{parent} of object \code{self}.
31+
#' attribute of the object. This is optional: if \code{NULL} (the default),
32+
#' no class name will be added to the object.
33+
#' @param _inherit ggproto object to inherit from. If \code{NULL}, don't
34+
#' inherit from any object.
3035
#' @param ... A list of members in the ggproto object.
3136
#' @export
37+
#' @examples
38+
#' Adder <- ggproto("Adder",
39+
#' x = 0,
40+
#' add = function(self, n) {
41+
#' self$x <- self$x + n
42+
#' self$x
43+
#' }
44+
#' )
45+
#' is.ggproto(Adder)
46+
#'
47+
#' Adder$add(10)
48+
#' Adder$add(10)
49+
#'
50+
#' Doubler <- ggproto("Doubler", Adder,
51+
#' add = function(self, n) {
52+
#' ggproto_parent(Adder, self)$add(n * 2)
53+
#' }
54+
#' )
55+
#' Doubler$x
56+
#' Doubler$add(10)
3257
ggproto <- function(`_class` = NULL, `_inherit` = NULL, ...) {
3358
e <- new.env(parent = emptyenv())
3459

@@ -65,10 +90,17 @@ ggproto <- function(`_class` = NULL, `_inherit` = NULL, ...) {
6590
e
6691
}
6792

68-
#' Is an object a ggproto object?
69-
#'
93+
94+
#' @export
95+
#' @rdname ggproto
96+
#' @param parent,self Access parent class \code{parent} of object \code{self}.
97+
ggproto_parent <- function(parent, self) {
98+
structure(list(parent = parent, self = self), class = "ggproto_parent")
99+
}
100+
70101
#' @param x An object to test.
71102
#' @export
103+
#' @rdname ggproto
72104
is.ggproto <- function(x) inherits(x, "ggproto")
73105

74106
fetch_ggproto <- function(x, name) {
@@ -98,12 +130,6 @@ fetch_ggproto <- function(x, name) {
98130
res
99131
}
100132

101-
#' @export
102-
#' @rdname ggproto
103-
ggproto_parent <- function(parent, self) {
104-
structure(list(parent = parent, self = self), class = "ggproto_parent")
105-
}
106-
107133
#' @export
108134
`$.ggproto` <- function(x, name) {
109135
res <- fetch_ggproto(x, name)
@@ -140,7 +166,6 @@ make_proto_method <- function(self, f) {
140166
fun
141167
}
142168

143-
144169
#' @export
145170
`[[.ggproto` <- `$.ggproto`
146171

@@ -153,6 +178,7 @@ make_proto_method <- function(self, f) {
153178
#' the returned list. If \code{FALSE}, do not include any inherited items.
154179
#' @param ... Further arguments to pass to \code{as.list.environment}.
155180
#' @export
181+
#' @keywords internal
156182
as.list.ggproto <- function(x, inherit = TRUE, ...) {
157183
res <- list()
158184

@@ -169,7 +195,7 @@ as.list.ggproto <- function(x, inherit = TRUE, ...) {
169195
}
170196

171197

172-
#' Print a ggproto object
198+
#' Format or print a ggproto object
173199
#'
174200
#' If a ggproto object has a \code{$print} method, this will call that method.
175201
#' Otherwise, it will print out the members of the object, and optionally, the
@@ -182,6 +208,14 @@ as.list.ggproto <- function(x, inherit = TRUE, ...) {
182208
#' will be passed to it. Otherwise, these arguments are unused.
183209
#'
184210
#' @export
211+
#' @examples
212+
#' Dog <- ggproto(
213+
#' print = function(self, n) {
214+
#' cat("Woof!\n")
215+
#' }
216+
#' )
217+
#' Dog
218+
#' cat(format(Dog), "\n")
185219
print.ggproto <- function(x, ..., flat = TRUE) {
186220
if (is.function(x$print)) {
187221
x$print(...)
@@ -193,10 +227,8 @@ print.ggproto <- function(x, ..., flat = TRUE) {
193227
}
194228

195229

196-
#' Format a ggproto object
197-
#'
198-
#' @inheritParams print.ggproto
199230
#' @export
231+
#' @rdname print.ggproto
200232
format.ggproto <- function(x, ..., flat = TRUE) {
201233
classes_str <- function(obj) {
202234
classes <- setdiff(class(obj), "ggproto")

R/plot.r

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ plot_clone <- function(plot) {
121121
#' @export
122122
is.ggplot <- function(x) inherits(x, "ggplot")
123123

124-
#' Draw plot on current graphics device.
124+
#' Explicitly draw plot
125+
#'
126+
#' Generally, you do not need to print or plot a ggplot2 plot explicitly: the
127+
#' default top-level print method will do it for you. You will, however, need
128+
#' to call \code{print()} explicitly if you want to draw a plot inside a
129+
#' function or for loop.
125130
#'
126131
#' @param x plot to display
127132
#' @param newpage draw new (empty) page first?
@@ -133,6 +138,20 @@ is.ggplot <- function(x) inherits(x, "ggplot")
133138
#' information about the scales, panels etc.
134139
#' @export
135140
#' @method print ggplot
141+
#' @examples
142+
#' colours <- list(~class, ~drv, ~fl)
143+
#'
144+
#' # Doesn't seem to do anything!
145+
#' for (colour in colours) {
146+
#' ggplot(mpg, aes_(~ displ, ~ hwy, colour = colour)) +
147+
#' geom_point()
148+
#' }
149+
#'
150+
#' # Works when we explicitly print the plots
151+
#' for (colour in colours) {
152+
#' print(ggplot(mpg, aes_(~ displ, ~ hwy, colour = colour)) +
153+
#' geom_point())
154+
#' }
136155
print.ggplot <- function(x, newpage = is.null(vp), vp = NULL, ...) {
137156
set_last_plot(x)
138157
if (newpage) grid.newpage()

_pkgdown.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,21 @@ reference:
139139
- margin
140140

141141
- title: Programming with ggplot2
142+
desc: >
143+
These functions provides tools to help you program with ggplot2,
144+
creating functions and for-loops that generate plots for you.
142145
contents:
143146
- aes_
144147
- print.ggplot
145148

146149
- title: Extending ggplot2 with ggproto
150+
desc: >
151+
To create your own geoms, stats, scales, and facets, you'll need to learn
152+
a bit about the object oriented system that ggplot2 uses. Start by
153+
reading `vignette("extending-ggplot2")` then consult these functions
154+
for more details.
147155
contents:
148156
- ggproto
149-
- as.list.ggproto
150-
- is.ggproto
151-
- format.ggproto
152157
- print.ggproto
153158

154159
- title: Autoplot and fortify

man/aes_.Rd

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/as.list.ggproto.Rd

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

man/format.ggproto.Rd

Lines changed: 0 additions & 21 deletions
This file was deleted.

man/ggproto.Rd

Lines changed: 42 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)