R base pipe can’t be referenced with backticks? could not find function “|>”

I’m trying to build a wrapper around the base pipe |> to do some modification to the left-hand side and then pipe it forward normally. While working on this I found the following peculiarity about the base pipe compared to other operators:

Work as expected:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>`+`(1,2)
#> [1] 3
`<-`("foo", 1)
foo
#> [1] 1
library(magrittr)
`%>%`(1, print)
#> [1] 1
</code>
<code>`+`(1,2) #> [1] 3 `<-`("foo", 1) foo #> [1] 1 library(magrittr) `%>%`(1, print) #> [1] 1 </code>
`+`(1,2)
#> [1] 3

`<-`("foo", 1)
foo
#> [1] 1

library(magrittr)
`%>%`(1, print)
#> [1] 1

Does not work as expected:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>`|>`(1, print)
#> Error in `|>`(1, print): could not find function "|>"
</code>
<code>`|>`(1, print) #> Error in `|>`(1, print): could not find function "|>" </code>
`|>`(1, print)
#> Error in `|>`(1, print): could not find function "|>"

This means that, for example, while the following works fine with the magrittr pipe

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>as.call(c(as.name("%>%"), "foo", substitute(print)))
#> "foo" %>% print
</code>
<code>as.call(c(as.name("%>%"), "foo", substitute(print))) #> "foo" %>% print </code>
as.call(c(as.name("%>%"), "foo", substitute(print)))
#> "foo" %>% print

and can be passed to eval():

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>eval(as.call(c(as.name("%>%"), "foo", substitute(print))))
#> [1] "foo"
</code>
<code>eval(as.call(c(as.name("%>%"), "foo", substitute(print)))) #> [1] "foo" </code>
eval(as.call(c(as.name("%>%"), "foo", substitute(print))))
#> [1] "foo"

It does not work for the base pipe:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>eval(as.call(c(as.name("|>"), "foo", substitute(print))))
#> Error in `|>`("foo", print): could not find function "|>"
</code>
<code>eval(as.call(c(as.name("|>"), "foo", substitute(print)))) #> Error in `|>`("foo", print): could not find function "|>" </code>
eval(as.call(c(as.name("|>"), "foo", substitute(print))))
#> Error in `|>`("foo", print): could not find function "|>"

I’ve struggled to find documentation on this because I’m not quite sure how to describe this problem, and google often doesn’t play nice with queries involving special characters, even in quotations.

Edit: more detail on the context

