Access & Manipulate Body of Function in R (2 Examples)
In this tutorial, I’ll explain how to print and change the body of a function using the body() command in the R programming language.
The content of the article is structured as follows:
If you want to know more about these content blocks, keep reading…
Example 1: Access Body of Function
In Example 1, I’ll explain how to return the body of a function (i.e. the source code) to the RStudio console.
For this task, we can use the body() function as shown below. In this example, we return the body of the var() function:
body(var) # Print body of var() function # { # if (missing(use)) # use <- if (na.rm) # "na.or.complete" # else "everything" # na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs", # "everything", "na.or.complete")) # if (is.na(na.method)) # stop("invalid 'use' argument") # if (is.data.frame(x)) # x <- as.matrix(x) # else stopifnot(is.atomic(x)) # if (is.data.frame(y)) # y <- as.matrix(y) # else stopifnot(is.atomic(y)) # .Call(C_cov, x, y, na.method, FALSE) # } |
body(var) # Print body of var() function # { # if (missing(use)) # use <- if (na.rm) # "na.or.complete" # else "everything" # na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs", # "everything", "na.or.complete")) # if (is.na(na.method)) # stop("invalid 'use' argument") # if (is.data.frame(x)) # x <- as.matrix(x) # else stopifnot(is.atomic(x)) # if (is.data.frame(y)) # y <- as.matrix(y) # else stopifnot(is.atomic(y)) # .Call(C_cov, x, y, na.method, FALSE) # }
As you can see, the previous R code has returned the source code of the var function.
Example 2: Manipulate Body of Function
In Example 2, I’ll explain how to modify the body of a function using the body() command in R.
For this, we first create our own user-defined function:
my_fun <- function(x) { # Create user-defined function x^2 + 5 * x } |
my_fun <- function(x) { # Create user-defined function x^2 + 5 * x }
The previous R code has created a new function called my_fun.
We can use the body() function to access the content of our function (as we already did in Example 1):
body(my_fun) # Print body of user-defined function # { # x^2 + 5 * x # } |
body(my_fun) # Print body of user-defined function # { # x^2 + 5 * x # }
We can also apply our function to a certain value, to return a result from the current body:
my_fun(x = 2) # Apply user-defined function # [1] 14 |
my_fun(x = 2) # Apply user-defined function # [1] 14
When we apply our function to the x-value 2, the result of our user-defined function is equal to 14.
If we want to change the body of our function, we can assign a new body using the body() and quote() commands:
body(my_fun, # Manipulate body of function envir = environment(my_fun)) <- quote(x^3 + 5 * x) |
body(my_fun, # Manipulate body of function envir = environment(my_fun)) <- quote(x^3 + 5 * x)
Let’s apply our function with updated body once again to the x-value 2:
my_fun(x = 2) # Apply updated user-defined function # [1] 18 |
my_fun(x = 2) # Apply updated user-defined function # [1] 18
This time the result is 18, i.e. the body has been changed.
Video & Further Resources
Some time ago, I have released a video on my YouTube channel, which explains the R code of this tutorial. You can find the video below.
The YouTube video will be added soon.
Furthermore, you may want to have a look at some of the other articles on this website. A selection of articles that are related to the application of the body() function is shown below.
At this point you should know how to apply the body() function in R programming. In case you have any further questions and/or comments, let me know in the comments below.
Statistics Globe Newsletter