[R] How to convert List object to function arguments?

Shiazy shiazy at gmail.com
Sat Mar 3 16:34:06 CET 2007


"do.call" createa a function call and evaluates it. I think this isn't 
for me. Same thing for "call"; it wants the function name as first argument.

However, I have to pass to "ks.test" the CDF function name and, 
separately, the related arguments.

ks.test( x, cdfname, ... );

The problem is "..." comes as a list object and I didn't find a way to 
"coerce" this object to a arguments list, preserving names a values 
(note each list item can be a complex object such as a vector, matrix, 
... so I cannot use unlist o as.array)

-- Marco

hadley wickham wrote:
> Have a look at do.call
> 
> Hadley
> 
> On 3/3/07, Shiazy <shiazy at gmail.com> wrote:
>> Dear R gurus,
>>
>> I have a function "goftests" that receives the following arguments:
>> * a vector "x" of data values;
>> * a distribution name "dist";
>> * the dots list ("...") containing a list a parameters to pass to CDF
>> function;
>> and calls several goodness-of-fit tests on the given data values against
>> the given distribution.
>> That is:
>>
>> ##### BEGIN CODE SNIP #####
>>
>> # Covert a distribution name to the related CDF function name
>> distname2cdfname <- function( dist )
>> {
>>    if ( dist == "beta" ) { return( "pbeta" ); }
>>    if ( dist == "cauchy" ) { return( "pcauchy" ); }
>>
>>    # many other and personalized (e.g. pareto, ...) distributions ...
>>
>>    return( "" ); # fall-back case
>> }
>>
>> # Performs some GoF tests (KS, Chi-Square, Anderson-Darling,...)
>> # (the "..." list contains parameters for CDF function)
>> goftests <- function( x, dist, ... )
>> {
>>    cdfname <- distname2cdfname( dist );
>>    res$ks <- ks.test( x, cdfname, ... );
>>    # res$chisq <- ...
>>    # res$anddarl <- ...
>>
>>    return( res )
>> };
>>
>> ##### END CODE SNIP #####
>>
>> So the problem is passing "..." to CDF function (for instance, see
>> ks.test above) in the "right form". The "..." is passed (to "goftests")
>> as a list object and it should be converted to an "argument list".
>> For instance:
>>
>> ##### BEGIN CODE SNIP #####
>>
>> parms <- list( shape1 = 2, shape2 = 5 );
>> goftests( x, "beta", parms[["shape1"]], parms[["shape2"]] ); # Works!
>> goftests( x, "beta", parms ) # Don't Works!
>>
>> parms <- list( aNum = 5, aVector = c(1,2,3), aMatrix =
>> matrix(c(4,5,6,7,8,9),nrow=2,byrow=T) );
>> goftests( x, "my-special-distr", parms[["aVector"]], parms[["aMatrix"]],
>> parms[["aNumber"]] ); # Works!
>> goftests( x, "my-special-distr", parms ) # Don't Works!
>>
>> ##### END CODE SNIP #####
>>
>> Obviously I have to use the "don't work" form.
>>
>> How can I do this in R (if possible)?
>>
>> Thank you very much in advance!
>>
>> Best regards,
>>
>> -- Marco
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> 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.
>>
>



More information about the R-help mailing list