R Error in Function: Unused Argument (2 Examples)

 

This article explains how to deal with the error “unused argument” in the R programming language.

Table of contents:

Let’s do this!

 

Example 1: Reproducing the Error Message: Unused Argument

The following syntax shows how to replicate the error “unused argument” in a manually created function.

First, we have to create our own function:

my_fun1 <- function(x) {         # Create user-defined function
  x^2
}

Now, we might try to apply our user-defined function as follows:

my_fun1(x = 1, y = 2)            # Try to apply user-defined function
# Error in my_fun1(x = 1, y = 2) : unused argument (y = 2)

Damn! The RStudio console returned the error massage “unused argument”. The reason for this is that we specified the argument y within our function, even though this argument is not defined for this function.

Let’s fix this problem!

 

Example 2: Fixing the Error Message: Unused Argument

Example 2 shows how to solve issues with the error message “unused argument” in R. For this, we can create another function:

my_fun2 <- function(x, ...) {    # User-defined function with ...
  x^2
}

As you can see in the previous R code, we have added “, …” within the head of our function. This allows the user to specify arguments that are not explicitly defined within the function.

Let’s apply our function in R:

my_fun2(x = 1, y = 2)            # Properly apply user-defined function
# 1

No error and an output was created – Looks good!

 

Video, Further Resources & Summary

Do you need more explanations on the topics of this article? Then you may want to watch the following video of my YouTube channel. In the video instruction, I’m explaining the R programming codes of this post in a live programming session in RStudio.

 

The YouTube video will be added soon.

 

Furthermore, you might read the other RStudio articles on my homepage.

 

In summary: In this article, I showed how to solve problems with the error “unused argument” in the R programming language. In case you have further questions, let me know in the comments.

 

Subscribe to my free statistics 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.

Menu
Top