R: Change value of an argument in ellipsis and pass ellipsis to the other function without using list() and eval()
Asked Answered
P

1

12

I am looking for a universal way to change a value of an argument inside ellipsis and pass it to the other function. I know an ugly solution for that, which looks like this:

test <- function(...) {
  a <- list(...)
  a[['y']] <- 2
  return(eval(parse(text=paste0('identical(',paste(unlist(a),collapse=','),')'))))
}

test(x=1,y=1)

Ideally I would like to avoid converting ... to a list and then using eval(). Is it possible to somehow refer to an argument inside ... by name and change it's value?

Plainspoken answered 1/5, 2013 at 16:2 Comment(0)
R
15

You do have to unpack ... to manipulate its contents. The ugly bit here, really, is your last line, which can be simplified to do.call(identical, a):

test <- function(...) {
  a <- list(...)
  a[['y']] <- 2
  do.call(identical, a)
}

test(x=1,y=1)
# [1] FALSE
Roper answered 1/5, 2013 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.