As a toy example, say we want to make a pipe variant that pipes the name of the object, rather than the object, and without otherwise changing our usage:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>library(magrittr)
`%>>%` <- function(lhs, rhs) {
# get lhs as a character
lhs.name <- deparse(substitute(lhs))
# the parent environment
parent <- parent.frame()
# the environment in which to evaluate pipeline
env <- new.env(parent = parent)
# magrittr version
eval(
as.call(c(as.name("%>%"), lhs.name, substitute(rhs))),
env, env
)
}
foo_func <- function(arg1, arg2, arg3) {
print(arg1)
print(arg2)
print(arg3)
arg2 + arg3
}
thing %>>% foo_func(2,4)
#> [1] "thing"
#> [1] 2
#> [1] 4
#> [1] 6
</code>
<code>library(magrittr) `%>>%` <- function(lhs, rhs) { # get lhs as a character lhs.name <- deparse(substitute(lhs)) # the parent environment parent <- parent.frame() # the environment in which to evaluate pipeline env <- new.env(parent = parent) # magrittr version eval( as.call(c(as.name("%>%"), lhs.name, substitute(rhs))), env, env ) } foo_func <- function(arg1, arg2, arg3) { print(arg1) print(arg2) print(arg3) arg2 + arg3 } thing %>>% foo_func(2,4) #> [1] "thing" #> [1] 2 #> [1] 4 #> [1] 6 </code>
library(magrittr)
`%>>%` <- function(lhs, rhs) {
  # get lhs as a character
  lhs.name <- deparse(substitute(lhs))
  
  # the parent environment
  parent <- parent.frame()
  
  # the environment in which to evaluate pipeline
  env <- new.env(parent = parent)
  
  # magrittr version
  eval(
    as.call(c(as.name("%>%"), lhs.name, substitute(rhs))),
    env, env
  )
}

foo_func <- function(arg1, arg2, arg3) {
  print(arg1)
  print(arg2)
  print(arg3)
  arg2 + arg3
}

thing %>>% foo_func(2,4)
#> [1] "thing"
#> [1] 2
#> [1] 4
#> [1] 6

To avoid the magrittr dependency, I was interested in making this work with |> instead of %>%. It appears we can substitute in

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>eval(
str2lang(paste0("'", lhs.name, "'", " |> ", deparse(substitute(rhs)))),
env, env
)
</code>
<code>eval( str2lang(paste0("'", lhs.name, "'", " |> ", deparse(substitute(rhs)))), env, env ) </code>
eval(
    str2lang(paste0("'", lhs.name, "'", " |> ", deparse(substitute(rhs)))), 
    env, env
  ) 

though I have not yet tested it thoroughly.

8

1) If the question is how to implement “%>>%” without dependencies then copy insert_dot source code from the poorman source code at
https://github.com/nathaneastwood/poorman/blob/master/R/pipe.R
and modify the source code of %>% on the same page to be:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>"%>>%" <- function(lhs, rhs) {
rhs_call <- insert_dot(substitute(rhs))
eval(rhs_call, envir = list(`.` = substitute(lhs)), enclos = parent.frame())
}
# tests
if (exists("xyz")) rm(xyz)
xyz %>>% paste0("*")
## [1] "xyz*"
xyz %>>% paste(., .)
## [1] "xyz xyz"
</code>
<code>"%>>%" <- function(lhs, rhs) { rhs_call <- insert_dot(substitute(rhs)) eval(rhs_call, envir = list(`.` = substitute(lhs)), enclos = parent.frame()) } # tests if (exists("xyz")) rm(xyz) xyz %>>% paste0("*") ## [1] "xyz*" xyz %>>% paste(., .) ## [1] "xyz xyz" </code>
"%>>%" <- function(lhs, rhs) {
  rhs_call <- insert_dot(substitute(rhs))
  eval(rhs_call, envir = list(`.` = substitute(lhs)), enclos = parent.frame())
}

# tests

if (exists("xyz")) rm(xyz)

xyz %>>% paste0("*")
## [1] "xyz*"

xyz %>>% paste(., .)
## [1] "xyz xyz"

2) All we are really saving with this new pipe operator is one leg in a pipeline containing substitute. May it is sufficient not to define a new piping operator at all and just write:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>xyz |> substitute() |> paste0("*")
## [1] "xyz*"
xyz |> substitute() |> ( (x) paste(x, x) )()
## [1] "xyz xyz"
</code>
<code>xyz |> substitute() |> paste0("*") ## [1] "xyz*" xyz |> substitute() |> ( (x) paste(x, x) )() ## [1] "xyz xyz" </code>
xyz |> substitute() |> paste0("*")
## [1] "xyz*"

xyz |> substitute() |> ( (x) paste(x, x) )()
## [1] "xyz xyz"

or use quote

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>xyz |> quote() |> paste0("*")
## [1] "xyz*"
xyz |> quote() |> ( (x) paste(x, x) )()
## [1] "xyz xyz"
</code>
<code>xyz |> quote() |> paste0("*") ## [1] "xyz*" xyz |> quote() |> ( (x) paste(x, x) )() ## [1] "xyz xyz" </code>
xyz |> quote() |> paste0("*")
## [1] "xyz*"

xyz |> quote() |> ( (x) paste(x, x) )()
## [1] "xyz xyz"

3) If a dependency on magrittr is ok then

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>library(magrittr)
"%>>%" <- function(lhs, rhs) {
eval.parent(substitute(lhs %>% substitute %>% rhs))
}
</code>
<code>library(magrittr) "%>>%" <- function(lhs, rhs) { eval.parent(substitute(lhs %>% substitute %>% rhs)) } </code>
library(magrittr)

"%>>%" <- function(lhs, rhs) {
  eval.parent(substitute(lhs %>% substitute %>% rhs))
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật