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) } |
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 |
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) } |
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 |
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 |
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 |
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" |
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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might read some of the related R posts on this website.
- Return Multiple Objects from User-Defined Function
- Access & Manipulate Body of Function in R
- Pass Column Names & Indices to User-Defined ggplot2 Function
- Get Second Sub Entry of Every List Element
- All R Programming Examples
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.
Statistics Globe Newsletter