Skip to content

Protection for position scale's call parameter - #5605

Merged
teunbrand merged 3 commits into
tidyverse:rc/3.5.0from
teunbrand:fix_scale_call
Dec 22, 2023
Merged

teunbrand merged 3 commits into
tidyverse:rc/3.5.0from
teunbrand:fix_scale_call

Conversation

@teunbrand

@teunbrand teunbrand commented Dec 21, 2023 •

Copy link
Copy Markdown
Collaborator

This PR to the RC aims to fix a bug uncovered in revdepchecks affecting ~44 packages.

One such example is this:
https://github.com/tidyverse/ggplot2/blob/rc/3.5.0/revdep/problems.md#newly-broken-29

Briefly, if a position scale is invoked in an non-standard way, where the first element of the call is not a name, it provoked an error.
To reproduce this error, the code below causes a function to be the scale's call[[1]]:

library(ggplot2) # RC branch

f <- function() {
  ggplot(mpg, aes(displ, hwy)) +
    geom_point() +
    scale_y_continuous()
}
do.call(f, list())
#> Error in as.character(call[[1]]): cannot coerce type 'closure' to vector of type 'character'

This is now wrapped in a try_fetch() block so that this does not cause a failure. If call[[1]] is not coercible to a character, it is presumed that the scale was not called as part of a scale-wrapper (like scale_y_log10()) and the call gets replaced with current_call().
This circumvents the error:

devtools::load_all("~/packages/ggplot2/") # This PR
#> ℹ Loading ggplot2
do.call(f, list())

Created on 2023-12-21 with reprex v2.0.2

@teunbrand teunbrand added this to the ggplot2 3.5.0 milestone Dec 21, 2023
@teunbrand
teunbrand requested a review from thomasp85 December 21, 2023 11:48
@thomasp85

Copy link
Copy Markdown
Member

Wouldn't it be better to check the type of call[[1]] rather than using a try_fetch?

@teunbrand

Copy link
Copy Markdown
Collaborator Author

I wasn't sure what types call[[1]] could assume. After testing a bit, I think most types, like name can be safely converted to character, but functions cannot.

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand
teunbrand merged commit c700dc8 into tidyverse:rc/3.5.0 Dec 22, 2023
@teunbrand
teunbrand deleted the fix_scale_call branch December 22, 2023 11:45
thomasp85 pushed a commit that referenced this pull request Feb 23, 2024
* protect against exotic calls

* misplaced parenthesis

* circumvent try_fetch
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

Successfully merging this pull request may close these issues.

2 participants