Skip to content

Closes #592 -- allows for incomplete specification of new order in setcolorder - #1792

Merged
mattdowle merged 1 commit into
Rdatatable:masterfrom
MichaelChirico:setcolorder
Aug 7, 2017
Merged

mattdowle merged 1 commit into
Rdatatable:masterfrom
MichaelChirico:setcolorder

Conversation

@MichaelChirico

Copy link
Copy Markdown
Member

Basic idea is simple -- if setcolorder gets its new 3rd argument, manipulate that into something that the old setcolorder would have understood and continue.

@jangorecki

Copy link
Copy Markdown
Member

@MichaelChirico can't just neworder move those columns to front in provided order, then all others. No new args, and no breaking changes as we put new feature in place of error Error in setcolorder(dt, "b") : neworder is length 1 but x has 2 columns..

@MichaelChirico

Copy link
Copy Markdown
Member Author

@jangorecki that seems reasonable. Approach in this PR represents a more general new feature, but as you point out, may be tougher to merge into current paradigm. If people actually ask for that, may be worth revisiting. Given that issue has little apparent traffic, your suggestion probably covers vast majority of empirical use cases.

Will edit this PR and resubmit.

@codecov-io

codecov-io commented Dec 5, 2016 •

Copy link
Copy Markdown

Current coverage is 90.21% (diff: 100%)

Merging #1792 into master will increase coverage by 0.02%

@@             master      #1792   diff @@
==========================================
  Files            58         58          
  Lines         11141      11147     +6   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          10048      10056     +8   
+ Misses         1093       1091     -2   
  Partials          0          0          

Powered by Codecov. Last update 6706882...32aab12

Comment thread inst/tests/tests.Rraw Outdated

@jangorecki jangorecki Dec 5, 2016 •

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.

this should be c,a,b isn't it? OK this is already after setcolorder above 👍

@jangorecki jangorecki Dec 5, 2016 •

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.

would be nice to have tests and example for vector neworder, eventually handling NAs or duplicates (setdiff will filter out duplicates).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll just change one of the tests to be length>1. the other tests are good but I guess aren't new to this PR...

@jangorecki jangorecki Dec 5, 2016 •

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.

yeah, but your chunk changes behavior of the next chunk, setdiff will remove duplicates so later validation will not raise error when it should. should be fine as we still check on names(x)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually setcolorder(DT, c(2, 2, 3)) (e.g.) causes erroneous behavior. But there's a simple fix. Will push momentarily. Thanks for the extra eyes.

…matically

amend tests/implementation per jan

@mattdowle mattdowle 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.

This provides the ability, but what if a user meant to reorder all columns, but accidentally didn't provide enough columns? That's a nice error currently which catches the mistake. This PR would lose that check, iiuc.

I'm thinking a new function setcolfirst() would be clearer. That way when reading the code, we see what the writer intended more easily.

DT = data.table(a=1:3, b=4:6, c=7:9)
DT
a b c
1: 1 4 7
2: 2 5 8
3: 3 6 9
setcolorder(DT,"b")
Error in setcolorder(DT, "b") : neworder is length 1 but x has 3 columns.

That error message could change to :

Error in setcolorder(DT, "b") : neworder is length 1 but x has 3 columns. Consider setcolfirst(DT, "b") instead.

@jangorecki

jangorecki commented Dec 5, 2016 •

Copy link
Copy Markdown
Member

@mattdowle why not just new argument then? we used to not create new functions for new stuff if it is not really needed, setcolorder is still good name for function to reorder partially.

@mattdowle

mattdowle commented Dec 5, 2016 •

Copy link
Copy Markdown
Member

Maybe new argument. Looking more into it, what happened to my original request of not just moving to the beginning but moving a column to a particular position? The title of #592 (that I filed on R-Forge in 2013, so I remember wanting the ability for some reason at the time) is "Add new movecol(DT, "colname", 1) ". This PR doesn't close that request because it always moves to the beginning.
Perhaps setcolfirst() is too restrictive, maybe setcolpos() then?

setcolpos(DT, "b", 1)   # move b first
setcolpos(DT, c("b", "d"), 1)  # move b and d first
setcolpos(DT, c("b","d"), 10)  # move b and d to be positions 10 and 11.
setcolpos(DT, c("b","d"), before="someCol")   # move b and d to be just before someCol.

@MichaelChirico

Copy link
Copy Markdown
Member Author

That was how I originally programmed it, as a new argument to setcolorder (so it works like setnames). I unfortunately just overwrote that to make jan's adjustment.

Problem with that PR was that I used new argument names which made more sense for the more robust version of setcolorder, but which would have broken old code.

@mattdowle

Copy link
Copy Markdown
Member

I see now - yes comparison to setnames is helpful. What would have broken? setnames checks missing(new) first and then takes old as if it were new. Perhaps it could be done in the same way for setcolorder so that nothing breaks.

@MichaelChirico

MichaelChirico commented Dec 5, 2016 •

Copy link
Copy Markdown
Member Author

Given the analogy/parallel use, perhaps we should change the setcolorder arg names to old and new?

Again this gets at the breaking change issue. But perhaps we can continue to accept neworder with warning?

(as it stands, the current argument is already neworder when we'd like it to be renamed oldorder or old, is basically the issue)

@mattdowle

Copy link
Copy Markdown
Member

Sounds reasonable to me. So it would be setcolorder(DT,old,new,neworder)? If user code contains setcolorder(DT,neworder=o), the warning would ask to either change to new= or no named argument at all. Then in future, remove neworder=. Any existing code such as setcolorder(DT, o) where o is passed unnamed, will continue to work in future and need to be as long as ncol(DT) just like setnames(DT, newnames).
In fact, the new argument could just be called neworder= and then no warning would need to happen. But then we'd be stuck with the old name and inconsistency with setnames. So I'd lean towards named neworder= being a warning and then deprecate it, yes. Think it's quite low risk that users have named that argument and would mind changing.
Jan?

@MichaelChirico

Copy link
Copy Markdown
Member Author

Actually Jan said it broke some of his own code to have different argument names! Since he called neworder specifically.

I do like the consistency with setnames, I think that's why I found this issue in the first place, was trying to use setcolorder like I do setnames.

@jangorecki

Copy link
Copy Markdown
Member

Sorry, I missed that part of PR.
Change of neworder argument breaks (at least) two of my pkgs. Better to avoid argument name changing if it isn't really necessary.
setcolorder(DT,old,new,neworder) is good one

@mattdowle

Copy link
Copy Markdown
Member

Merging as-is. Thought process ...
Looking back with fresh eyes, I was being over-cautious on this one: the convenience of the common case of moving columns to the front outweighs the loss of the (most often, inconvenient, rather than welcomed) error. This PR itself doesn't add any functions or arguments and so won't impact any dependent packages. It's a good low-risk first step. Whether and how to move a subset of columns to a not-first position can be revisited in future. I can't see that accepting this PR now will rule out any options in future.

@mattdowle
mattdowle merged commit 32aab12 into Rdatatable:master Aug 7, 2017
@mattdowle mattdowle added this to the v1.10.6 milestone Aug 7, 2017
@MichaelChirico
MichaelChirico deleted the setcolorder branch August 7, 2017 22:21
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.

4 participants