Coder Social home page Coder Social logo

Comments (9)

brodieG avatar brodieG commented on May 27, 2024 1

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.

HenrikBengtsson avatar HenrikBengtsson commented on May 27, 2024 1

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.

HenrikBengtsson avatar HenrikBengtsson commented on May 27, 2024

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.

HenrikBengtsson avatar HenrikBengtsson commented on May 27, 2024

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.

HenrikBengtsson avatar HenrikBengtsson commented on May 27, 2024

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.

HenrikBengtsson avatar HenrikBengtsson commented on May 27, 2024

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.

HenrikBengtsson avatar HenrikBengtsson commented on May 27, 2024

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.

HenrikBengtsson avatar HenrikBengtsson commented on May 27, 2024

Related:

from wishlist-for-r.

HenrikBengtsson avatar HenrikBengtsson commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.