Functions Are Objects
R functions are first-class objects (of the class “function”, of course), meaning that they can be used for the most part just like other objects. This is seen in the syntax of function creation:
g <- function(x) {
return(x+1)
}
These two arguments to function() can later be accessed via the R functions formals() and body(), as follows:
formals(g)
## $x
body(g)
## {
## return(x + 1)
## }
Simply typing the name of an object results in printing that object to the screen. Functions are no exception, since they are objects just like anything else.
g
## function(x) {
## return(x+1)
## }
Created a function h and simulated a similar case scenario to the one shown above.
h <- function(x) {
return(x+1)
}
formals(h)
## $x
body(h)
## {
## return(x + 1)
## }
h
## function(x) {
## return(x+1)
## }
Browsed through the code to better understand how to use it.
g <- function(x) {
return(x+1)
}