[Rd] R-devel: as.vector(x, mode = "list") drops attributes despite documented not to

2021-12-01 Thread Henrik Bengtsson
Hi,

in R 4.1.2 we have:

> x <- structure(as.list(1:2), dim = c(1,2))
> x
 [,1] [,2]
[1,] 12
> as.vector(x, mode = "list")
 [,1] [,2]
[1,] 12

whereas in recent versions of R-devel (4.2.0) we have:

> x <- structure(as.list(1:2), dim = c(1,2))
> x
 [,1] [,2]
[1,] 12
> as.vector(x, mode = "list")
[[1]]
[1] 1

[[2]]
[1] 2

However, as I read ?as.vector, dropping of attributes should _not_
happen for non-atomic results such as lists.  Is the new behavior a
mistake?

Specifically, ?as.vector says:

'as.vector', a generic, attempts to coerce its argument into a vector
of mode 'mode' (the default is to coerce to whichever vector mode is
most convenient): if the result is atomic all attributes are removed.

[...]

Details:

The atomic modes are "logical", "integer", "numeric" (synonym
"double"), "complex", "character" and "raw".

[...] On the other hand, as.vector removes all attributes including
names for results of atomic mode (but not those of mode "list" nor
"expression").

Value:

[...]

For as.vector, a vector (atomic or of type list or expression). All
attributes are removed from the result if it is of an atomic mode, but
not in general for a list result. The default method handles 24 input
types and 12 values of type: the details of most coercions are
undocumented and subject to change.

/Henrik

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


[Rd] Add ... to Reduce?

2021-12-01 Thread SOEIRO Thomas
Dear list,

Currently, it is needed to use anonymous functions to pass additional 
parameters to f in Reduce.

The following patch adds ... to pass additional arguments directly and seems to 
work in simple cases (see example below).

However, since this was not available (even though it is common for similar 
functions), I suspect that I am missing something...

Best,

Thomas


dfs <- list(x = warpbreaks)
dfs$x$id <- seq_along(dfs$x$breaks)
dfs$y <- dfs$x[1:15, ]
dfs$z <- dfs$x[20:35, ]

identical(
  Reduce(function(...) merge(..., by = "id", all = TRUE), dfs),
  Reduce(merge, dfs, by = "id", all = TRUE)
)


diff -u orig/funprog.R mod/funprog.R
--- orig/funprog.R  2021-12-01 23:02:09.710231318 +0100
+++ mod/funprog.R   2021-12-01 23:23:58.591120101 +0100
@@ -1,7 +1,7 @@
 #  File src/library/base/R/funprog.R
 #  Part of the R package, https://www.R-project.org
 #
-#  Copyright (C) 1995-2014 The R Core Team
+#  Copyright (C) 1995-2021 The R Core Team
 #
 #  This program is free software; you can redistribute it and/or modify
 #  it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
 #  https://www.R-project.org/Licenses/
 
 Reduce <-
-function(f, x, init, right = FALSE, accumulate = FALSE)
+function(f, x, init, right = FALSE, accumulate = FALSE, ...)
 {
 mis <- missing(init)
 len <- length(x)
@@ -49,11 +49,11 @@
 if(!accumulate) {
 if(right) {
 for(i in rev(ind))
-init <- forceAndCall(2, f, x[[i]], init)
+init <- forceAndCall(2, f, x[[i]], init, ...)
 }
 else {
 for(i in ind)
-init <- forceAndCall(2, f, init, x[[i]])
+init <- forceAndCall(2, f, init, x[[i]], ...)
 }
 init
 }
@@ -66,13 +66,13 @@
 if(right) {
 out[[len]] <- init
 for(i in rev(ind)) {
-init <- forceAndCall(2, f, x[[i]], init)
+init <- forceAndCall(2, f, x[[i]], init, ...)
 out[[i]] <- init
 }
 } else {
 out[[1L]] <- init
 for(i in ind) {
-init <- forceAndCall(2, f, init, x[[i]])
+init <- forceAndCall(2, f, init, x[[i]], ...)
 out[[i]] <- init
 }
 }
@@ -80,14 +80,14 @@
 if(right) {
 out[[len]] <- init
 for(i in rev(ind)) {
-init <- forceAndCall(2, f, x[[i]], init)
+init <- forceAndCall(2, f, x[[i]], init, ...)
 out[[i]] <- init
 }
 }
 else {
 for(i in ind) {
 out[[i]] <- init
-init <- forceAndCall(2, f, init, x[[i]])
+init <- forceAndCall(2, f, init, x[[i]], ...)
 }
 out[[len]] <- init
 }
diff -u orig/funprog.Rd mod/funprog.Rd
--- orig/funprog.Rd 2021-12-01 23:02:38.400738386 +0100
+++ mod/funprog.Rd  2021-12-01 23:29:28.993976101 +0100
@@ -21,7 +21,7 @@
   given function.
 }
 \usage{
-Reduce(f, x, init, right = FALSE, accumulate = FALSE)
+Reduce(f, x, init, right = FALSE, accumulate = FALSE, ...)
 Filter(f, x)
 Find(f, x, right = FALSE, nomatch = NULL)
 Map(f, ...)
@@ -44,7 +44,7 @@
 combination is used.}
   \item{nomatch}{the value to be returned in the case when
 \dQuote{no match} (no element satisfying the predicate) is found.}
-  \item{\dots}{vectors.}
+  \item{\dots}{arguments to be passed to FUN.}
 }
 \details{
   If \code{init} is given, \code{Reduce} logically adds it to the start



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