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.

 

 

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 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.


4 Comments. Leave new

  • Clifford Mwenda
    October 26, 2021 9:05 am

    Hi, am following on the big data analytics in R and came across this code I was applying (It converted numeric ff to factor ff):
    flights.data.ff$WEEKDAY <- cut.ff(flights.data.ff$DAY_OF_WEEK, breaks = 7, labels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))

    Here is the error:
    Error in chunks(labels = c("Monday", "Tuesday", "Wednesday", "Thursday", : unused argument (labels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))

    I need your help

    Reply
    • Hey Clifford,

      I have never used the cut.ff function myself. However, it seems like it does not provide the argument labels.

      Would it be a solution to cut the vector and assign the labels afterwards?

      Regards,
      Joachim

      Reply
  • Hello! Why? I don`t understand:( Help me, please…

    df2 <- read.xlsx('C:/Stat/variant_37.xlsx',sheetIndex = 2)[,1:2]
    Error in read.xlsx("C:/Stat/variant_37.xlsx", sheetIndex = 2) :
    unused argument (sheetIndex = 2)

    Reply

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