Hi, I have two small issues with my R code, no big deal but curiosity really. Here is a sample code,> > x <- rnorm(1:10) > > foo <- function(a = 1, b = list(x = c(1:10), y = c(1:10))){ > > for (ii in seq(along=b$y)){ > > print(x[ii] + b$x[ii]) > } > > > } > > foo() # default OK > > foo(b=list(x=1, y=c(1:10))) # only the first term works > > foo(b$y = 1:5) # error >In the last call, i wish to use all default arguments but b$y (that is, using the default a and b$x). Is this possible? The second call is more related to indices: i would like my argument b $x to be either a vector (default), or a scalar. In the latter, the loop b$x[ii] breaks when i would like it to recycle the single value. I can check the length of b$x with a "if" statement, but it becomes intricate when several arguments have this option of being vector or scalar. Is there a way to use b$x that would work in both cases? Many thanks, baptiste _____________________________ Baptiste Augui? Physics Department University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag http://projects.ex.ac.uk/atto
any input? (and also, why would all my posts get bounced off while I clearly subscribed to this list?) Best regards, On 18 Feb 2008, at 09:53, baptiste Augui? wrote:> Hi, > > I have two small issues with my R code, no big deal but curiosity > really. Here is a sample code, > > >> >> x <- rnorm(1:10) >> >> foo <- function(a = 1, b = list(x = c(1:10), y = c(1:10))){ >> >> for (ii in seq(along=b$y)){ >> >> print(x[ii] + b$x[ii]) >> } >> >> >> } >> >> foo() # default OK >> >> foo(b=list(x=1, y=c(1:10))) # only the first term works >> >> foo(b$y = 1:5) # error >> > > In the last call, i wish to use all default arguments but b$y (that > is, using the default a and b$x). Is this possible? > > The second call is more related to indices: i would like my argument b > $x to be either a vector (default), or a scalar. In the latter, the > loop b$x[ii] breaks when i would like it to recycle the single value. > I can check the length of b$x with a "if" statement, but it becomes > intricate when several arguments have this option of being vector or > scalar. Is there a way to use b$x that would work in both cases? > > > Many thanks, > > baptiste > > _____________________________ > > Baptiste Augui? > > Physics Department > University of Exeter > Stocker Road, > Exeter, Devon, > EX4 4QL, UK > > Phone: +44 1392 264187 > > http://newton.ex.ac.uk/research/emag > http://projects.ex.ac.uk/atto > > ______________________________________________ > 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.
On 18/02/2008 4:53 AM, baptiste Augui? wrote:> Hi, > > I have two small issues with my R code, no big deal but curiosity > really. Here is a sample code, > > >> x <- rnorm(1:10) >> >> foo <- function(a = 1, b = list(x = c(1:10), y = c(1:10))){ >> >> for (ii in seq(along=b$y)){ >> >> print(x[ii] + b$x[ii]) >> } >> >> >> } >> >> foo() # default OK >> >> foo(b=list(x=1, y=c(1:10))) # only the first term works >> >> foo(b$y = 1:5) # error >> > > In the last call, i wish to use all default arguments but b$y (that > is, using the default a and b$x). Is this possible?No, arguments are a simple list where you use the defaults or you don't. You can't treat them as a hierarchical structure.> > The second call is more related to indices: i would like my argument b > $x to be either a vector (default), or a scalar. In the latter, the > loop b$x[ii] breaks when i would like it to recycle the single value. > I can check the length of b$x with a "if" statement, but it becomes > intricate when several arguments have this option of being vector or > scalar. Is there a way to use b$x that would work in both cases?The normal way to do this is to standardize the length of the object early in the function, e.g. len <- max(length(b$y), length(b$x)) b$y <- rep(b$y, length=len) b$x <- rep(b$x, length=len) Another way (which is the way used internally in the C implementation) is to compute an index using the recycling rules, but that's probably not efficient in R code, e.g. you'd need b$x[ (ii - 1) %% length(b$x) + 1 ] in place of b$x[ii] (and this solution is not general; it doesn't handle zero or negative indices properly). Duncan Murdoch> > > Many thanks, > > baptiste > > _____________________________ > > Baptiste Augui? > > Physics Department > University of Exeter > Stocker Road, > Exeter, Devon, > EX4 4QL, UK > > Phone: +44 1392 264187 > > http://newton.ex.ac.uk/research/emag > http://projects.ex.ac.uk/atto > > ______________________________________________ > 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.