Get Function Parameters as List in R (Example)

 

On this page, I’ll demonstrate how to save all parameters of a user-defined function in R programming.

The content of the tutorial is structured as follows:

Let’s do this…

 

Creating a User-Defined Function in R

The following user-defined function will be used as a basis for this R programming tutorial:

my_fun1 <- function(input1, input2 = 5, ...) {    # Create user-defined function
 
  out <- input1 + input2
  print(out)
}

We may apply our user-defined function as shown in the following code:

my_fun1(input1 = 10)                              # Apply user-defined function
# [1] 15

Have a look at the previous output of the RStudio console. It shows the result of our function, i.e. the value 15.

However, the function above does not store the input arguments that we have used to create this result.

This is what I’m going to show you next.

 

Example: Create List of Parameters Using environment() Function

The following R code shows how to add a code snippet to a user-defined function that stores all the input values of this function.

Have a look at the R syntax below. At the beginning of the user-defined function, we are creating a new data object called my_parameters that stores all the input parameters of our function:

my_fun2 <- function(input1, input2 = 5, ...) {    # Save parameters of user-defined function
 
  my_parameters <<- c(as.list(environment()), list(...))
 
  out <- input1 + input2
  print(out)
}

Let’s apply our new function:

my_fun2(input1 = 10)                              # Apply user-defined function
# [1] 15

Once again, the output is the value 15. However, in addition to that we have created a new list containing all the input arguments. Let’s print this list:

my_parameters                                     # Print parameters
# $input1
# [1] 10
# 
# $input2
# [1] 5

We may also insert additional inputs into our function:

my_fun2(input1 = 10,                              # Specify multiple arguments
        input2 = 20, 
        input3 = "xxx")
# [1] 30

Let’s print the list of input parameters once again:

my_parameters                                     # Print parameters
# $input1
# [1] 10
# 
# $input2
# [1] 20
# 
# $input3
# [1] "xxx"

As you can see, our list was extended by the new parameters.

 

Video & Further Resources

Have a look at the following video on my YouTube channel. I’m explaining the R programming syntax of this page in the video:

 

 

Furthermore, you might read some of the related R posts on this website.

 

To summarize: You have learned in this article how to get all parameters of a user-defined function as a list in the R programming language. In case you have further questions and/or comments, kindly let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top