[External] Warning with new placeholder piped to data.frame extractors `[` and `[[`.
On Sat, 16 Jul 2022, Rui Barradas wrote:
Hello,
When piping to any of `[.data.frame` or `[[.data.frame`, the placeholder in
mandatory.
df1 <- data.frame(y = 1:10, f = rep(c("a", "b"), each = 5))
aggregate(y ~ f, df1, mean) |> `[`('y')
# Error: function '[' not supported in RHS call of a pipe
aggregate(y ~ f, df1, mean) |> `[[`('y')
# Error: function '[' not supported in RHS call of a pipe
But if used it throws a warning.
aggregate(y ~ f, df1, mean) |> `[`(x = _, 'y')
# Warning in `[.data.frame`(x = aggregate(y ~ f, df1, mean), "y"): named
arguments
# other than 'drop' are discouraged
# y
# 1 3
# 2 8
aggregate(y ~ f, df1, mean) |> `[[`(x = _, 'y')
# Warning in `[[.data.frame`(x = aggregate(y ~ f, df1, mean), "y"): named
# arguments other than 'exact' are discouraged
# [1] 3 8
The pipe syntax requirs that the placeolder be used as a named argument. If you do that, then the syntax is legal and parses successfully.
Hasn't this become inconsistent behavior? More than merely right, the named argument is mandatory, it shouldn't give warnings.
Any R function can decide whether it wants to allow explicitly named arguments. Disallowing or discouraging using explicitly named arguments requires some work and is usually not a good idea. In the case of the data.frame mechods for [ and [[ the decision was made to discourage using named arguments other than 'exact'. This seems to have been to allow a more an expedient way to implement these functions. This could be revisited, but I doubt is is worth the effort. For me the main reason for using pipes is to make code more readable. Using `[` and such constructs is not furthering that cause. When I use pipes I am almost always using tidyverse features, so I have dpyr::pull available, which is more readable, to me at least. Arguably, base R could have a similar function, but again I doubt this would be a good investment of time. An option that we have experimented with is to allow the placeholder at the head of an extraction chain. This is supported in the experimental branch at https://svn.r-project.org/R/branches/R-syntax. So for example: > mtcars |> _$cyl[1] [1] 6 This may make it into R-devel for the next release, but it still needs more testing. Best, luke
Hope this helps, Rui Barradas
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa Phone: 319-335-3386
Department of Statistics and Fax: 319-335-3017
Actuarial Science
241 Schaeffer Hall email: luke-tierney at uiowa.edu
Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu