-
Notifications
You must be signed in to change notification settings - Fork 979
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
Clarify T/F
versus TRUE/FALSE
usage in j
argument with new FAQ entry*
#6196
Conversation
Does |
don't know much about them other than them being abbreviation but here is some info i found online Historical Context: In early versions of S (the precursor to R), T and F were introduced as constants representing TRUE and FALSE, respectively. This convention has been carried over into R for consistency and familiarity with users who have experience with S. Compatibility: Some packages or legacy code might still use T and F instead of TRUE and FALSE. Therefore, maintaining compatibility with these conventions ensures that older code continues to work seamlessly in newer R environments. |
vignettes/datatable-faq.Rmd
Outdated
@@ -460,6 +460,10 @@ Happily, an internet search for "How does R method dispatch work" (at the time o | |||
|
|||
However, features like basic S3 dispatch (pasting the function name together with the class name) is why some R folk love R. It's so simple. No complicated registration or signature is required. There isn't much needed to learn. To create the `merge` method for data.table all that was required, literally, was to merely create a function called `merge.data.table`. | |||
|
|||
## Why do `T` and `F` behave differently from `TRUE` and `FALSE` in `data.table`? | |||
|
|||
In R, `T` and `F` are global variables that default to `TRUE` and `FALSE`, respectively. However, they can be redefined in the user environment, leading to unexpected behavior. The `data.table` package might treat `T` and `F` as variable names rather than logical constants. To avoid this issue, always use `TRUE` and `FALSE` for logical indexing and column selection in `data.table`. |
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.
logical indexing -> logical subsetting?
also the user could re-define TRUE and FALSE, so I think saying that T and F default to TRUE and FALSE, and can be redefined is misleading. better to say anything can be redefined in the user environment? but this is not specific to data table so I'm not sure that any FAQ is even needed
@jangorecki can you please review since you commented on the original issue?
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.
looks ok to me
It looks like it is something not just for |
In discussion of issue #4846 decision was to add this as faq, Hi @MichaelChirico WDYT ? if this is not needed anymore, i can close this PR |
Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
vignettes/datatable-faq.Rmd
Outdated
@@ -460,6 +460,10 @@ Happily, an internet search for "How does R method dispatch work" (at the time o | |||
|
|||
However, features like basic S3 dispatch (pasting the function name together with the class name) is why some R folk love R. It's so simple. No complicated registration or signature is required. There isn't much needed to learn. To create the `merge` method for data.table all that was required, literally, was to merely create a function called `merge.data.table`. | |||
|
|||
## Why do `T` and `F` behave differently from `TRUE` and `FALSE` in `data.table`? | |||
|
|||
In R, `T` and `F` are global variables that default to `TRUE` and `FALSE`, respectively. However, they can be redefined in the user environment, leading to unexpected behavior. The `data.table` package might treat `T` and `F` as variable names rather than logical constants. To avoid this issue, always use `TRUE` and `FALSE` for logical subsetting and column selection in `data.table`. |
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 think it's too many words. I would simply reproduce the example from the cited issue, then quickly explain what's happening and give the recommendation to avoid T
/F
shorthand in general.
We might even give a shoutout to T_and_F_symbol_linter()
🙃
@Anirban166 no, not quite -- read the linked issue. Consider Moreover data.table scoping makes things complicated -- consider |
I think it's reasonable that |
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'm all good with adding this FAQ entry for that purpose, and I think it would be helpful for those who stick to T
/F
. I just personally don't see much point in their existence in R, as sticking with TRUE
or FALSE
when needed sounds reasonable and good enough (in terms of typing convenience) to me. (In other words, people shouldn't generally use them even without us telling them I feel)
Agreed, but it's definitely a gotcha for beginners. Heaven knows why but tons of newbies (including myself!) start out writing R with |
I took a pass at condensing this a bit, in case anyone wants to take a read with a 2nd pair of eyes before merging. |
vignettes/datatable-faq.Rmd
Outdated
@@ -460,6 +460,44 @@ Happily, an internet search for "How does R method dispatch work" (at the time o | |||
|
|||
However, features like basic S3 dispatch (pasting the function name together with the class name) is why some R folk love R. It's so simple. No complicated registration or signature is required. There isn't much needed to learn. To create the `merge` method for data.table all that was required, literally, was to merely create a function called `merge.data.table`. | |||
|
|||
## Why do `T` and `F` behave differently from `TRUE` and `FALSE` in some `data.table` queries? | |||
|
|||
Using `T` and `F` as abbreviations for `TRUE` and `FALSE` in `data.table` can lead to unexpected behavior. This is because `T` and `F` are global variables that can be redefined, which causes them to be treated as variable names rather than logical constants. This issue does not occur with `TRUE` and `FALSE`. Avoiding `T` and `F` is advice for using R generally, but it shows up in data.table in some perhaps surprising ways, for example: |
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.
Using
T
andF
as abbreviations forTRUE
andFALSE
indata.table
can lead to unexpected behavior. This is becauseT
andF
are global variables that can be redefined, which causes them to be treated as variable names rather than logical constants. This issue does not occur withTRUE
andFALSE
.
Using T
and F
as abbreviations for TRUE
and FALSE
in data.table
can lead to unexpected behavior as they are global variables that can be redefined, and are thus treated as variable names rather than reserved logical constants.
(Doesn't have to be what I rephrased above, but I would suggest something slightly less repetitive, and I don't think we need to explicitly mention the somewhat obvious resolution that the issue does not occur when using TRUE
/FALSE
instead)
Avoiding
T
andF
is advice for using R generally, but it shows up in data.table in some perhaps surprising ways, for example
data.table
# same output as DT[, .SD, .SDcols=c(TRUE, TRUE, FALSE)] | ||
``` | ||
|
||
As a general word of advice, `lintr::T_and_F_symbol_linter()` detects the usage of `T` and `F` and suggests replacing them with `TRUE` and `FALSE` to avoid such issues. |
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.
Nice plug for the linter :)
But in seriousness, I like how condensed this is. Conveys the message clearly.
Thanks! |
closes #4846
Description
This pull request adds a new FAQ entry in
data.table.faq.Rmd
to clarify the behavior difference betweenT/F
andTRUE/FALSE
when used in thej
argument ofdata.table
. The FAQ entry explains why it's recommended to useTRUE
andFALSE
overT
andF
to avoid unexpected behavior.