"Jens Oehlschlägel"
2004-Feb-27 13:10 UTC
[R] Is there a way to deactivate partial matching in R?
Dear R-experts, I just tracked down a nasty bug in a dynamically parametrized function to wrong argument matching. As we get more and more complex applications build on top of R (like bioconductor) partial matching gets more and more dangerous. I would like to deactivate partial matching in R (partial argument matching as well as partial matching of list elements), e.g. using an environment variable. If this is currently not possible this would be my current most important wishlist topic. Best regards Jens Oehlschl?gel --
Marc Schwartz
2004-Feb-27 13:51 UTC
[R] Is there a way to deactivate partial matching in R?
On Fri, 2004-02-27 at 07:10, "Jens Oehlschl?gel" wrote:> Dear R-experts, > > I just tracked down a nasty bug in a dynamically parametrized function to > wrong argument matching. As we get more and more complex applications build on > top of R (like bioconductor) partial matching gets more and more dangerous. I > would like to deactivate partial matching in R (partial argument matching as > well as partial matching of list elements), e.g. using an environment > variable. If this is currently not possible this would be my current most important > wishlist topic.As a temporary solution for the argument matching issue, you could modify the code for match.arg() and add a T/F 'exact' argument, thus using either match() or pmatch(), the latter of which is the present default. match.arg() is a fairly short function. my.match.arg <- function (arg, choices, exact = FALSE) { if (missing(choices)) { formal.args <- formals(sys.function(sys.parent())) choices <- eval(formal.args[[deparse(substitute(arg))]]) } if (all(arg == choices)) return(choices[1]) # HERE IS THE MODIFIED CODE if (exact) i <- match(arg, choices) else i <- pmatch(arg, choices) # END MODIFIED CODE if (is.na(i)) stop(paste("ARG should be one of", paste(choices, collapse = ", "), sep = " ")) if (length(i) > 1) stop("there is more than one match in match.arg") choices[i] } I did not add any additional error checking code here or how you want to handle non-matches, since that maybe unique to your application. I am not sure what you are using for list element matching (charmatch?), but a similar approach can feasibly be taken there, keeping in mind that charmatch() is a .Internal. In terms of global variables, you can always add one to your environment (ie. using .Rprofile). In that case, you could use the following in place of the four lines above, after setting options(exact) to a default value (ie. options(exact = TRUE) ): if (options()$exact) i <- match(arg, choices) else i <- pmatch(arg, choices) You would of course need to ensure that options()$exact is unique based upon the addition of non-base packages and then leave off the 'exact' argument as I have the function defined above. I hope that this helps, keeping in mind I am only on my first cup of coffee so far this morning... :-) Marc Schwartz
Prof Brian Ripley
2004-Feb-27 14:36 UTC
[Rd] Re: [R] Is there a way to deactivate partial matching in R?
[Moved to R-devel, as that is where `wishlist' items fit best.] It is not currently possible. Furthermore, the R base code is riddled with uses of partial argument matching (check out the uses of rep(), for example) and partial list matching, although from time to time I try to eradicate a bunch of them. I don't see why in programatic uses of R you cannot always guarantee exact matching (which will always win), if necessary by having ... as the first formal argument to your function. Can you give us an example of the need to disable partial matching? On Fri, 27 Feb 2004, "Jens Oehlschl?gel" wrote:> > Dear R-experts, > > I just tracked down a nasty bug in a dynamically parametrized function to > wrong argument matching. As we get more and more complex applications build on > top of R (like bioconductor) partial matching gets more and more dangerous. I > would like to deactivate partial matching in R (partial argument matching as > well as partial matching of list elements), e.g. using an environment > variable. If this is currently not possible this would be my current most important > wishlist topic. > > Best regards > > > Jens Oehlschl?gel-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595