Home > database >  R - Update elements in ... (dot-dot-dot) and return
R - Update elements in ... (dot-dot-dot) and return

Time:02-04

I want to change some elements of ... argument in the child function and return it back to the parent function. So the ... is updated within the parent function. I give my idea and a simple example below (not working yet):

parent <- function(x, ...){
  ... <- child(x, ...) # expect to return and update ...
  child_2(x, ...)
}
child <- function(x, ...){
  args <- list(...)
  args$y = 10 # change value
  return(args)
}
child_2 <- function(x, ...){
  args <- list(...)
  print(args$y = 10) # expect y = 10
}
parent(x=1,y=2)

How can I realize my idea and make it workable?

CodePudding user response:

Use do.call:

parent <- function(x, ...){
  args <- child(x, ...) # expect to return and update ...
  do.call("child_2", c(x = x, args))
}

child <- function(x, ...){
  args <- list(...)
  args$y = 10 # change value
  return(args)
}

child_2 <- function(x, ...){
  args <- list(...)
  print(args$y) # expect y = 10
}

parent(x=1,y=2)
## [1] 10

Old

The question was changed. This was the answer to the original question.

Change parent as shown. child is unchanged.

parent <- function(x, ...) {
  args <- child(x, ...)
  args$y
}
parent(x=1,y=2)
## [1] 10
  • Related