1

NB: This question is not a duplicate of How to use R's ellipsis feature when writing your own function?. That question asks how to use ellipses, and in particular, "how can [one] convert the ellipsis from the function's signature into, for example, a list?" Below I don't ask anything of the sort. I am asking why my use of ellipses fails to produce the expected result.


As an example, the function my.ls below is meant to be a wrapper around base::ls that makes all.names = TRUE the default:

my.ls <- function (...) base::ls(..., all.names = TRUE)

I had expected that, with this definition, my.ls() would produce the same value as base::ls(all.names = TRUE) does. To my surprise, the values from these two expressions are not even close.

For example:

% /usr/bin/R --quiet --vanilla
> x <- 3; .y <- 1; z <- 4
> base::ls(all.names = TRUE)
[1] "x"  ".y" "z" 
> my.ls <- function (...) base::ls(..., all.names = TRUE)
> my.ls()
[1] "..."

What am I doing wrong?

Community
  • 1
  • 1
kjo
  • 33,683
  • 52
  • 148
  • 265

2 Answers2

1

Is it because the default argument for envir in base::ls is as.environement(pos)?

Adding envir = gloal.env() seems to work for me, assuming you are working from the global environment.

Frost_Maggot
  • 309
  • 2
  • 12
1

OK, I figured out the problem. The following implementation of my.ls is closer to what I'm after:

my.ls <- function (name, all.names = TRUE, ...) {
    if (missing(name))
        return(my.ls(parent.frame(1), all.names = all.names, ...))

    base::ls(name, all.names = all.names, ...)
}
kjo
  • 33,683
  • 52
  • 148
  • 265