<- function(x, y) {
f02 # A comment
+ y
x
}
typeof(f02)
#> [1] "closure"
class(f02)
#> [1] "function"
::ftype(f02)
sloop#> [1] "function"
::otype(f02)
sloop#> [1] "base"
Function
Fundamentals
Function Components
R functions are objects in their own right or “first-class functions”.
3 Function components
formals(f02)
#> $x
#>
#>
#> $y
body(f02)
#> {
#> x + y
#> }
environment(f02)
#> <environment: R_GlobalEnv>
srcref
attribute is used for printing the source code.
attributes(f02)
#> $srcref
#> function(x, y) {
#> # A comment
#> x + y
#> }
print.function(f02)
#> function(x, y) {
#> # A comment
#> x + y
#> }
First-class Function
anonymous function (no name binding is necessary)
lapply(mtcars, function(x) length(unique(x)))
Filter(function(x) !is.numeric(x), mtcars)
integrate(function(x) sin(x) ^ 2, 0, pi)
List of functions
<- list(
funs half = function(x) x / 2,
double = function(x) x * 2
)
$double(10)
funs#> [1] 20
do.call()
<- list(1:10, na.rm = TRUE)
args do.call(mean, args)
#> [1] 5.5
Exercise
match.fun()
finds the function
match.fun("mean")
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x7f7c4028a358>
#> <environment: namespace:base>
- Calling anonymous function
class(function(x) x + 1)
#> [1] "function"
function(x) x + 1)(2)
(#> [1] 3
- List all function in base
<- mget(ls("package:base", all = TRUE), inherits = TRUE)
objs <- Filter(is.function, objs) funs
Find functions that has most arguments
library(purrr)
library(dplyr)
library(tibble)
Helper to counts number of arguments
<- function(f){
get_num_args
<- formals(f)
args if(is.null(args)) return(NA_integer_) # for primitive function
length(args)
}
get_num_args(f02)
#> [1] 2
get_num_args(sum)
#> [1] NA
<- map_dbl(funs, get_num_args) %>%
basefuns_tbl enframe("base_fun", "num_args")
Base functions that has most arguments
%>%
basefuns_tbl arrange(desc(num_args)) %>%
head()
#> # A tibble: 6 × 2
#> base_fun num_args
#> <chr> <dbl>
#> 1 scan 22
#> 2 format.default 16
#> 3 source 16
#> 4 formatC 15
#> 5 library 13
#> 6 merge.data.frame 13