r-devel@r-project.org
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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
r-devel@r-project.org
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 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 > > __ > 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
r-devel@r-project.org
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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] class() bug when used within a validity method
Hi, This is a long-standing bug where 'class(object)' does not return the actual class of 'object' when used inside a validity method. Instead it seems to return the class for which the validity method is defined. For example: setClass("A", slots=c(stuff="ANY")) setValidity("A", function(object) { cat("validating an object of class:", class(object), "\n") TRUE }) Then: a <- new("A") validObject(a) # validating an object of class: A # [1] TRUE which is OK. But trying to validate an object that belongs to a subclass of A leads to a big surprise: setClass("B", contains="A") b <- new("B") class(b) # [1] "B" validObject(b) # validating an object of class: A # [1] TRUE The class of object 'b' as seen by class() is wrong. Note that this doesn't happen if A is defined as a VIRTUAL class. 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
Re: [Rd] class() bug when used within a validity method
On 8/19/19 16:23, Pages, Herve wrote: ... > Note that this doesn't happen if A is defined as a VIRTUAL class. To be precise, when A is a VIRTUAL class, it requires at least one additional level of class extension to break class(): setClass("A", contains="VIRTUAL", slots=c(stuff="ANY")) setValidity("A", function(object) { cat("validating an object of class:", class(object), "\n") TRUE }) setClass("B", contains="A") setClass("C", contains="B") Then: c <- new("C") validObject(c) # validating an object of class: B # [1] TRUE 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