[Rd] question

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sun Mar 8 14:56:13 CET 2009


ivowel at gmail.com wrote:
> Gentlemen---these are all very clever workarounds, but please forgive me  
> for voicing my own opinion: IMHO, returning multiple values in a  
> statistical language should really be part of the language itself. there  
> should be a standard syntax of some sort, whatever it may be, that everyone  
> should be able to use and which easily transfers from one local computer to  
> another. It should not rely on clever hacks in the .Rprofile that are  
> different from user to user, and which leave a reader of end user R code  
> baffled at first by all the magic that is going on. Even the R tutorials  
> for beginners should show a multiple-value return example right at the  
> point where function calls and return values are first explained.
>
>   

hi again,

i was playing a bit with the idea of multiple assignment, and came up
with a simple codebit [1] that redefines the operator '='.  it hasn't
been extensively tested and is by no means foolproof, but allows various
sorts of tricks with multiple assignments:

    source('http://miscell.googlecode.com/svn/rvalues/rvalues.r',
local=TRUE)

    a = function(n) 1:n
    # a is a function

    b = a(3)
    # b is c(1, 2, 3)

    c(c, d) = a(1)
    # c is 1, d is NULL

    c(a, b) = list(b, a)
    # swap: a is 1:3, b is a function

    # these are equivalent:
    c(a, b) = 1:2
    {a; b} = 1:2
    list(a, b) = 1:2

    a = data.frame(x=1:3, y=3)
    # a is a 2-column data frame

    c(a, b) = data.frame(x=1:3, b=3)
    # a is c(1, 2, 3), b is c(3, 3, 3)

and so on.  this is sort of pattern matching as in some functional
languages, but only sort of:  it does not do recursive matching, for
example:

    c(c(a, b), c) = list(1:2, 3)
    # error
    # not: a = 1, b = 2, c = 3
 
anyway, it's just a toy for which there is no need.

vQ


[1] svn checkout */http/*://miscell.googlecode.com/svn/rvalues



More information about the R-devel mailing list