R Error Message: unused argument

The “unused argument error in r” error message is primarily a coding mistake, a fact that makes it easy to find and correct. It results from incorrect entry of arguments into a function. It usually results from an easy-to-make mistake and one that is just as easy to correct. It can result from something as simple as hitting the wrong key. In most cases, once you find the mistake fixing the problem is easy.

The circumstances of this error.

The “unused argument error in r” message occurs when you are using a function in R. It occurs when an argument used does not match the arguments in the argument list. This is particularly true when a different argument is added than what is in the list. It does not occur just because an argument without a default argument value is missing because this causes a different message than a missing default value error.

# r error unused argument
> log(x=8)
[1] 2.079442
> log(x=8,y=7)
Error in log(x = 8, y = 7) : unused argument (y = 7)

It can happen with both built-in and user-defined functions. In both cases, this message occurs when a select unused argument is included in a function’s input. Anytime you include an unused argument in calling an r function you will get this error message.

What is causing this problem?

The “unused argument error in r” message is caused by a mistake in the code when entering an argument or object such as a data frame, vector, or matrix into a base r function. It is not a case of missing values or data but rather a variable that is not expected as an additional argument. It is easiest to illustrate this problem by using a user-defined r function argument.

# unused argument error in r
> expn = function(x,n)
+ {
+ xx = 1
+ for (i in 1:n) xx = xx*x
+ xx
+ }
> expn(x = 4,n =3)
[1] 64
> expn(x = 4,n =3,s = 5 )
Error in expn(x = 4, n = 3, s = 5) : unused argument (s = 5)
> expn(x = 4,s = 5 )
Error in expn(x = 4, s = 5) : unused argument (s = 5)

In this example, the function definition raises “x” to the power of “n” to get the result as long as you enter only the “x” and “n” arguments. However, as you can see entering a third argument triggers our message. Furthermore, entering another function argument in place of an existing single argument also triggers our return value error message.

How to solve this error

Fixing the “unused argument error in r” message is quite simple, although there are several options you can use. In the first approach you can just make sure you get the correct argument names, this is the only solution for an existing r function but it can be used for a user-made one as well. Whether you are dealing with a vector, file, or some form of output command if you did not write the r function this is the only solution you have.

# r unused argument error solution
> expn = function(x,n)
+ {
+ xx = 1
+ for (i in 1:n) xx = xx*x
+ xx
+ }
> expn(4, 5)
[1] 1024

While this example is user-defined it illustrates this approach quite well. In this case, we also exclude the labels making typing the wrong labels impossible.

# unused argument error r solution
> exps = function(x,n,s)
+ {
+ xx = 1
+ for (i in 1:n) xx = xx*x
+ xx+s
+ }
> exps(x = 4,n =3,s = 5 )
[1] 69

The other option works only with a user-defined function, and that is to add additional unknown arguments to simply absorb the syntax mistake. This is a practical solution if you have repeated the same single argument mistake several times. By the way, you do not actually have to use it to fix the problem.

This is a fairly simple problem to solve. If you wrote the base r function, then you have additional dataset options that you would not have if it were predefined. This problem may result from an easy to make mistake but it is easy to fix. It is part of the learning process of R programming.

R Error Message: unused argument

Scroll to top