首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R6和Magrittr

R6和Magrittr
EN

Stack Overflow用户
提问于 2016-12-31 06:49:39
回答 0查看 151关注 0票数 1

我有一个R6类,其中包含一系列用于执行例行检查的辅助函数。这些函数存在于公用列表中,通常采用单个参数,对传递给单个参数的值执行一些检查,如果没有错误,则返回值。对我来说,经常使用多个检查是很常见的。

我想使用magrittr来简化这些测试的链接,是否可以使用with函数来进一步缩短代码

代码语言:javascript
复制
library(magrittr)
library(R6)

test = R6::R6Class("Test",inherit=NULL,
  public = list(
    initialize = function(){

    },
    fA = function(x){
      writeLines("Called FA")
      x
    },
    fB = function(x){
      writeLines("Called FB")
      x
    },

    #Enable Chaining by Returning Invisible copy of the R6 Instance
    fC = function(x){
      writeLines("Called FC")
      invisible(self)
    },
    fD = function(x){
      writeLines("Called FD")
      invisible(self)
    }
  )
)

#Works as expected
x = test$new()
y <- 1 %>% x$fA() %>% x$fB()
y

#This is also possible, but it loses the desired return value, and the value of the argument
#needs to be explicitly passed through to each function.
x$fC(1)$fD(1)

#I Would Like to do something like this:
y <- with(x,1 %>% fA() %>% fB()) #ERROR, could not find function "%>%"

#Trying to resolve the above, I also tried this, which doesn't work.
y <- with(x,1 magrittr::`%>%` fA() magrittr::`%>%` fB()) #ERROR

如何在with函数中识别%>%操作符?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41403668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档