Henrik Bengtsson
2018-Aug-12 20:00 UTC
[Rd] substitute() on arguments in ellipsis ("dot dot dot")?
Hi. For any number of *known* arguments, we can do: one <- function(a) list(a = substitute(a)) two <- function(a, b) list(a = substitute(a), b = substitute(b)) and so on. But how do I achieve the same when I have: dots <- function(...) list(???) I want to implement this such that I can do:> exprs <- dots(1+2) > str(exprs)List of 1 $ : language 1 + 2 as well as:> exprs <- dots(1+2, "a", rnorm(3)) > str(exprs)List of 3 $ : language 1 + 2 $ : chr "a" $ : language rnorm(3) Is this possible to achieve using plain R code? Thanks, Henrik
Duncan Murdoch
2018-Aug-12 20:16 UTC
[Rd] substitute() on arguments in ellipsis ("dot dot dot")?
On 12/08/2018 4:00 PM, Henrik Bengtsson wrote:> Hi. For any number of *known* arguments, we can do: > > one <- function(a) list(a = substitute(a)) > two <- function(a, b) list(a = substitute(a), b = substitute(b)) > > and so on. But how do I achieve the same when I have: > > dots <- function(...) list(???) > > I want to implement this such that I can do: > >> exprs <- dots(1+2) >> str(exprs) > List of 1 > $ : language 1 + 2 > > as well as: > >> exprs <- dots(1+2, "a", rnorm(3)) >> str(exprs) > List of 3 > $ : language 1 + 2 > $ : chr "a" > $ : language rnorm(3) > > Is this possible to achieve using plain R code?I think so. substitute(list(...)) gives you a single expression containing a call to list() with the unevaluated arguments; you can convert that to what you want using something like dots <- function (...) { exprs <- substitute(list(...)) as.list(exprs[-1]) } Duncan Murdoch
Jeroen Ooms
2018-Aug-12 20:46 UTC
[Rd] substitute() on arguments in ellipsis ("dot dot dot")?
On Sun, Aug 12, 2018 at 10:00 PM, Henrik Bengtsson <henrik.bengtsson at gmail.com> wrote:> Hi. For any number of *known* arguments, we can do: > > one <- function(a) list(a = substitute(a)) > two <- function(a, b) list(a = substitute(a), b = substitute(b)) > > and so on. But how do I achieve the same when I have: > > dots <- function(...) list(???) > > I want to implement this such that I can do: > >> exprs <- dots(1+2) >> str(exprs) > List of 1 > $ : language 1 + 2 > > as well as: > >> exprs <- dots(1+2, "a", rnorm(3)) >> str(exprs) > List of 3 > $ : language 1 + 2 > $ : chr "a" > $ : language rnorm(3) > > Is this possible to achieve using plain R code?You could use match.call, for example: dots <- function(...) match.call(expand.dots = FALSE)[['...']] Note that this returns a pairlist, so if you want an ordinary list you should wrap it in as.list()
Peter Meilstrup
2018-Aug-13 05:09 UTC
[Rd] substitute() on arguments in ellipsis ("dot dot dot")?
Interestingly, as.list(substitute(...())) also works. On Sun, Aug 12, 2018 at 1:16 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 12/08/2018 4:00 PM, Henrik Bengtsson wrote: >> >> Hi. For any number of *known* arguments, we can do: >> >> one <- function(a) list(a = substitute(a)) >> two <- function(a, b) list(a = substitute(a), b = substitute(b)) >> >> and so on. But how do I achieve the same when I have: >> >> dots <- function(...) list(???) >> >> I want to implement this such that I can do: >> >>> exprs <- dots(1+2) >>> str(exprs) >> >> List of 1 >> $ : language 1 + 2 >> >> as well as: >> >>> exprs <- dots(1+2, "a", rnorm(3)) >>> str(exprs) >> >> List of 3 >> $ : language 1 + 2 >> $ : chr "a" >> $ : language rnorm(3) >> >> Is this possible to achieve using plain R code? > > > I think so. substitute(list(...)) gives you a single expression containing > a call to list() with the unevaluated arguments; you can convert that to > what you want using something like > > dots <- function (...) { > exprs <- substitute(list(...)) > as.list(exprs[-1]) > } > > Duncan Murdoch > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel