[Rd] Proposing a change in the base::sink interface for type argument

Hamid Bazzaz hhamid at gmail.com
Thu Mar 19 04:15:25 CET 2015


Thanks Don/Kasper for the detailed explanation. Understood the idea behind
match.arg. Meanwhile, it might look handy, the part I am finding tricky is
that it makes the signature of the function misleading. That is, client
would have no idea that a particular arg is accessed through match.arg. So,
unless (A) this is well-documented or (B) one go read the implementation,
he wouldn't know the actual type of the arg is atomic not a vector. To be
fair, in the case of sink, documentation is clear that type is a character.
So, I am dropping my request. But, I probably won't use match.arg myself
given the illusion it causes in the interface of a function.

Thanks,
Hamid

On Wed, Mar 18, 2015 at 4:48 PM, Kasper Daniel Hansen <
kasperdanielhansen at gmail.com> wrote:

> 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]]



More information about the R-devel mailing list