-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ggproto: Coord cannot be inherited #6133
Comments
I've found that library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(wt, mpg))
if (!identical(p$coordinates$clip, "off")) {
old_coord <- p$coordinates
p$coordinates <- ggproto(NULL, old_coord, clip = "off")
}
print(p) Created on 2024-10-10 with reprex v2.1.1 |
Is there anything wrong with the following code? Not sure why a new deep copy is needed. library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(wt, mpg))
p$coordinates$clip <- "off"
p Created on 2024-10-10 with reprex v2.0.2 |
For everyday interactive use, I don't think there is a lot wrong with that code. library(ggplot2)
fun <- function(p) {
p$coordinates$clip <- "off"
p
}
main <- ggplot(mtcars) +
geom_point(aes(wt, mpg))
main$coordinates$clip
#> [1] "on"
derived <- fun(main)
# This is not conform copy-on-modify
main$coordinates$clip
#> [1] "off" Created on 2024-10-10 with reprex v2.1.1 |
Thanks! the method provided by @teunbrand works. @clauswilke I just don't want to change the user input, when we set the knitr::opts_knit$set(upload.fun = identity)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(wt, mpg))
# if user input the `p`
set_clip <- function(p) {
p$coordinates$clip <- "off"
p
}
old_clip <- p$coordinates$clip
set_clip(p) new_clip <- p$coordinates$clip
identical(old_clip, new_clip)
#> [1] FALSE Created on 2024-10-11 with reprex v2.1.0 |
I see. Yes, if you need a new copy then you need to make a copy. |
The behaviour is documented in |
It's okay for me to close this issue, thanks for your help. |
I don't know if this should be a valid way to change the internal
ggproto
value (avoid modifying the data in place)Created on 2024-10-11 with reprex v2.1.0
~
The text was updated successfully, but these errors were encountered: