62
62
qplot <- function (x , y = NULL , ... , data , facets = NULL , margins = FALSE ,
63
63
geom = " auto" , xlim = c(NA , NA ),
64
64
ylim = c(NA , NA ), log = " " , main = NULL ,
65
- xlab = deparse(substitute( x )) , ylab = deparse(substitute( y )) ,
65
+ xlab = NULL , ylab = NULL ,
66
66
asp = NA , stat = NULL , position = NULL ) {
67
67
68
- testthat :: skip( " qplot " )
68
+ caller_env <- parent.frame( )
69
69
70
70
if (! missing(stat )) warning(" `stat` is deprecated" , call. = FALSE )
71
71
if (! missing(position )) warning(" `position` is deprecated" , call. = FALSE )
72
72
if (! is.character(geom )) stop(" `geom` must be a character vector" , call. = FALSE )
73
73
74
- argnames <- names(as.list(match.call( expand.dots = FALSE )[ - 1 ]) )
75
- arguments <- as.list(match.call()[ - 1 ] )
76
- env <- parent.frame( )
74
+ exprs <- rlang :: enquos( x = x , y = y , ... )
75
+ is_missing <- vapply( exprs , rlang :: quo_is_missing , logical ( 1 ) )
76
+ is_constant <- vapply( exprs , rlang :: quo_is_call , logical ( 1 ), name = " I " )
77
77
78
- aesthetics <- compact(arguments [.all_aesthetics ])
79
- aesthetics <- aesthetics [! is.constant(aesthetics )]
80
- aes_names <- names(aesthetics )
81
- aesthetics <- rename_aes(aesthetics )
82
- class(aesthetics ) <- " uneval"
78
+ mapping <- new_aes(exprs [! is_missing & ! is_constant ], env = parent.frame())
79
+ consts <- exprs [is_constant ]
80
+
81
+ aes_names <- names(mapping )
82
+ mapping <- rename_aes(mapping )
83
+
84
+
85
+ xlab <- rlang :: quo_name(exprs $ x )
86
+ # Work around quo_name() bug: https://github.com/r-lib/rlang/issues/430
87
+ if (rlang :: quo_is_null(exprs $ y )) {
88
+ ylab <- " NULL"
89
+ } else {
90
+ ylab <- rlang :: quo_name(exprs $ y )
91
+ }
83
92
84
93
if (missing(data )) {
85
94
# If data not explicitly specified, will be pulled from workspace
@@ -89,7 +98,8 @@ qplot <- function(x, y = NULL, ..., data, facets = NULL, margins = FALSE,
89
98
facetvars <- all.vars(facets )
90
99
facetvars <- facetvars [facetvars != " ." ]
91
100
names(facetvars ) <- facetvars
92
- facetsdf <- as.data.frame(mget(facetvars , envir = env ))
101
+ # FIXME?
102
+ facetsdf <- as.data.frame(mget(facetvars , envir = caller_env ))
93
103
if (nrow(facetsdf )) data <- facetsdf
94
104
}
95
105
@@ -98,22 +108,22 @@ qplot <- function(x, y = NULL, ..., data, facets = NULL, margins = FALSE,
98
108
if (" sample" %in% aes_names ) {
99
109
geom [geom == " auto" ] <- " qq"
100
110
} else if (missing(y )) {
101
- x <- eval( aesthetics $ x , data , env )
111
+ x <- rlang :: eval_tidy( mapping $ x , data , caller_env )
102
112
if (is.discrete(x )) {
103
113
geom [geom == " auto" ] <- " bar"
104
114
} else {
105
115
geom [geom == " auto" ] <- " histogram"
106
116
}
107
- if (missing (ylab )) ylab <- " count"
117
+ if (is.null (ylab )) ylab <- " count"
108
118
} else {
109
119
if (missing(x )) {
110
- aesthetics $ x <- bquote (seq_along(.( y )), aesthetics )
120
+ mapping $ x <- rlang :: quo (seq_along(!! mapping $ y ) )
111
121
}
112
122
geom [geom == " auto" ] <- " point"
113
123
}
114
124
}
115
125
116
- p <- ggplot(data , aesthetics , environment = env )
126
+ p <- ggplot(data , mapping , environment = NULL )
117
127
118
128
if (is.null(facets )) {
119
129
p <- p + facet_null()
@@ -127,12 +137,8 @@ qplot <- function(x, y = NULL, ..., data, facets = NULL, margins = FALSE,
127
137
128
138
# Add geoms/statistics
129
139
for (g in geom ) {
130
- # Arguments are unevaluated because some are aesthetics. Need to evaluate
131
- # params - can't do in correct env because that's lost (no lazyeval)
132
- # so do the best we can by evaluating in parent frame.
133
- params <- arguments [setdiff(names(arguments ), c(aes_names , argnames ))]
134
- params <- lapply(params , eval , parent.frame())
135
-
140
+ # We reevaluate constants once per geom for historical reasons?
141
+ params <- lapply(consts , rlang :: eval_tidy )
136
142
p <- p + do.call(paste0(" geom_" , g ), params )
137
143
}
138
144
0 commit comments