-
Notifications
You must be signed in to change notification settings - Fork 991
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
names(.SD) should work #4163
names(.SD) should work #4163
Conversation
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.
my vote is for only names(.SD)
in LHS and not .SD
even if we decide to use just .SD
on LHS then still names(.SD)
should works as well
Thank you, Jan. I am working on better test cases that do not involve logicals. I will update tonight. I also noticed that #4031 would be partially addressed:
I will try to substitute |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #4163 +/- ##
=======================================
Coverage 97.50% 97.50%
=======================================
Files 80 80
Lines 14884 14884
=======================================
Hits 14513 14513
Misses 371 371 ☔ View full report in Codecov by Sentry. |
This should be good to go unless we want to support Regarding codecov, I am not sure what to do. There are 10 new tests to address 7 lines of new code plus all previous tests using |
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.
Some minor comments.
Support for length(.SD)
, ncol(.SD)
would be nice but that would generally require a different approach, probably an temporary object relating to subset of self. Current approach is fine for most of use cases.
Thing to decide IMO is if we really want to support .SD
on LHS which breaks consistency about column names there. My vote goes for an extra names()
to be necessary, so we maintain consistency in API.
We also have .SD
related vignette so would be good to put some examples there, but that we can always do as a follow-up.
Should be good again. The .SD vignette could be updated with a few items such as But there are two examples that this and Michael's recent PR will work great with:
|
I may have went too far. There's no use of ```(cols) := ...``` now but there is at least a reference to the other vignette.
Friendly ping :) would be great to get this merged! |
I will make changes tonight or by the end of tomorrow night. Thanks for going through everything. Cole |
I will give the vignette's another look tomorrow and give you a final ping when I'm done. |
R/data.table.R
Outdated
for (i in 2:length(e)) if (!is.null(e[[i]])) e[[i]] = replace_names_sd(e[[i]], cols) | ||
e | ||
} | ||
lhs = eval(replace_names_sd(lhs, sdvars), parent.frame(), parent.frame()) |
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.
A simpler implementation might be the following:
e <- copyenv(parent.frame()) # pseudocode
e$.SD <- setNames(logical(length(sdvars)), sdvars) # or vector("list"), or even vector("raw") to really scrimp on storage
lhs = eval(lhs, e, e)
WDYT?
#### -- How can we update multiple existing columns in place using `.SD`? | ||
|
||
```{r} | ||
flights[, names(.SD) := lapply(.SD, as.factor), .SDcols = is.character] |
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.
Something I can imagine will happen soon is a user trying:
x[, names(.SD)[1:5] := ...]
Does that work already? If so, please add a test. If not, no need to handle it until it's requested later unless you see an easy fix.
Looks great! Last thing is checking if we can simplify implementation / possibly make it much more general. |
I attempted to make it more general per your suggestion. I copied the implementation below. All the tests pass and it supports something like It's easy to revert back or to go down a route you suggested as well. Let me know and I add a # i.e lhs is names(.SD) || setdiff(names(.SD), cols) || (cols)
lhs = eval(lhs, list(.SD = setNames(logical(length(sdvars)), sdvars)), parent.frame()) |
I'm a personal fan of the ability to use |
New version looks great, awesome! |
Almost 10 years later, it's a one-line change 😎 Great stuff! |
Closes #795. Towards #3189.
This is the proposed implementation:
assign
helpTO DO after release:
Update StackOverflow post to mirror updated .SD vignette.