Comments (9)
I would suggest a warning for symmetry with:
> 1:2 + 1:3
[1] 2 4 4
Warning message:
In 1:2 + 1:3 :
longer object length is not a multiple of shorter object length
Seems like a similar type of error. Changing the NA behavior seems like a much higher hurdle because if anyone ran into this in the past they would have noticed (e.g. if(NA)
) so existing code may actually expet the NA in some cases.
I'm happy to help with this to the extent my abilities and other demands allow me to.
from wishlist-for-r.
UPDATE: I just posted 'ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1' to R-devel on 2018-08-28.
from wishlist-for-r.
UPDATE 2018-09-12: R-devel r75289 just gained support for this:
NEWS: Experimentally, setting environment variable
_R_CHECK_LENGTH_1_LOGIC2_
will lead to warnings (or errors if the variable is set to a 'true' value) when&&
or||
encounter and use arguments of length more than one.
from wishlist-for-r.
Hmm...
The _R_CHECK_LENGTH_1_LOGIC2_=true
check on LHS || RHS
applies only to the LHS and not the RHS:
> Sys.setenv("_R_CHECK_LENGTH_1_LOGIC2_"="true")
> c(TRUE, TRUE) || TRUE
Error in c(TRUE, TRUE) || TRUE :
'length(x) = 2 > 1' in coercion to 'logical(1)'
> TRUE || c(TRUE, TRUE)
[1] TRUE
For LHS && RHS
it works as expected, e.g.
> c(TRUE, TRUE) && TRUE
Error in c(TRUE, TRUE) && TRUE :
'length(x) = 2 > 1' in coercion to 'logical(1)'
> TRUE && c(TRUE, TRUE)
Error in TRUE && c(TRUE, TRUE) :
'length(x) = 2 > 1' in coercion to 'logical(1)'
Verified on R version 3.6.1 (2019-07-05), R version 3.6.1 Patched (2019-09-12 r77183), and R Under development (unstable) (2019-10-12 r77279).
UPDATE 2019-10-12: I've filed PR17630 about this problem.
UPDATE 2019-10-12: This comment was invalid because:
Doh... brain fried. To further clarify Peter's point/explanation for future eyes. The following is indeed expected:
Sys.setenv("R_CHECK_LENGTH_1_LOGIC2"="true")
TRUE || c(TRUE, TRUE)
[1] TRUE
FALSE || c(TRUE, TRUE)
Error in FALSE || c(TRUE, TRUE) :
'length(x) = 2 > 1' in coercion to 'logical(1)'and
FALSE && c(TRUE, TRUE)
[1] FALSE
TRUE && c(TRUE, TRUE)
Error in TRUE && c(TRUE, TRUE) :
'length(x) = 2 > 1' in coercion to 'logical(1)'It's only when R needs evaluates the RHS, to resolve 'LHS || RHS' or 'LHS && RHS', that it will be evaluated(*). Without evaluating RHS, it is not possible to know
length(RHS)
. (I'm pretty sure this was also discussed in length before on R-devel when 'R_CHECK_LENGTH_1_LOGIC2' was first proposed.)(*) This is document in help("Logic", package="base") as:
The longer form [&& and ||] evaluates left to right examining only the first
element of each vector. Evaluation proceeds only until the result is
determined.
from wishlist-for-r.
UPDATE 2022-02-23: _R_CHECK_LENGTH_1_LOGIC2_=warn
is the new default in R-devel (to become R 4.2.0), cf. wch/r-source@b3b9f66
from wishlist-for-r.
UPDATE 2022-04-27: This will now be an error in R-devel (to become R 4.3.0), cf. Make calling && or || with either argument of length greater than one into an error.
from wishlist-for-r.
UPDATE 2022-06-02: This is now an error in R-devel (to become R 4.3.0) and there's no longer an _R_CHECK_LENGTH_1_LOGIC2_
environment variable, cf. remove _R_CHECK_LENGTH_1_LOGIC2_
.
from wishlist-for-r.
Related:
- Bug 18378: Use &&/||, not &/|, inside condition clauses (a patch toward base-R code) [2022-07-17]
from wishlist-for-r.
UPDATE: R 4.3.0 (2023-04-21) now produces an error whenever length(x) != 1
in x || y
and x && y
, or when length(y) != 1
in x || y
.
from wishlist-for-r.
Related Issues (20)
- WISH: `drop` = FALSE by default for `[.data.frame` HOT 9
- Fast check for discreteness
- Make UTF-8 the default encoding for package metadata HOT 2
- WISH/ROBUSTNESS: Mechanism to prevent var <<- value from assigning non-existing 'var' HOT 1
- Consistency: fix matrix subsetting behaviour to be consistent with vectors and data.frames.
- Base R function for length(unique(x))
- WISH: Standardized SystemRequirements HOT 1
- `grDevices::dev.capabilities()` enhancements
- Permit larger seed argument values in set.seed() HOT 1
- Wish: base version of glue::glue() HOT 3
- Add predict method for `stats::kmeans()`
- R CMD check: Option for reporting on writes/updates to tools::R_user_dir() during checks HOT 1
- R CMD check: Option for testing with empty tools::R_user_dir() folders
- Control over `NA` equality in `base::rle()`
- WISH: Make serverSocket(0) useful (+ find a random TCP port that can be listened to) HOT 1
- Task & browser hooks to support implementation of a debug adapter protocol client
- parallel: A sequential cluster
- parallel: Two types of 'cluster':s - one a subclass of the other HOT 1
- WISH: `load()` to have an option to bust saved unevaluated promise objects (`PROMSXP`) HOT 2
- WISH: Customizable print method for character vector with long text
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wishlist-for-r.