In R, how do you evaluate ... in the calling function?

If I want to know what is stored in the argument ...inside the R function, I can just convert it to a list, for example

foo <- function(...)
{
  dots <- list(...)
  print(dots)
}

foo(x = 1, 2, "three")
#$x
#[1] 1
#
#[[2]]
#[1] 2
#
#[[3]]
#[1] "three"

I can't figure out how to evaluate ...in the calling function. In the following example, I want the content to bazreturn an argument ...to bar.

bar <- function(...)
{
  baz()
}

baz <- function()
{ 
  # What should dots be assigned as?
  # I tried                                           
  # dots <- get("...", envir = parent.frame())
  # and variations of
  # dots <- eval(list(...), envir = parent.frame())
  print(dots)
}

bar(x = 1, 2, "three")

get("...", envir = parent.frame())returns <...>that looks promising, but I can't figure out how to extract anything useful from it.

eval(list(...), envir = parent.frame())throws an error, claiming that it is ...used incorrectly.

How to get ...out bar?

+3
source share
3 answers

. eval substitute. baz

baz <- function()
{ 
  dots <- eval(substitute(list(...), env = parent.frame()))
  print(dots)
}
+3

:

bar <- function(...)
{
  baz(...=...)
}

baz <- function(...)
{ 
  print(list(...))
}

bar(x = 1, 2, "three")

.

:

bar <- function(...)
{
  bar.x <- list(...)
  baz()
}

baz <- function()
{ 
  dots <- get("bar.x", envir = parent.frame())
  print(dots)
}

bar(x = 1, 2, "three")

, , :

bar <- function(...)
{
  ... <- list(...)
  baz()
}

baz <- function()
{ 
  dots <- get("...", envir = parent.frame())
  print(dots)
}

bar(x = 1, 2, "three")
+1

In a word: don't. Trying to redefine the rules of R-coverage, the likelihood that he will suffer from pain and pain is likely.

+1
source

Source: https://habr.com/ru/post/1758768/


All Articles