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

pairs.R: evaluate in parent.frame() (maybe) #1156

Merged
merged 1 commit into from
Mar 11, 2025
Merged

Conversation

jgabry
Copy link
Member

@jgabry jgabry commented Mar 5, 2025

Summary:

Fixes #1155, although this might not be the preferred solution. This PR changes pairs.stanfit so evaluation takes place in the calling environment, which avoids the error in #1155. However, this approach could have unintended consequences, so I'm not sure whether we should actually do this.

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company): Columbia University

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

@jgabry jgabry requested a review from bgoodri March 5, 2025 21:46
@jgabry jgabry changed the title pairs.R: evaluate in parent.frame() pairs.R: evaluate in parent.frame() (maybe) Mar 5, 2025
@iago-lito
Copy link

iago-lito commented Mar 6, 2025

Just to make sure I understand this change correctly. We would transition from:

value <- 5
for (value in 1:3) {
   pairs(fit, arg = value) # <- `value` evaluates to global scope: 5 then 5 then 5.
}

to

value <- 5
for (value in 1:3) {
   pairs(fit, arg = value) # <- `value` evaluates to local scope: 1 then 2 then 3.
}

Is that correct?

If so, then I have a strong opinion that it is the correct fix.

To state this further, and considering that it is a breaking change: my opinion is that any behaviour broken by this change should be retrospectively considered a bug. I think that lexical scoping is a standard in all modern languages because it is the most intuitive to expect. The current behaviour can be the source of numerous, subtle, very hard to diagnose bugs. I think that it is a mistake that R even allows this behaviour with its (too) flexible evaluation control opportunities.

Fortunatly this is only about plotting, and it only impacts cosmetic arguments passed to pairs.stanfit so the stakes are rather low. I don't expect much science to be broken by this quirk ;)

Also worth considering: should we keep the current behaviour, then how would we achieve a loop like the second one above?

@jgabry
Copy link
Member Author

jgabry commented Mar 6, 2025

Is that correct?

Yes, I think that's right.

To state this further, and considering that it is a breaking change: my opinion is that any behaviour broken by this change should be retrospectively considered a bug.

I agree.

Also worth considering: should we keep the current behaviour, then how would we achieve a loop like the second one above?

Yeah, I don't think it would be possible.

@bgoodri
Copy link
Contributor

bgoodri commented Mar 9, 2025

I'll test this and presumably roll it into the release we need to do for unrelated reasons.

@andrjohns
Copy link
Contributor

These changes were included in the CRAN update, so merging - thanks for finding and reporting this @iago-lito!

@andrjohns andrjohns merged commit f9a6372 into develop Mar 11, 2025
9 checks passed
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.

pairs.stanfit delays arguments evaluation in a way that breaks lexical scoping.
4 participants