do: Execute a function call not unlike 'do.call'.

Description Usage Arguments Value Note Examples

View source: R/utilities.R

Description

This function serves as an efficient replacement for do.call; arguments can be passed via ... to avoid any copying of potentially large objects.

Usage

1
do(what, ..., arg_list = list())

Arguments

what

either a function or a non-empty character string naming the function to be called

...

arguments to what, usually specified as key = value pairs

arg_list

a list of arguments to the function call. The names attribute of arg_list gives the argument names.

Value

The result of the (evaluated) function call.

Note

This function was inspired by do.call2 in package BBmisc

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# create a largish data.frame
x <- data.frame(a = seq_len(1e7), b = seq_len(1e7)/10)

# check that do() and do.call() returns the same;
# suppose we want to call head() to display the first 10 rows
stopifnot(identical(head(x, n = 10L), 
                    do("head", arg_list = list(x, n = 10L))))
stopifnot(identical(do.call("head", list(x, n = 10L)), 
                    do("head", x, n = 10L)))

#
# speed comparisons
# 

# a little helper function (do not use for serious measurements)
test <- function(expr) {
    gc(reset = TRUE)
    cat("CPU time:\n")
    print(system.time(expr, gcFirst = FALSE))
    cat("\nRAM usage:\n")
    print(gc())
}

# a direct call for comparison
test(head(x, n = 10L))

# do.call() can be substantially slower because it might make a copy
test(do.call("head", list(x, n = 10L)))

# do() is almost as fast as a direct call in this case
test(do("head", x, n = 10L))

# try to avoid using the 'arg_list' argument for passing large objects
test(do("head", n = 10L, arg_list = list(x = x)))

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.