@@ -154,6 +154,28 @@ test_that("layer names can be resolved", {
154
154
expect_snapshot(p + l + l , error = TRUE )
155
155
})
156
156
157
+ test_that(" attributes on layer data are preserved" , {
158
+ # This is a good layer for testing because:
159
+ # * It needs to compute a statistic at the group level
160
+ # * It needs to setup data to reshape x/y/width/height into xmin/xmax/ymin/ymax
161
+ # * It needs to use a position adjustment
162
+ # * It has an `after_stat()` so it enters the map_statistic method
163
+ old <- stat_summary(
164
+ aes(fill = after_stat(y )),
165
+ fun = mean , geom = " col" , position = " dodge"
166
+ )
167
+ # We modify the compute aesthetics method to append a test attribute
168
+ new <- ggproto(NULL , old , compute_aesthetics = function (self , data , plot ) {
169
+ data <- ggproto_parent(old , self )$ compute_aesthetics(data , plot )
170
+ attr(data , " test" ) <- " preserve me"
171
+ data
172
+ })
173
+ # At the end of plot building, we want to retrieve that metric
174
+ ld <- layer_data(
175
+ ggplot(mpg , aes(drv , hwy , colour = factor (year ))) + new + facet_grid(~ year )
176
+ )
177
+ expect_equal(attr(ld , " test" ), " preserve me" )
178
+ })
157
179
158
180
# Data extraction ---------------------------------------------------------
159
181
0 commit comments