Skip to content
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

Closed
Yunuuuu opened this issue Oct 10, 2024 · 7 comments
Closed

ggproto: Coord cannot be inherited #6133

Yunuuuu opened this issue Oct 10, 2024 · 7 comments

Comments

@Yunuuuu
Copy link

Yunuuuu commented Oct 10, 2024

I don't know if this should be a valid way to change the internal ggproto value (avoid modifying the data in place)

library(ggplot2)
p <- ggplot(mtcars) +
    geom_point(aes(wt, mpg))
# I want to ensure the coordinate always has clip = "off"
if (!identical(p$coordinates$clip, "off")) {
    # to prevent from changing the input of user.
    p$coordinates <- ggproto(NULL, p$coordinates, clip = "off")
}
print(p)
#> Error in super() : node stack overflow

Created on 2024-10-11 with reprex v2.1.0
~

@teunbrand
Copy link
Collaborator

I've found that ggproto() sometimes has problems with self-references. If you make a temporary variable and use that to construct a new ggproto object it should work.

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

@clauswilke
Copy link
Member

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

@teunbrand
Copy link
Collaborator

teunbrand commented Oct 10, 2024

For everyday interactive use, I don't think there is a lot wrong with that code.
However, when you're writing functions you can run into weird bugs because ggproto objects are not copy-on-modify:

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

@Yunuuuu
Copy link
Author

Yunuuuu commented Oct 10, 2024

Thanks! the method provided by @teunbrand works.

@clauswilke I just don't want to change the user input, when we set the clip directly, it change the input p.

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
~
~
~

@clauswilke
Copy link
Member

I see. Yes, if you need a new copy then you need to make a copy.

@teunbrand
Copy link
Collaborator

The behaviour is documented in ?ggproto under 'working with ggproto classes', so I don't think there is a lot to improve here. As such, I'd like to propose to close this issue.

@Yunuuuu
Copy link
Author

Yunuuuu commented Oct 10, 2024

It's okay for me to close this issue, thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants