I've written out an example of how to create a wrapper function to handle
lengthy inputs in an automated way, just in case anyone searches for "how to
pass arguments into a function as a list" and wants to see something that
works start to finish.

This can be especially helpful for use with functions like "optim" that can
have lots of arguments that you rarely change but still want to control.  In
that case "somefun" could be optim, and "somefunwrapper" could be something
like "optimCustom"

##################################################
# COMPLETE BAD EXAMPLE (FOR ILLUSTRATION)
##################################################
rm(list=ls())
somefun=function(x1,x2,x3,x4,x5,x6,x7,x8,x9){
    ans=x1+x2+x3+x4+x5+x6+x7+x8+x9
    return(ans)
}
somefun(1,2,3,4,5,6,7,8,9)
# This works, but is verbose and annoying
argsPartial=c(x3=3,x4=4,x5=5,x6=6,x7=7,x8=8,x9=9)
argsComplete=c(list(x1=1,x2=2), argsPartial)
do.call('somefun', argsComplete)

argsMod=c(list(x1=100,x2=200), argsPartial)
do.call('somefun', argsMod)


##################################################
# COMPLETE BETTER EXAMPLE
##################################################
rm(list=ls())

somefun=function(x1,x2,x3,x4,x5,x6,x7,x8,x9){
    ans=x1+x2+x3+x4+x5+x6+x7+x8+x9
    return(ans)
}

somefunwrapper=function(x1value, x2value, UsualArguments){
    SomeNewSomeOldArgs=c(list(x1=x1value,x2=x2value), UsualArguments)
    do.call('somefun', SomeNewSomeOldArgs)
}
TheUsualArgs=c(x3=3,x4=4,x5=5,x6=6,x7=7,x8=8,x9=9)
somefunwrapper(1,2,TheUsualArgs)
somefunwrapper(100,200,TheUsualArgs)

SomeNewArgs=c(x3=30,x4=40,x5=50,x6=60,x7=70,x8=80,x9=90)
somefunwrapper(1,2,SomeNewArgs)
somefunwrapper(100,200,SomeNewArgs)



By the way, thank you Henrique for getting me a little closer to this
solution.

On Mon, Apr 19, 2010 at 12:16 PM, Barry Rowlingson <
b.rowlingson@lancaster.ac.uk> wrote:

> On Mon, Apr 19, 2010 at 5:58 PM, Gene Leynes <gleynes+r@gmail.com<gleynes%2Br@gmail.com>>
> wrote:
> > Does anyone know how to pass a list of parameters into a function?
> >
> >
> > for example:
> >
> > somefun=function(x1,x2,x3,x4,x5,x6,x7,x8,x9){
> >    ans=x1+x2+x3+x4+x5+x6+x7+x8+x9
> >    return(ans)
> > }
> >
> > somefun(1,2,3,4,5,6,7,8,9)
> >
> > # I would like this to work:
> > temp=c(x3=3,x4=4,x5=5,x6=6,x7=7,x8=8,x9=9)
> > somefun(x1=1,x2=2,temp)
> >
> > # OR I would like this to work:
> > temp=list(x3=3,x4=4,x5=5,x6=6,x7=7,x8=8,x9=9)
> > somefun(x1=1,x2=2,temp)
>
>  Why?
>
>  These are the kind of things that should only be done by people who
> know how to do them and hence know not to do them. A bit like the
> definition of a gentleman being someone who knows how to play the
> bagpipes but chooses not to.
>
>  You can do this, but it requires all sorts of fiddling of the
> argument lists which would make the functions very messy, and most
> probably unlike any other R functions the user has encountered.
>
>  But... for starters you might want to look at the '...' argument
> which gives some flexibility in argument handling, something like:
>
>  foo = function(...){
>   return(list(...))
> }
>
>  then try foo(x1=1,x2=2) and so on. See what you get back. Then work
> out how to add them all up, check they are all x1 to x9 and so on. And
> recursively unwrap your 'temp' variable by testing if it is atomic or
> not.
>
>  I'm off to play the bagpipes now.
>
> Barry
>

	[[alternative HTML version deleted]]

