Check length of logical vector also for operands of || and &&?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Check length of logical vector also for operands of || and &&?

Kirill Müller-2
Hi everyone


The following behavior (in R 3.6.1 and R-devel r77040) caught me by
surprise today:

truthy <- c(TRUE, FALSE)
falsy <- c(FALSE, TRUE, FALSE)

if (truthy) "check"
#> Warning in if (truthy) "check": the condition has length > 1 and only the
#> first element will be used
#> [1] "check"
if (falsy) "check"
#> Warning in if (falsy) "check": the condition has length > 1 and only the
#> first element will be used
if (FALSE || truthy) "check"
#> [1] "check"
if (FALSE || falsy) "check"
if (truthy || FALSE) "check"
#> [1] "check"
if (falsy || FALSE) "check"

The || operator gobbles the warning about a length > 1 vector. I wonder
if the existing checks for length 1 can be extended to the operands of
the || and && operators. Thanks (and apologies if this has been raised
before).


Best regards

Kirill

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Check length of logical vector also for operands of || and &&?

Henrik Bengtsson-5
NEWS for R 3.6.0:

* 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.

> Sys.setenv("_R_CHECK_LENGTH_1_LOGIC2_" = "TRUE")
> if (FALSE || truthy) "check"
Error in FALSE || truthy :
  'length(x) = 2 > 1' in coercion to 'logical(1)'

Some more info and breadcrumbs at
https://github.com/HenrikBengtsson/Wishlist-for-R/issues/48

/Henrik

On Mon, Aug 19, 2019 at 4:19 PM Kirill Müller <[hidden email]> wrote:

>
> Hi everyone
>
>
> The following behavior (in R 3.6.1 and R-devel r77040) caught me by
> surprise today:
>
> truthy <- c(TRUE, FALSE)
> falsy <- c(FALSE, TRUE, FALSE)
>
> if (truthy) "check"
> #> Warning in if (truthy) "check": the condition has length > 1 and only the
> #> first element will be used
> #> [1] "check"
> if (falsy) "check"
> #> Warning in if (falsy) "check": the condition has length > 1 and only the
> #> first element will be used
> if (FALSE || truthy) "check"
> #> [1] "check"
> if (FALSE || falsy) "check"
> if (truthy || FALSE) "check"
> #> [1] "check"
> if (falsy || FALSE) "check"
>
> The || operator gobbles the warning about a length > 1 vector. I wonder
> if the existing checks for length 1 can be extended to the operands of
> the || and && operators. Thanks (and apologies if this has been raised
> before).
>
>
> Best regards
>
> Kirill
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Check length of logical vector also for operands of || and &&?

Duncan Murdoch-2
In reply to this post by Kirill Müller-2
On 19/08/2019 10:19 a.m., Kirill Müller wrote:

> Hi everyone
>
>
> The following behavior (in R 3.6.1 and R-devel r77040) caught me by
> surprise today:
>
> truthy <- c(TRUE, FALSE)
> falsy <- c(FALSE, TRUE, FALSE)
>
> if (truthy) "check"
> #> Warning in if (truthy) "check": the condition has length > 1 and only the
> #> first element will be used
> #> [1] "check"
> if (falsy) "check"
> #> Warning in if (falsy) "check": the condition has length > 1 and only the
> #> first element will be used
> if (FALSE || truthy) "check"
> #> [1] "check"
> if (FALSE || falsy) "check"
> if (truthy || FALSE) "check"
> #> [1] "check"
> if (falsy || FALSE) "check"
>
> The || operator gobbles the warning about a length > 1 vector. I wonder
> if the existing checks for length 1 can be extended to the operands of
> the || and && operators. Thanks (and apologies if this has been raised
> before).
>

This seems to be an August topic.  It was discussed last year in a  long
thread starting with this message:

https://stat.ethz.ch/pipermail/r-devel/2018-August/076678.html

I think there was general agreement that it would be a good idea to add
some warnings.  News for R 3.6.0 includes this:

"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."

You get a warning if you set that variable to "warn", you get an error
if you set it to "true".

Duncan Murdoch


Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel