Skip to contents

Use many functions in one call using predeclared calls

Usage

chain_func(..., otherargs = list())

Arguments

...

functions to include. see example for how to use

otherargs

other arguments for use in each function

Value

returns a function that combines multiple function

Examples


# Example 1 with base functions
combf1 <- chain_func(unique, cumsum, print)
result <- combf1(sample(1:5,10,replace = TRUE))
#> [1]  3  7 12 13
#or
u = sample(1:5,10,replace = TRUE)
result <- combf1(u)
#> [1]  1  4  6 11

# Example 2 with base functions with arguments
combf2 <- chain_func(unique, print, otherargs = list(.= c(FALSE),.=c(2)))
result <- combf2(sample(1:3,10,replace = TRUE))
#> [1] 1 3 2

# Example 3 with custom functions
r = function(a,b,c){
  if(!missing(a))print(a)
  if(!missing(b))print(b)
  if(!missing(c))print(c)
  return(a)
}

r2 = function(a,b,c){
  if(!missing(a))message(a)
  if(!missing(b))message(b)
  if(!missing(c))message(c)
  return(a)
}

combf3 <- chain_func(r,r2, otherargs =list(.=c("apple","cat"),.=c("rice")))
res <- combf3(head(mtcars))
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#> [1] "apple"
#> [1] "cat"
#> c(21, 21, 22.8, 21.4, 18.7, 18.1)c(6, 6, 4, 6, 8, 6)c(160, 160, 108, 258, 360, 225)c(110, 110, 93, 110, 175, 105)c(3.9, 3.9, 3.85, 3.08, 3.15, 2.76)c(2.62, 2.875, 2.32, 3.215, 3.44, 3.46)c(16.46, 17.02, 18.61, 19.44, 17.02, 20.22)c(0, 0, 1, 1, 0, 1)c(1, 1, 1, 0, 0, 0)c(4, 4, 4, 3, 3, 3)c(4, 4, 1, 1, 2, 1)
#> rice