1
+ # ---
2
+ # repo: polkas/ggcall
3
+ # file: ggcall.R
4
+ # last-updated: 2024-11-21
5
+ # license: https://unlicense.org
6
+ # imports: ggplot2
7
+ # ---
8
+ #
9
+ # This file provides a minimal shim to provide a ggcall functionality on top of
10
+ # ggplot2.
11
+ #
12
+ # ## Changelog
13
+ #
14
+
15
+ # nocov start
16
+
1
17
# ' Enhanced `ggplot` Function with History Tracking
2
18
# '
3
19
# ' Overrides the default `ggplot` function from the ggplot2 package, adding the
10
26
# ' attribute 'ggcall' that stores the history of plot construction.
11
27
# '
12
28
# ' @seealso \code{\link[ggplot2]{ggplot}}
13
- # ' @importFrom ggplot2 ggplot
29
+ # ' @rawNamespace import( ggplot2, except = c( ggplot))
14
30
# ' @examples
31
+ # ' library(ggplot2, exclude = "ggplot")
15
32
# ' p <- ggplot(mtcars, aes(x = wt, y = mpg))
16
33
# ' # the + function has to come from ggcall package
17
34
# ' attr(p + geom_point(), "ggcall")
@@ -38,10 +55,12 @@ ggplot <- function(...) {
38
55
# ' conjunction with the enhanced ggplot function provided by this package.
39
56
# '
40
57
# ' @param e1 A ggplot object of class 'ggcall'.
41
- # ' @param e2 A layer or theme to add to the ggplot object .
58
+ # ' @param e2 A layer, theme or ggcall to add .
42
59
# '
43
60
# ' @return A modified ggplot object with updated plot history.
61
+ # ' @rdname ggcall-add-operator
44
62
# ' @examples
63
+ # ' library(ggplot2, exclude = "ggplot")
45
64
# ' p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
46
65
# ' geom_point()
47
66
# ' attr(p, "ggcall") # View the plot call
@@ -92,6 +111,7 @@ ggplot <- function(...) {
92
111
# ' a list representing the history of the ggplot object.
93
112
# '
94
113
# ' @examples
114
+ # ' library(ggplot2, exclude = "ggplot")
95
115
# ' # Example: Create a function which combines a few ggplot layers
96
116
# ' # Typically, it will be a function from your R package where you implemented ggcall
97
117
# ' func <- function(data, x, y, bool = TRUE) {
@@ -112,8 +132,12 @@ ggplot <- function(...) {
112
132
# ' }
113
133
# ' plot_call <- ggcall(func(mtcars, "wt", "mpg"))
114
134
# ' # Optionally: Style the code with styler
115
- # ' styler::style_text(backports:::deparse1(plot_call))
116
- # '
135
+ # ' # deparse1 is recommended and available in R>=4.0.0
136
+ # ' \dontrun{
137
+ # ' styler::style_text(
138
+ # ' paste(deparse(plot_call), collapse = "\n")
139
+ # ' )
140
+ # ' }
117
141
# ' @export
118
142
# '
119
143
ggcall <- function (plot ) {
@@ -143,6 +167,7 @@ ggcall <- function(plot) {
143
167
# ' More complex variables are referenced to ggcall environment.
144
168
# '
145
169
# ' @examples
170
+ # ' library(ggplot2, exclude = "ggplot")
146
171
# ' # Example: Create a function which combines a few ggplot layers
147
172
# ' # Typically, it will be a function from your R package where you implemented ggcall
148
173
# ' func <- function(data, x, y, bool = TRUE) {
@@ -164,14 +189,17 @@ ggcall <- function(plot) {
164
189
# ' plot_call <- ggcall(func(mtcars, "wt", "mpg"))
165
190
# ' # Optionally: Add assignments
166
191
# ' plot_call_with_assignments <- ggcall_add_assignments(plot_call)
192
+ # ' \dontrun{
167
193
# ' styler::style_text(
168
194
# ' paste(deparse(plot_call_with_assignments), collapse = "\n")
169
195
# ' )
170
- # '
196
+ # ' }
171
197
# ' eval_ggcall(plot_call_with_assignments)
172
198
# '
173
199
# ' # Will Fail as data is needed and skipped
174
- # ' # eval_ggcall(ggcall_add_assignments(plot_call, vars = c("x", "y")))
200
+ # ' \dontrun{
201
+ # ' eval_ggcall(ggcall_add_assignments(plot_call, vars = c("x", "y")))
202
+ # ' }
175
203
# ' @export
176
204
ggcall_add_assignments <- function (call , vars = extract_names(call )) {
177
205
stopifnot(inherits(call , " ggcall_code" ))
@@ -232,9 +260,14 @@ ggcall_add_assignments <- function(call, vars = extract_names(call)) {
232
260
# ' @return The resulting ggplot object produced by evaluating the expression `x`.
233
261
# '
234
262
# ' @examples
235
- # ' p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
236
- # ' geom_point()
237
- # ' plot_call <- ggcall(p)
263
+ # ' library(ggplot2, exclude = "ggplot")
264
+ # '
265
+ # ' func <- function() {
266
+ # ' ggplot(mtcars, aes(x = wt, y = mpg)) +
267
+ # ' geom_point()
268
+ # ' }
269
+ # ' gplot <- func()
270
+ # ' plot_call <- ggcall(gplot)
238
271
# ' reconstructed_plot <- eval_ggcall(plot_call)
239
272
# ' print(reconstructed_plot)
240
273
# '
@@ -259,11 +292,13 @@ eval_ggcall <- function(call, ...) {
259
292
# ' @param call An expression representing the ggplot construction code.
260
293
# ' @return The environment in which the ggplot construction code was created.
261
294
# ' @examples
295
+ # ' library(ggplot2, exclude = "ggplot")
262
296
# ' fun <- function(data, x, y) {
263
297
# ' ggplot(data, aes(x = !!as.name(x), y = !!as.name(y))) +
264
298
# ' geom_point()
265
299
# ' }
266
- # ' plot_call <- ggcall(fun(mtcars, "wt", "mpg"))
300
+ # ' gplot <- fun(mtcars, "wt", "mpg")
301
+ # ' plot_call <- ggcall(gplot)
267
302
# ' env <- ggcall_env(plot_call)
268
303
# ' ls(env)
269
304
# ' env[["data"]]
@@ -309,3 +344,5 @@ extract_names <- function(expr) {
309
344
310
345
return (character ())
311
346
}
347
+
348
+ # nocov end
0 commit comments