[Rd] dubious behaviour of match.arg() with nested functions.

Berend Hasselman bhh at xs4all.nl
Mon Aug 25 18:49:49 CEST 2014

On 25-08-2014, at 16:27, Joris Meys <jorismeys at gmail.com> wrote:

> Dear all,
> I initially ran into this problem while rebuilding a package dependent on
> nleqslv. I got the following error:
> Error in match.arg(global) : 'arg' must be of length 1
> This didn't occur in previous versions of nleqslv, but did in the current
> one (2.4). I think I pinned the problem down to the following example:
> Take two functions:
> test <- function(x=c("q","r","s"),global=c("d","e","r","z","q")){
>  x <- match.arg(x)
>  global <- match.arg(global)
>  return(list(x,global))
> }
> test2 <- function(x=c("q","r","s"),global=c("d","z","q")){
>  test(x=x,global=global)
> }

Can’t  the problem easily be avoided by using a slightly modified version of the test2() function?

test2 <- function(x=c("q","r","s"),global=c("d","z","q")){
 x <- match.arg(x)
global <- match.arg(global)

Then test2() would be calling test() as it was intended to be called.
I’ve tried that and it appears to solve the problem.

So before calling nleqslv within another function use match.arg on those arguments with the same name and different defaults.
And (possibly) document that some arguments expect a single value.


More information about the R-devel mailing list