Is it possible to have a the software ignore the fact that there are unused arguments defined when a module is run?
For example, I have a module multiply(a,b)
, which returns the product of a
and b
. I will receive an error if I call the module like so:
multiply(a=20,b=30,c=10)
Returning an error on this just seems a bit unnecessary, since the required inputs a
and b
have been specified. Is it possible to avoid this bad behaviour?
An easy solution would be just to stop specifying c
, but that doesn't answer why R behaves like this. Is there another way to solve this?
Change the definition of multiply to take additional unknown arguments:
multiply <- function(a, b, ...) { # Original code }
The R.utils package has a function called doCall which is like do.call, but it does not return an error if unused arguments are passed.
multiply <- function(a, b) a * b # these will fail multiply(a = 20, b = 30, c = 10) # Error in multiply(a = 20, b = 30, c = 10) : unused argument (c = 10) do.call(multiply, list(a = 20, b = 30, c = 10)) # Error in (function (a, b) : unused argument (c = 10) # R.utils::doCall will work R.utils::doCall(multiply, args = list(a = 20, b = 30, c = 10)) # [1] 600 # it also does not require the arguments to be passed as a list R.utils::doCall(multiply, a = 20, b = 30, c = 10) # [1] 600
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With