[R] Is there a way to deactivate partial matching in R?
Marc Schwartz
MSchwartz at medanalytics.com
Fri Feb 27 14:51:02 CET 2004
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
More information about the R-help
mailing list