tidyverse / magrittr

Improve the readability of R code with the pipe

Home Page:https://magrittr.tidyverse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dot forced by `{...}` anonymous function

egnha opened this issue · comments

According to NEWS.md, as of 1.5.0.9000 %>% won't force the LHS.

Example from NEWS.md:

ignore <- function(x) "return value"
stop("never called") %>% ignore()
#> [1] "return value"

However, the equivalent expression using {...} forces the LHS:

stop("never called") %>% {"return value"}
#> Error in stop("never called") %>% { : never called

Is this the expected behavior? (Or have I misunderstood the semantics of {...}?)

You can see what's happening if you remove the pipe:

ignore <- function(x) "return value"
ignore(stop("never called"))
#> [1] "return value"

{
 stop("never called")
 "return value"
}
#> Error in eval(expr, envir, enclos): never called

Created on 2020-09-09 by the reprex package (v0.3.0.9001)

Perhaps it would then be helpful to document the fact that x %>% {...} forces x. (Or put differently, that x %>% {...} and x %>% (function(.) ...) are not strictly equivalent.)

That fact is sufficiently esoteric that I'm not sure including it in the documentation would help anyone.