[Rd] Re: [R] Is there a way to deactivate partial matching in R?
"Jens Oehlschlägel"
joehl at gmx.de
Fri Feb 27 19:15:46 MET 2004
Dear Prof. Ripley,
Thank you for reminding me to the possibility to place the dots as first
argument.
If this is the solution against partial argument matching, we e.g. cannot
safely define a plot method which gives a demo plot in case of no object
(because we have to obey the class paramter conventions).
plot.someobject <- function(x=NULL, ...)
# another example would be paramter pairs where the second one is optional
like in
plot.someobject(xAndOry, yOptional=NULL, ...) # the standard plot(x,
y=NULL, ...) is a safe exception
# I agree that such examples can be avoided, but I ran into my problem after
6 years experience with S.
# My actual example had the following structure
makeoutput <- function(outchannel=NULL, ...){
if (is.null(outchannel))
outchannel <- activechannel
## for each in ... output to outchannel
}
# as outchannel was usually specified and only occasionally the default
used,
# it was more 'natural' (and lazy) to call it
makeoutput(mychannel, ...)
# compared to
makeoutput(..., outchannel=mychannel)
# standard use is
makeoutput(thischannel, element1name=element1content,
element2name=element2content)
# with an option to send to the active channel as default called like
makeoutput(element1name=element1content, element2name=element2content)
# calling this as
do.call("makeoutput", ll) # where ll <- list(out=outcontent,
whatever=whatevercontent)
# actually generates the nonsense call
makeoutput(outchannel=outcontent, whatever=whatevercontent)
# I still find it difficult to write reliable code in R.
# I am always afraid to run into something like
> # simple example for dangerous partial list matching
>
> increment.counters <- function(..., mycounters){
+ inclist <- list(...)
+ for (i in seq(along=inclist))
+ mycounters[[names(inclist[i])]] <- mycounters[[names(inclist[i])]] +
inclist[[i]]
+ mycounters
+ }
>
> mycounters <- list(leftcounter=0, rightcounter=0)
> # intended use
> mycounters <- increment.counters(leftcounter=4, mycounters=mycounters)
> mycounters
$leftcounter
[1] 4
$rightcounter
[1] 0
>
> # now let me have a typo
> mycounters <- increment.counters(leftcounte=4, mycounters=mycounters)
> # and my increment silently misses its target, and I have an additional
nonsense counter
> mycounters
$leftcounter
[1] 4
$rightcounter
[1] 0
$leftcounte
[1] 8
Best
Jens Oehlschlägel
P.S. I am not subscribed to r-devel
--
More information about the R-devel
mailing list