我想写一个自定义管道操作符的地方,用的是operator name is open.它可以是例如%>%%|%:=、...也许它需要根据需要的operator precedence来 Select ,就像在Same function but using for it the name %>% causes a different result compared when using the name :=中解释的那样.

使用的placeholder name is open._是常见的,它需要是placed explicitly(不会自动放置作为第一个参数).

evaluation environment is open号.但在this answer中,似乎应该避免使用用户环境.

如果它与占位符具有相同的名称,它应该能够为keep the value in the user environment.

1 %>% identity(.)
#[1] 1
.
#Error: object '.' not found

. <- 2
1 %>% identity(.)
#[1] 1
.
#[1] 2

它应该能够update values in the user environment包括占位符的名称.

1 %>% assign("x", .)
x
#[1] 1

"x" %>% assign(., 2)
x
#[1] 2

1 %>% assign(".", .)
.
#[1] 1

"." %>% assign(., 2)
.
#[1] 2

x <- 1 %>% {names(.) <- "foo"; .}
x
#foo 
#  1 

应该是evaluate from left to right.

1 %>% . + 2 %>% . * 3
#[1] 9

目前,我有:

`:=` <- function(lhs, rhs) {
  e <- exists(".", parent.frame(), inherits = FALSE)
  . <- get0(".", envir = parent.frame(), inherits = FALSE)
  assign(".", lhs, envir=parent.frame())
  on.exit(if(identical(lhs, get0(".", envir = parent.frame(), inherits = FALSE))) {
            if(e) {
              assign(".", ., envir=parent.frame())
            } else {
              if(exists(".", parent.frame())) rm(., envir = parent.frame())
            }
          })
  eval(substitute(rhs), parent.frame())
}

但它在try 时失败了:

. <- 0
1 := assign(".", .)
.
#[1] 0

下面给出了预期的结果,但我不确定它是否真的从左到右计算.

1 := . + 2 := . * 3
#[1] 9

推荐答案

这意味着在算术运算下需要一个优先级

1 %>% . + 2 %>% . * 3

这忽略了任何%>%操作,:=是一个不错的 Select ,我们也可以使用?,让我们使用:=.

默认情况下,assign()<-通常执行相同的操作.但你的例子暗示了另一种情况:

您希望assign(".", "foo")覆盖旧的点,但names(.) <- "foo"(可能还有. <- "foo")覆盖新的点,而不影响旧的点.

我相信实现这一点的唯一方法是特例assign(),我在下面做,你的测试是满意的.

使用此解决方案,我们在调用者的子环境中计算表达式,该表达式继承了除此子环境中的点之外的所有值,以及修改后的赋值函数,当未提供环境参数时,该赋值函数在调用者中赋值.

`:=` <- function(lhs, rhs) {
  pf <- parent.frame()
  rhs_call <- substitute(rhs)
  assign2 <- function (x, value, pos = -1, envir = as.environment(pos), inherits = FALSE, 
                       immediate = TRUE) {
    if (missing(pos) && missing(envir)) envir <- pf
    assign(x, value, envir = envir, inherits = inherits, immediate = immediate)
  }
  eval(rhs_call, envir = list(. = lhs, assign = assign2), enclos = pf)
}

1 := identity(.)
#> [1] 1
.
#> Error in eval(expr, envir, enclos): object '.' not found

. <- 2
1 := identity(.)
#> [1] 1
.
#> [1] 2

1 := assign("x", .)
x
#> [1] 1

"x" := assign(., 2)
x
#> [1] 2

1 := assign(".", .)
.
#> [1] 1

"." := assign(., 2)
.
#> [1] 2

x <- 1 := {names(.) <- "foo"; .}
x
#> foo 
#>   1

1 := . + 2 := . * 3
#> [1] 9

创建于2023-05-03年第reprex v2.0.2

R相关问答推荐

如何以编程方式将X轴勾号上的希腊符号合并到R图中?

self_函数无法工作--无法子集结束后的列

导入到固定列宽的R中时出现问题

如何在R中正确对齐放射状图中的文本

计算R中的威布尔分布的EDF

手动打印线型gplot

我想在R中总结一个巨大的数据框架,使我只需要唯一的lat、lon、Date(Year)和Maxium Value""""""""

par函数中的缩写,比如mgp,mar,mai是如何被破译的?

无法定义沿边轨迹的 colored颜色 渐变(与值无关)

使用`Watch()`和`renderUI()`时,不再满足仍出现在SHILINY AFTER条件中的条件输入

使用RSelenium在R中抓取Reddit时捕获多个标签

R -使用矩阵reshape 列表

将列表中的字符串粘贴到R中for循环内的dplyr筛选器中

自定义交互作用图的标签

创建新列,其中S列的值取决于该行S值是否与其他行冗余

在ggploy中创建GeV分布时出错

我已经运行了几个月的代码的`Palette()`中出现了新的gglot错误

在REST API中使用参数R

如何使用list_rind在列表中保留已命名但不包含第0行的记录?

具有由向量定义的可变步长的序列