I have an R6 class, which contains a family of helper functions for conducting routine checks. These functions exist in the public list, usually take a single argument, conduct some checks on the value passed to the single argument, and if nothing is wrong, returns the value. It is common for me to use multiple checks quite frequently.
I would like to use magrittr
to make chaining these tests together easy, is it possible to use the with
function to further shorten the code
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
How can I get the %>%
operator to get recognised within the with
function?