-
Notifications
You must be signed in to change notification settings - Fork 106
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
hue_pal should preserve referencial transparency #81
Conversation
0c63fc2
to
15361f3
Compare
Current coverage is 63.18% (diff: 71.69%)@@ master #81 diff @@
==========================================
Files 26 26
Lines 803 853 +50
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 491 539 +48
- Misses 312 314 +2
Partials 0 0
|
I have reviewed the code looking for places where referencial transparency was not honoured, typically this kind of code: function(arg1) {
function(x) {
x + arg1
}
} Had to be changed to: function(arg1) {
force(arg1)
function(x) {
x + arg1
}
} By using Let the |
@hadley, can this PR be reviewed/merged before the next release? Is it too late already to ask this? |
It's too late, sorry. |
I just rebased this PR, in case you want to merge it before the next scales release. |
R/pal-hue.r
Outdated
@@ -23,7 +23,11 @@ | |||
#' show_col(hue_pal(h = c(180, 270))(9)) | |||
#' show_col(hue_pal(h = c(270, 360))(9)) | |||
hue_pal <- function(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1) { | |||
|
|||
h <- force(h) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to assign
R/trans.r
Outdated
@@ -24,7 +24,10 @@ | |||
trans_new <- function(name, transform, inverse, breaks = extended_breaks(), format = format_format(), domain = c(-Inf, Inf)) { | |||
if (is.character(transform)) transform <- match.fun(transform) | |||
if (is.character(inverse)) inverse <- match.fun(inverse) | |||
|
|||
force(name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
list()
will force
R/formatter.r
Outdated
@@ -265,6 +279,8 @@ trans_format <- function(trans, format = scientific_format()) { | |||
#' \code{\link{format.POSIXct}} | |||
#' @export | |||
format_format <- function(...) { | |||
force(list(...)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make force_all <- function(...) list(...)
just to make this and others slightly cleaner?
Thanks, with Note that the github web interface is having some issues, hopefully my last commit will appear here soon. |
R/a-helper.r
Outdated
@@ -0,0 +1,2 @@ | |||
# Evaluates all arguments (see #81) | |||
force_all <- function(...) list(...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put in utils.R
please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I had to use @include utils.r
where needed to make sure the Collate will be well done.
And add a bullet to NEWS? I wouldn't mention referential transparency, but I'm not sure how to describe otherwise. |
This is the current NEWS bullet:
It's not an awesome description, but I hope it is clear enough... |
I don't understand why you needed |
I removed all the It is needed because the |
R/formatter.r
Outdated
@@ -4,6 +4,7 @@ | |||
#' @param x a numeric vector to format | |||
#' @return a function with single parameter x, a numeric vector, that | |||
#' returns a character vector | |||
#' @include utils.r |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary because a bit below (R/formatter.r, line 104
) the function dollar
is defined as follows:
#' @export
#' @rdname dollar_format
dollar <- dollar_format()
So dollar_format()
is called at build time and dollar_format()
calls force_all()
, defined in utils.r
. If you would rather that I put the @include utils.r
in the dollar definition below, feel free to tell me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d rather get rid of the dependency altogether somehow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, I have got rid of it in the last commit :-) : 98c42d0
R/formatter.r
Outdated
@@ -158,6 +162,9 @@ scientific <- function(x, digits = 3, ...) { | |||
#' ordinal(1:10) | |||
|
|||
ordinal_format <- function(x) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this even have an argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, good point. It shouldn't. I am going to remove that argument.
scales now works as expected when it is used inside a for loop. Before, when a scales function was used with a variable custom parameters inside a for loop, some of the parameters were not evaluated until the end of the loop, due to how R lazy evaluation works.
Collate in DESCRIPTION is not required
@hadley, I see you merged master into the PR branch, I expected the merge to go the other way around :-P Anyway, If you need anything from my end feel free to tell me, I don't mind rebasing if you like. The build failure seems unrelated to this PR and only happens in older R versions that I do not have installed right now though... |
Build seems to be failing because of timezone tests in the earlier versions. Since timezones are platform specific this could be a travis/linux specific issue. Not sure how to debug. (Apologies for the late edit, accidently hit comment before I was finished!) Using Rstudio cloud to debug this, R Version 3.1.3:
|
Easiest I think to just skip the test with e.g. |
Hi @zeehio, You'll see all builds are passing now but there are still a couple things we would like to see happen before merging:
Can you make these changes and update the PR? If you need anything from my end, I'm around to help! Thanks! |
Thanks for the review and the indications. I believe I addressed all the points you mentioned, although for the first point I'd like to know what solution you prefer.
The difference between
Thanks for your detailed comments, your review and your time! 😄 |
Why is dollar different to every other function of that form? |
In
So Whether or not it could be deprecated or removed, I have no idea, but I (humbly) feel it goes beyond this PR. Considering that:
I see there are only left two options out of the four I proposed. I still don't understand your reasons for disliking the Collate solution, but I accept your criteria because a) it is your package and b) I trust your judgement (you are Hadley Wickham!). Also, I usually don't ask you for details on your decisions because I don't want to waste (more of) your time with questions that I usually understand with my own reading/work/time. However this time either I get some rationale on what solution you would like or why you aren't convinced by any of the four proposals (if that's the case), or I assume my incompetence to address this issue and surrender this pull request. After all, if in 1.5 years I've been unable to get it merged I am starting to doubt I ever will. In any case, I have learnt a lot during the process and I am very grateful to all of you for that. |
Sorry for being so vague. I've now taken a thorough look at the code and can give you a better path forward. The basic problem, I think, is that comma_format <- function(...) {
function(x) comma(x, ...)
}
comma <- function(x, ...) {
format(x, ..., big.mark = ",", scientific = FALSE, trim = TRUE)
} I think this is a better organisation since the bulk of the code is stored inside a function that you can test directly, rather than via a layer of indirection. I appreciate your patience with this pull request — I know it's taken a very long time and you have been very understanding — if you don't want to work on it anymore, @dpseidel can take it over the finish line. |
Good news, everyone! Following the path you gave me I rewrote I fully agree with you that this approach is better. And thanks to your specific explanation I've learnt a bit about code organization as well. So big thanks again! 😃 If you review the commits, you will also see that I dropped |
Scales 1.0.0 fixed a bug r-lib/scales#81. The bug can lead to wrong plots when they are defined inside for loops.
When
hue_pal
is used inside a for loop, hue_pal arguments may not be evaluated until the end of the loop and referencial transparency is lost.See the failing test case on the first commit:
The proposed patch on the second commit simply uses
force
on the input arguments of hue_pal, fixing the issue.Other palettes may suffer from the same issues. If there is interest in this PR I can fix them as well.