[R] passing elements of named vector / named list to function
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Thu Jan 15 15:55:18 CET 2015
It x is a list...
do.call(fun,x)
You should keep external data input away from this code construct to avoid intentional or unintentional misuse of your fun.
If your toy example were your actual usage I would suggest the collapse argument of paste.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On January 15, 2015 6:25:43 AM PST, Rainer M Krug <Rainer at krugs.de> wrote:
>
>Hi
>
>Following scenario: I have a function fun
>
>--8<---------------cut here---------------start------------->8---
>fun <- function(A, B, C, ...){paste(A, B, C, ...)}
>--8<---------------cut here---------------end--------------->8---
>
>and x defined as follow
>
>--8<---------------cut here---------------start------------->8---
>x <- 1:5
>names(x) <- LETTERS[x]
>--8<---------------cut here---------------end--------------->8---
>
>now I want to pass the *elements* of x to fun as named arguments, i.e.
>
>,----
>| > fun(A=1, B=2, C=3, D=4, E=5)
>| [1] "1 2 3 4 5"
>`----
>
>The below examples obviously do not work:
>
>,----
>| > fun(x)
>| Error in paste(A, B, C, list(...)) :
>| argument "B" is missing, with no default
>| > fun(unlist(x))
>| Error in paste(A, B, C, list(...)) :
>| argument "B" is missing, with no default
>`----
>
>How can I extract from x the elements and pass them on to fun()?
>
>I could easily change x to a list() if this would be easier.
>
>--8<---------------cut here---------------start------------->8---
>x <- list(A=1, B=2, C=3, D=4, E=5)
>--8<---------------cut here---------------end--------------->8---
>
>In my actual program, x can have different elements as well as fun -
>this is decided programmatically.
>
>Any suggestions how I can achieve this?
>
>Thanks,
>
>Rainer
More information about the R-help
mailing list