An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120515/8928ae75/attachment.pl>
pass objects into "..." (dot dot dot)
7 messages · Ben quant, R. Michael Weylandt, Steve Lianoglou +1 more
Hi,
On Tue, May 15, 2012 at 12:46 PM, Ben quant <ccquant at gmail.com> wrote:
Hello, Thanks in advance for any help! How do I pass an unknown number of objects into the "..." (dot dot dot) parameter? Put another way, is there some standard way to pass multiple objects into "..." to "fool" the function into thinking the objects are passed in separately/explicitly with common separation (like "x,y,z" when x, y and z are objects to be passed into "...")?
Calling `list(...)` will return a list as long as there are elements
caught in `...`
Does this help?
R> howMany <- function(...) {
args <- list(...)
cat("There are", length(args), "items passed in here\n")
}
R> howMany(1, 2, 3, 4)
There are 4 items passed in here
R> howMany(10, list(1:10))
There are 2 items passed in here
-steve
Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120515/1bad9d54/attachment.pl>
Are you perhaps looking for the do.call() construction?
z = Intervals(c(1,10))
y = Intervals(c(5,10))
x = Intervals(c(4,6))
do.call("interval_intersection", list(x,y,z))
Michael
On Tue, May 15, 2012 at 12:46 PM, Ben quant <ccquant at gmail.com> wrote:
Hello,
Thanks in advance for any help!
How do I pass an unknown number of objects into the "..." (dot dot dot)
parameter? Put another way, is there some standard way to pass multiple
objects into "..." to "fool" the function into thinking the objects are
passed in separately/explicitly with common separation (like "x,y,z" when
x, y and z are objects to be passed into "...")?
Details:
I'm working with this parameter list and function:
interval_intersection(x, ..., check_valid = TRUE)
To illustrate...
This works and I get the expected interval:
library('intervals')
# create individual Intervals objects
z = Intervals(c(1,10))
y = Intervals(c(5,10))
x = Intervals(c(4,6))
interval_intersection(x,y,z)
Object of class Intervals 1 interval over R: [5, 6] ...but at run time I don't know how many Intervals objects I will have so I can't list them explicitly like this "x,y,z". So I build a matrix of Intervals (per the package manual) and the function doesn't work:
xyz = matrix(c(4,5,1,6,10,10),nrow=3) xyz
? ? [,1] [,2] [1,] ? ?4 ? ?6 [2,] ? ?5 ? 10 [3,] ? ?1 ? 10
xyz_interval = Intervals(xyz) interval_intersection(xyz_interval)
Object of class Intervals 1 interval over R: [1, 10] ...[1,10] is unexpected/wrong because I want the intersection of the three intervals. So I conclude that I need to pass in the individual Intervals objects, but how do I do that if I don't know how many I have at run time? I tried putting them in a list, but that didn't work. I also tried using paste( ? ?,sep=',') and get(). Is there some standard way to pass multiple objects into "..." to "fool" the function into thinking they are passed in separately/explicitly with common separation? Thanks! ben ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
Hi,
On Tue, May 15, 2012 at 1:38 PM, Ben quant <ccquant at gmail.com> wrote:
Thank you for that. Sorry, I don't know how to use that to solve the issue. I need to pass a handful (an unknown length) of objects into "...". I see how you can get the count of what is in "...", but I'm not seeing how knowing the length in "..." will help me.
Hmm ... ok, I see. The interval_intersection function signature
suggests that this should work:
myIntersection <- function(...) {
do.call(interval_intersection, unname(list(...)))
}
Or you can just make a list of intervals and do the same do.call mojo, ie:
do.call(interval_intersection, my.interval.list)
yay/nay?
-steve
Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120515/1e542a74/attachment.pl>
You can also just pass the ... to the next function. E.g., the following two functions do the same thing: > myPaste1 <- function(...) paste(...) > myPaste2 <- function(...) do.call(paste, list(...)) > myPaste1(1,2:3,4) [1] "1 2 4" "1 3 4" > myPaste2(1,2:3,4) [1] "1 2 4" "1 3 4" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Steve Lianoglou Sent: Tuesday, May 15, 2012 10:49 AM To: Ben quant Cc: r-help at r-project.org Subject: Re: [R] pass objects into "..." (dot dot dot) Hi, On Tue, May 15, 2012 at 1:38 PM, Ben quant <ccquant at gmail.com> wrote:
Thank you for that. Sorry, I don't know how to use that to solve the issue. I need to pass a handful (an unknown length) of objects into "...". I see how you can get the count of what is in "...", but I'm not seeing how knowing the length in "..." will help me.
Hmm ... ok, I see. The interval_intersection function signature
suggests that this should work:
myIntersection <- function(...) {
do.call(interval_intersection, unname(list(...)))
}
Or you can just make a list of intervals and do the same do.call mojo, ie:
do.call(interval_intersection, my.interval.list)
yay/nay?
-steve
--
Steve Lianoglou
Graduate Student: Computational Systems Biology
?| Memorial Sloan-Kettering Cancer Center
?| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
______________________________________________ R-help at r-project.org 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.