You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is really a minor thing, but the [[.ggproto() function has name as the index argument instead of i.
This is fine for everyday use of [[-indexing, but becomes a mild annoyance when trying to programmatically extract something from a list of ggproto objects.
library(ggplot2)
# Simple function for extracting the same thing from a list of thingsextract<-function(x, what) {
lapply(x, `[[`, i=what)
}
# Works finex<-list(list(limits= c(NA, NA)), list(limits= c("A", "B")))
extract(x, "limits")
#> [[1]]#> [1] NA NA#> #> [[2]]#> [1] "A" "B"x<-list(
scale_x_continuous(limits= c(NA, NA)),
scale_color_brewer(limits= c("A", "B"))
)
extract(x, "limits")
#> Error in `[[.ggproto`(X[[i]], ...): unused argument (i = "limits")# Can fix this by changing `i` to `name`
lapply(x, `[[`, name="limits")
#> [[1]]#> [1] NA NA#> #> [[2]]#> [1] "A" "B"
Since the [[ primitive ignores the names of arguments and only considers position, I'd think the correct idiom in both cases would be lapply(x, `[[`, what)?
Activity
mjskay commentedon Oct 14, 2023
Since the
[[primitive ignores the names of arguments and only considers position, I'd think the correct idiom in both cases would belapply(x, `[[`, what)?(which is not to say that the index argument name shouldn't be
i)teunbrand commentedon Mar 21, 2024
Yeah you make a fair point, let's just close this.