Skip to content

Commit 468563b

Browse files
committed
restore attributes after layer methods
1 parent 3408ea4 commit 468563b

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

R/layer.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,13 @@ Layer <- ggproto("Layer", NULL,
359359
},
360360

361361
compute_statistic = function(self, data, layout) {
362-
if (empty(data))
363-
return(data_frame0())
362+
if (empty(data)) return(data_frame0())
364363

364+
ptype <- vec_ptype(data)
365365
self$computed_stat_params <- self$stat$setup_params(data, self$stat_params)
366366
data <- self$stat$setup_data(data, self$computed_stat_params)
367-
self$stat$compute_layer(data, self$computed_stat_params, layout)
367+
data <- self$stat$compute_layer(data, self$computed_stat_params, layout)
368+
merge_attrs(data, ptype)
368369
},
369370

370371
map_statistic = function(self, data, plot) {
@@ -420,23 +421,25 @@ Layer <- ggproto("Layer", NULL,
420421

421422
compute_geom_1 = function(self, data) {
422423
if (empty(data)) return(data_frame0())
424+
ptype <- vec_ptype(data)
423425

424426
check_required_aesthetics(
425427
self$geom$required_aes,
426428
c(names(data), names(self$aes_params)),
427429
snake_class(self$geom)
428430
)
429431
self$computed_geom_params <- self$geom$setup_params(data, c(self$geom_params, self$aes_params))
430-
self$geom$setup_data(data, self$computed_geom_params)
432+
data <- self$geom$setup_data(data, self$computed_geom_params)
433+
merge_attrs(data, ptype)
431434
},
432435

433436
compute_position = function(self, data, layout) {
434437
if (empty(data)) return(data_frame0())
435-
438+
ptype <- vec_ptype(data)
436439
params <- self$position$setup_params(data)
437440
data <- self$position$setup_data(data, params)
438-
439-
self$position$compute_layer(data, params, layout)
441+
data <- self$position$compute_layer(data, params, layout)
442+
merge_attrs(data, ptype)
440443
},
441444

442445
compute_geom_2 = function(self, data, params = self$aes_params, theme = NULL, ...) {

R/utilities.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ toupper <- function(x) {
249249
cli::cli_abort("Please use {.fn to_upper_ascii}, which works fine in all locales.")
250250
}
251251

252+
merge_attrs <- function(new, old) {
253+
new_attr <- attributes(new)
254+
new <- vec_restore(new, old) # copies old attributes to new
255+
new_attr <- new_attr[setdiff(names(new_attr), names(attributes(new)))]
256+
attributes(new) <- c(attributes(new), new_attr)
257+
new
258+
}
259+
252260
# Convert a snake_case string to camelCase
253261
camelize <- function(x, first = FALSE) {
254262
x <- gsub("_(.)", "\\U\\1", x, perl = TRUE)

0 commit comments

Comments
 (0)