Mario José Marques-Azevedo
2015-Dec-11 18:52 UTC
[R] Weird behaviour function args in ellipses
Hi Duncan and David, Thank you for explanation. I'm really disappointed with this R "resource". I think that partial match, mainly in function args, must be optional and not default. We can have many problems and lost hours find errors (it occur with me). I tried to find a solution to disable partial match, but it seems that is not possible. Program with hacks for this will be sad. Best regards! Mario On 11 December 2015 at 15:55, David Winsemius <dwinsemius at comcast.net> wrote:> > > On Dec 11, 2015, at 9:40 AM, Mario Jos? Marques-Azevedo < > mariojmaaz at gmail.com> wrote: > > > > Dears, > > > > I'm having a weird behaviours when setting arguments in functions. > > > > fn <- function(x, st="mean", b=NULL, col.range="black", ...){ > > dots <- list(...) > > cat("col.range =", col.range, "\n") > > cat("dots =\n") > > print(dots) > > } > > > > fn(1, b=2,col="red") > > > > # Output > > col.range = red > > Argument matching is done with `pmatch`. So "col" is a partial match to > "col.range". > > See ?match.arg > > > > dots > > list() > > > > Why 'col' arguments are not in ellipses, but setting col.range to 'red'? > > > > If I change the position of ellipses > > > > fn2 <- function(x, ..., st="mean", b=NULL, col.range="black"){ > > dots <- list(...) > > cat("col.range =", col.range, "\n") > > cat("dots =\n") > > print(dots) > > } > > > > fn2(1, b=2, col="red") > > > > # Output > > col.range = black > > dots > > $col > > [1] "red" > > > > It works! I'm using R version 3.2.2. > > It's been that way 'forever'. > > > > > Best regards! > > > > Mario > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > David Winsemius > Alameda, CA, USA > >[[alternative HTML version deleted]]
On 11/12/2015 1:52 PM, Mario Jos? Marques-Azevedo wrote:> Hi Duncan and David, > > Thank you for explanation. I'm really disappointed with this R "resource". > I think that partial match, mainly in function args, must be optional and > not default. We can have many problems and lost hours find errors (it occur > with me). I tried to find a solution to disable partial match, but it seems > that is not possible. Program with hacks for this will be sad.Nowadays with smart editors, I agree that partial matching isn't really necessary. However, R has been around for 20 years, and lots of existing code depends on it. Eventually you'll get to know the quirks of the design. Duncan Murdoch> > Best regards! > > Mario > > > > On 11 December 2015 at 15:55, David Winsemius <dwinsemius at comcast.net> > wrote: > > > > > > On Dec 11, 2015, at 9:40 AM, Mario Jos? Marques-Azevedo < > > mariojmaaz at gmail.com> wrote: > > > > > > Dears, > > > > > > I'm having a weird behaviours when setting arguments in functions. > > > > > > fn <- function(x, st="mean", b=NULL, col.range="black", ...){ > > > dots <- list(...) > > > cat("col.range =", col.range, "\n") > > > cat("dots =\n") > > > print(dots) > > > } > > > > > > fn(1, b=2,col="red") > > > > > > # Output > > > col.range = red > > > > Argument matching is done with `pmatch`. So "col" is a partial match to > > "col.range". > > > > See ?match.arg > > > > > > > dots > > > list() > > > > > > Why 'col' arguments are not in ellipses, but setting col.range to 'red'? > > > > > > If I change the position of ellipses > > > > > > fn2 <- function(x, ..., st="mean", b=NULL, col.range="black"){ > > > dots <- list(...) > > > cat("col.range =", col.range, "\n") > > > cat("dots =\n") > > > print(dots) > > > } > > > > > > fn2(1, b=2, col="red") > > > > > > # Output > > > col.range = black > > > dots > > > $col > > > [1] "red" > > > > > > It works! I'm using R version 3.2.2. > > > > It's been that way 'forever'. > > > > > > > > Best regards! > > > > > > Mario > > > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > > > David Winsemius > > Alameda, CA, USA > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
I normally don't respond to this sort of rant, but I will here. Feel free to ignore. My response is: sour grapes! **I'm really disappointed** that you failed to read (carefully) the documents (R language manual) or tutorials (numerous and ubiquitous) that clearly explain this. IMO, you are blaming others for your failings. If you care to respond, yours will be the last word. I will not comment further (and probably shouldn't have here). Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Dec 11, 2015 at 10:52 AM, Mario Jos? Marques-Azevedo <mariojmaaz at gmail.com> wrote:> Hi Duncan and David, > > Thank you for explanation. I'm really disappointed with this R "resource". > I think that partial match, mainly in function args, must be optional and > not default. We can have many problems and lost hours find errors (it occur > with me). I tried to find a solution to disable partial match, but it seems > that is not possible. Program with hacks for this will be sad. > > Best regards! > > Mario > > > > On 11 December 2015 at 15:55, David Winsemius <dwinsemius at comcast.net> > wrote: > >> >> > On Dec 11, 2015, at 9:40 AM, Mario Jos? Marques-Azevedo < >> mariojmaaz at gmail.com> wrote: >> > >> > Dears, >> > >> > I'm having a weird behaviours when setting arguments in functions. >> > >> > fn <- function(x, st="mean", b=NULL, col.range="black", ...){ >> > dots <- list(...) >> > cat("col.range =", col.range, "\n") >> > cat("dots =\n") >> > print(dots) >> > } >> > >> > fn(1, b=2,col="red") >> > >> > # Output >> > col.range = red >> >> Argument matching is done with `pmatch`. So "col" is a partial match to >> "col.range". >> >> See ?match.arg >> >> >> > dots >> > list() >> > >> > Why 'col' arguments are not in ellipses, but setting col.range to 'red'? >> > >> > If I change the position of ellipses >> > >> > fn2 <- function(x, ..., st="mean", b=NULL, col.range="black"){ >> > dots <- list(...) >> > cat("col.range =", col.range, "\n") >> > cat("dots =\n") >> > print(dots) >> > } >> > >> > fn2(1, b=2, col="red") >> > >> > # Output >> > col.range = black >> > dots >> > $col >> > [1] "red" >> > >> > It works! I'm using R version 3.2.2. >> >> It's been that way 'forever'. >> >> > >> > Best regards! >> > >> > Mario >> > >> > [[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> >> David Winsemius >> Alameda, CA, USA >> >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Fri, Dec 11, 2015 at 1:13 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 11/12/2015 1:52 PM, Mario Jos? Marques-Azevedo wrote: >> >> Hi Duncan and David, >> >> Thank you for explanation. I'm really disappointed with this R "resource". >> I think that partial match, mainly in function args, must be optional and >> not default. We can have many problems and lost hours find errors (it >> occur >> with me). I tried to find a solution to disable partial match, but it >> seems >> that is not possible. Program with hacks for this will be sad. > > > Nowadays with smart editors, I agree that partial matching isn't really > necessary. However, R has been around for 20 years, and lots of existing > code depends on it. Eventually you'll get to know the quirks of the > design.And if you really dislike this behavour, you can at least warn on it: options( warnPartialMatchArgs = TRUE, warnPartialMatchAttr = TRUE, warnPartialMatchDollar = TRUE ) Hadley -- http://had.co.nz/
> On 11 Dec 2015, at 20:13 , Duncan Murdoch <murdoch.duncan at gmail.com> wrote: > > On 11/12/2015 1:52 PM, Mario Jos? Marques-Azevedo wrote: >> Hi Duncan and David, >> >> Thank you for explanation. I'm really disappointed with this R "resource". >> I think that partial match, mainly in function args, must be optional and >> not default. We can have many problems and lost hours find errors (it occur >> with me). I tried to find a solution to disable partial match, but it seems >> that is not possible. Program with hacks for this will be sad. > > Nowadays with smart editors, I agree that partial matching isn't really necessary. However, R has been around for 20 years, and lots of existing code depends on it. Eventually you'll get to know the quirks of the design.Yes, partial matching is largely regretted by its inventors, but it is hard to get rid of due to various arcane bits of history. For instance, have you noticed that seq() doesn't have a "length" argument? I.e.> seq(2, length=3)[1] 2 3 4 actually only works due to partial matching -- the argument is really "length.out". Now, looking that the code for seq.default offers a hint at the: It uses things like length(by) internally. In ancient times (in S) that would give you a type mismatch, but one of the things that R changed was to have function lookup disregard non-function objects. In other places, some argument names have a period added for similar reasons. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com