Hamid Bazzaz
2015-Mar-17 05:08 UTC
[Rd] Proposing a change in the base::sink interface for type argument
Hi folks, Here is the current interface: sink(file=NULL, append=FALSE, type = c("output", "message"), split=FALSE) However, reading the implementation there is implicit assumption that type is a single character value: https://github.com/wch/r-source/blob/trunk/src/library/base/R/sink.R#L23 I'm finding this very confusing as the interface is giving a default value of a character _vector_ causing the illusion that by default both output/message will be redirected. I'm proposing either a change in the interface so it is a single character (either output or message) or a loop in the implementation on all values in type so it will actually be considered a vector. Here is an example change for the former: https://github.com/hhamid/r-source/commit/d3cad22e1b9beca0a55004d74fc95059c279d770#diff-498a99501a04c6d9a66ee95ad6614734L19 Just wondering what people think and if this makes sense. Thanks a lot, Hamid [[alternative HTML version deleted]]
MacQueen, Don
2015-Mar-18 21:42 UTC
[Rd] Proposing a change in the base::sink interface for type argument
It's only an illusion until one actually tries providing a vector. > sink('foo', type=c('s','m')) Error in match.arg(type) : 'arg' must be of length 1 The additional benefit of match.arg() which you may have not appreciated is that it allows the user to abbreviate. That is, > sink('foo', type='o') is valid usage. The essential concept of match.arg() is that it tries to match whatever the user supplied with one, and only one, of the values provided in the argument's default value, and that is the value used in the rest of the function. For example:> foo <- function(arg=c('aa','bb','cc')) cat(match.arg(arg),'\n') > foo('a')aa> foo('b')bb> foo('x')Error in match.arg(arg) : 'arg' should be one of "aa", "bb", "cc"> > foo('aa')aa Being "non-intuitive" or puzzling to people coming from other languages is not a sufficient reason for a change. Obviously, different languages have different features, otherwise, why bother to have different languages? And yes, match.arg() is widely used in R. I find it quite useful in my own programming. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 3/16/15, 10:08 PM, "Hamid Bazzaz" <hhamid at gmail.com> wrote:>Hi folks, > >Here is the current interface: > >sink(file=NULL, append=FALSE, type = c("output", "message"), split=FALSE) > >However, reading the implementation there is implicit assumption that type >is a single character value: >https://github.com/wch/r-source/blob/trunk/src/library/base/R/sink.R#L23 > >I'm finding this very confusing as the interface is giving a default value >of a character _vector_ causing the illusion that by default both >output/message will be redirected. > >I'm proposing either a change in the interface so it is a single character >(either output or message) or a loop in the implementation on all values >in >type so it will actually be considered a vector. Here is an example change >for the former: > >https://github.com/hhamid/r-source/commit/d3cad22e1b9beca0a55004d74fc95059 >c279d770#diff-498a99501a04c6d9a66ee95ad6614734L19 > >Just wondering what people think and if this makes sense. > >Thanks a lot, >Hamid > > [[alternative HTML version deleted]] > >______________________________________________ >R-devel at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-devel
Kasper Daniel Hansen
2015-Mar-18 23:48 UTC
[Rd] Proposing a change in the base::sink interface for type argument
In other words: this is a standard programming paradigm in R/S which (unfortunately) is not widely known, based on my network. It is really nice for developers. Best, Kasper On Wed, Mar 18, 2015 at 5:42 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:> It's only an illusion until one actually tries providing a vector. > > > sink('foo', type=c('s','m')) > Error in match.arg(type) : 'arg' must be of length 1 > > The additional benefit of match.arg() which you may have not appreciated > is that it allows the user to abbreviate. That is, > > sink('foo', type='o') > is valid usage. The essential concept of match.arg() is that it tries to > match whatever the user supplied with one, and only one, of the values > provided in the argument's default value, and that is the value used in > the rest of the function. > > For example: > > foo <- function(arg=c('aa','bb','cc')) cat(match.arg(arg),'\n') > > foo('a') > aa > > foo('b') > bb > > foo('x') > Error in match.arg(arg) : 'arg' should be one of "aa", "bb", "cc" > > > > foo('aa') > aa > > > > > Being "non-intuitive" or puzzling to people coming from other languages is > not a sufficient reason for a change. Obviously, different languages have > different features, otherwise, why bother to have different languages? > > And yes, match.arg() is widely used in R. I find it quite useful in my own > programming. > > > -- > Don MacQueen > > Lawrence Livermore National Laboratory > 7000 East Ave., L-627 > Livermore, CA 94550 > 925-423-1062 > > > > > > On 3/16/15, 10:08 PM, "Hamid Bazzaz" <hhamid at gmail.com> wrote: > > >Hi folks, > > > >Here is the current interface: > > > >sink(file=NULL, append=FALSE, type = c("output", "message"), split=FALSE) > > > >However, reading the implementation there is implicit assumption that type > >is a single character value: > >https://github.com/wch/r-source/blob/trunk/src/library/base/R/sink.R#L23 > > > >I'm finding this very confusing as the interface is giving a default value > >of a character _vector_ causing the illusion that by default both > >output/message will be redirected. > > > >I'm proposing either a change in the interface so it is a single character > >(either output or message) or a loop in the implementation on all values > >in > >type so it will actually be considered a vector. Here is an example change > >for the former: > > > > > https://github.com/hhamid/r-source/commit/d3cad22e1b9beca0a55004d74fc95059 > >c279d770#diff-498a99501a04c6d9a66ee95ad6614734L19 > > > >Just wondering what people think and if this makes sense. > > > >Thanks a lot, > >Hamid > > > > [[alternative HTML version deleted]] > > > >______________________________________________ > >R-devel at r-project.org mailing list > >https://stat.ethz.ch/mailman/listinfo/r-devel > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]