Simon
Simon

Reputation: 661

pass arguments to do.call from parent environment

Im trying pass arguments to do.call from its parent environment, but the variables are in the local environment. It would be really convenient to be able to do this, are there any workarounds that allows me to do the data manipulation and do.call in the same function?

data(cars)
function1 <- function(data, function2, arguments){
        manipulated.data <- data[1:(dim(data)[1]/2),]  # Arbitrary function
        results <- do.call(function2, arguments)
}

function1(data = cars, function2 = "mean", arguments = list(arg1 = manipulated.data$speed)

Upvotes: 0

Views: 64

Answers (1)

Vincent Guyader
Vincent Guyader

Reputation: 3199

does it help ?

data(cars)
function1 <- function(data, function2, arguments){
  manipulated.data <- data[1:(dim(data)[1]/2),]  # Arbitrary function
  do.call(function2, eval(parse(text=arguments)))
}

function1(data = cars, function2 = "mean", arguments = 'list(x = (manipulated.data$speed))')

I think that a nicer solution should use the lazyeval package ...

Upvotes: 1

Related Questions