[R] Preserving lists in a function
b.rowlingson at lancaster.ac.uk
Sat Feb 27 01:06:24 CET 2010
On Fri, Feb 26, 2010 at 10:56 PM, Shang Gao <sgao at econone.com> wrote:
> Dear R users,
> A co-worker and I are writing a function to facilitate graph plotting in R. The function makes use of a lot of lists in its defaults.
> However, we discovered that R does not necessarily preserve the defaults if we were to input them in the form of list() when initializing the function.
It does preserve the defaults, it's just that the default is a single
object. If you assign anything to that argument it becomes the full
value of the argument, as I think you discovered!
>For example, if you feed the function codes below into R:
> list1=list (variable1=1,
> list2=list (variable1="variable1",
> list3=list (variable1="character",
What I think you need to do is to replace your lists with functions.
So you'd do:
for your first argument. That gives you a way of overriding some of
do the same for the second argument with a new function with
then define your main function to get its args from the defaults of
- and then you can override bits thus:
- which only overrides the second part of the first argument, keeping
the defaults for everything else.
What you are saying by doing:
is that a1 is by default the default value of arg1f(), and similarly
for a2. It's almost like constructing an object of some type. When you
call the function you have to do a1=arg1f(v1=whatever) but that's an
advantage to just a list() call in more ways - for example your arg1f
function can check that the inputs are valid, or it can return an
object of some class you can work with.
Hope this helps.
More information about the R-help