Re: [Rd] ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1

2018-09-01 Thread Hugh Parsonage
I would add my support for the change.

>From a cursory survey of existing code, I'd say that usage like `class(x)
== 'y'` -- rather than inherits(x, 'y') or is.y -- is probably going to be
the major source of new warnings. So perhaps in the NEWS item it could be
noted as a clue for developers encountering nascent warnings.

Of course `if (class(x) == 'y')` already throws a warning, just not `if
(class(x) == 'y' && TRUE)`.



On Thu, 30 Aug 2018 at 21:09 Emil Bode  wrote:

> I have to disagree, I think one of the advantages of '||' (or &&) is the
> lazy evaluation, i.e. you can use the first condition to "not care" about
> the second (and stop errors from being thrown).
> So if I want to check if x is a length-one numeric with value a value
> between 0 and 1, I can do 'class(x)=='numeric' && length(x)==1 && x>0 &&
> x<1'.
> In your proposal, having x=c(1,2) would throw an error or multiple
> warnings.
> Also code that relies on the second argument not being evaluated would
> break, as we need to evaluate y in order to know length(y)
> There may be some benefit in checking for length(x) only, though that
> could also cause some false positives (e.g. 'x==-1 || length(x)==0' would
> be a bit ugly, but not necessarily wrong, same for someone too lazy to
> write x[1] instead of x).
>
> And I don’t really see the advantage. The casting to length one is (I
> think), a feature, not a bug. If I have/need a length one x, and a length
> one y, why not use '|' and '&'? I have to admit I only use them in
> if-statements, and if I need an error to be thrown when x and y are not
> length one, I can use the shorter versions and then the if throws a warning
> (or an error for a length-0 or NA result).
>
> I get it that for someone just starting in R, the differences between |
> and || can be confusing, but I guess that's just the price to pay for
> having a vectorized language.
>
> Best regards,
> Emil Bode
>
> Data-analyst
>
> +31 6 43 83 89 33
> emil.b...@dans.knaw.nl
>
> DANS: Netherlands Institute for Permanent Access to Digital Research
> Resources
> Anna van Saksenlaan 51 | 2593 HW Den Haag
> 
> | +31 70 349 44 50 | i...@dans.knaw.nl  |
> dans.knaw.nl  www.dans.knaw.nl>
> DANS is an institute of the Dutch Academy KNAW  and
> funding organisation NWO .
>
> On 29/08/2018, 05:03, "R-devel on behalf of Henrik Bengtsson" <
> r-devel-boun...@r-project.org on behalf of henrik.bengts...@gmail.com>
> wrote:
>
> # Issue
>
> 'x || y' performs 'x[1] || y' for length(x) > 1.  For instance (here
> using R 3.5.1),
>
> > c(TRUE, TRUE) || FALSE
> [1] TRUE
> > c(TRUE, FALSE) || FALSE
> [1] TRUE
> > c(TRUE, NA) || FALSE
> [1] TRUE
> > c(FALSE, TRUE) || FALSE
> [1] FALSE
>
> This property is symmetric in LHS and RHS (i.e. 'y || x' behaves the
> same) and it also applies to 'x && y'.
>
> Note also how the above truncation of 'x' is completely silent -
> there's neither an error nor a warning being produced.
>
>
> # Discussion/Suggestion
>
> Using 'x || y' and 'x && y' with a non-scalar 'x' or 'y' is likely a
> mistake.  Either the code is written assuming 'x' and 'y' are scalars,
> or there is a coding error and vectorized versions 'x | y' and 'x & y'
> were intended.  Should 'x || y' always be considered an mistake if
> 'length(x) != 1' or 'length(y) != 1'?  If so, should it be a warning
> or an error?  For instance,
> '''r
> > x <- c(TRUE, TRUE)
> > y <- FALSE
> > x || y
>
> Error in x || y : applying scalar operator || to non-scalar elements
> Execution halted
>
> What about the case where 'length(x) == 0' or 'length(y) == 0'?  Today
> 'x || y' returns 'NA' in such cases, e.g.
>
> > logical(0) || c(FALSE, NA)
> [1] NA
> > logical(0) || logical(0)
> [1] NA
> > logical(0) && logical(0)
> [1] NA
>
> I don't know the background for this behavior, but I'm sure there is
> an argument behind that one.  Maybe it's simply that '||' and '&&'
> should always return a scalar logical and neither TRUE nor FALSE can
> be returned.
>
> /Henrik
>
> PS. This is in the same vein as
> https://mailman.stat.ethz.ch/pipermail/r-devel/2017-March/073817.html
> - in R (>=3.4.0) we now get that if (1:2 == 1) ... is an error if
> _R_CHECK_LENGTH_1_CONDITION_=true
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Argument 'dim' misspelled in error message

2018-09-01 Thread Kurt Hornik
> Hervé Pagès writes:

Thanks: fixed in the trunk with c75223.

Best
-k

> Hi,
> The following error message misspells the name of
> the 'dim' argument:

>> array(integer(0), dim=integer(0))
>Error in array(integer(0), dim = integer(0)) :
>  'dims' cannot be of length 0

> The name of the argument is 'dim' not 'dims':

>> args(array)
>function (data = NA, dim = length(data), dimnames = NULL)
>NULL

> Cheers,
> H.

> -- 
> Hervé Pagès

> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024

> E-mail: hpa...@fredhutch.org
> Phone:  (206) 667-5791
> Fax:(206) 667-1319

> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Segfault when performing match on POSIXlt object

2018-09-01 Thread Kurt Hornik
> Marco Giuliano writes:

Thanks.  Should be fixed in the trunk with c75224: will close the PR
after more testing.

Best
-k

> Bug report submitted :
> https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17459
> Thanks!

> On Fri, Aug 31, 2018 at 6:48 PM Martin Maechler 
> wrote:

>> > Marco Giuliano
>> > on Fri, 31 Aug 2018 16:50:56 +0200 writes:
>> 
>> > Hi Martin, should I file a formal bug report somewhere or
>> > you've already done it ?
>> 
>> No, I haven't,
>> and as I may not address this bug further myself (in the near
>> future), it may be best if you file a formal report.
>> 
>> I will create an account for you on R's bugzilla - you will be
>> notified and can update your initial pseudo-random password.
>> 
>> Best,
>> Martin
>> 
>> 

>   [[alternative HTML version deleted]]

> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Segfault when performing match on POSIXlt object

2018-09-01 Thread Marco Giuliano
Thanks a lot !

Best,
Marco

On Sat, Sep 1, 2018 at 2:48 PM Kurt Hornik  wrote:

> > Marco Giuliano writes:
>
> Thanks.  Should be fixed in the trunk with c75224: will close the PR
> after more testing.
>
> Best
> -k
>
> > Bug report submitted :
> > https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17459
> > Thanks!
>
> > On Fri, Aug 31, 2018 at 6:48 PM Martin Maechler <
> maech...@stat.math.ethz.ch>
> > wrote:
>
> >> > Marco Giuliano
> >> > on Fri, 31 Aug 2018 16:50:56 +0200 writes:
> >>
> >> > Hi Martin, should I file a formal bug report somewhere or
> >> > you've already done it ?
> >>
> >> No, I haven't,
> >> and as I may not address this bug further myself (in the near
> >> future), it may be best if you file a formal report.
> >>
> >> I will create an account for you on R's bugzilla - you will be
> >> notified and can update your initial pseudo-random password.
> >>
> >> Best,
> >> Martin
> >>
> >>
>
> >   [[alternative HTML version deleted]]
>
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] True length - length(unclass(x)) - without having to call unclass()?

2018-09-01 Thread Dénes Tóth
The solution below introduces a dependency on data.table, but otherwise 
it does what you need:


---

# special method for Foo objects
length.Foo <- function(x) {
  length(unlist(x, recursive = TRUE, use.names = FALSE))
}

# an instance of a Foo object
x <- structure(list(a = 1, b = list(b1 = 1, b2 = 2)), class = "Foo")

# its length
stopifnot(length(x) == 3L)

# get its length as if it were a standard list
.length <- function(x) {
  cls <- class(x)
  # setattr() does not make a copy, but modifies by reference
  data.table::setattr(x, "class", NULL)
  # get the length
  len <- base::length(x)
  # re-set original classes
  data.table::setattr(x, "class", cls)
  # return the unclassed length
  len
}

# to check that we do not make unwanted changes
orig_class <- class(x)

# check that the address in RAM does not change
a1 <- data.table::address(x)

# 'unclassed' length
stopifnot(.length(x) == 2L)

# check that address is the same
stopifnot(a1 == data.table::address(x))

# check against original class
stopifnot(identical(orig_class, class(x)))

---


On 08/24/2018 07:55 PM, Henrik Bengtsson wrote:

Is there a low-level function that returns the length of an object 'x'
- the length that for instance .subset(x) and .subset2(x) see? An
obvious candidate would be to use:

.length <- function(x) length(unclass(x))

However, I'm concerned that calling unclass(x) may trigger an
expensive copy internally in some cases.  Is that concern unfounded?

Thxs,

Henrik

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] ROBUSTNESS: a:b to give an error when length(a) > 1 or length(b) > 1 - not just a warning

2018-09-01 Thread Henrik Bengtsson
For the construct a:b, we get an error if either 'a' or 'b' is empty, e.g.

> x <- integer(0)
> x:3
Error in x:3 : argument of length 0
> 3:x
Error in 3:x : argument of length 0


However, for length(a) > 1 or length(b) > 1, we only get a warning;

> x <- 1:2
> x:3
[1] 1 2 3
Warning in x:3 :
In x:3 : numerical expression has 2 elements: only the first used
> 3:x
[1] 3 2 1
Warning message:
In 3:x : numerical expression has 2 elements: only the first used


I'd like to suggest to update R to produce an *error* whenever
length(a) != 1 or length(b) != 1.  For example,

> x <- 1:3
> x:3
Error in x:3 : argument is not of length one
> 3:x
Error in 3:x : argument is not of length one

The code involved is in the native function do_colon() of src/main/seq.c.

/Henrik

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel