user1609452
user1609452

Reputation: 4444

assign attributes to a function on the fly

I am trying to use function A within function B. I want to fix the attributes of function A dependent on the input from function B. As a simple example:

somfun<-function(x,atra){

   functionA(x,atra$subset)

}

Sorry if it is vague. But I need atra to operate as the arguments like paste(x,sep='sss') etc. but with an arbitrary number of arguments. `atra would be a named vector for example or whatever was appropriate.

example

atra<-list(a=1:2,b=3:4,c=5:6,1:2,sep='')

x<-'data'
somfun<-function(x,atra){

   c(atra[[1]],atra[[2]],atra[[3]],paste(x,atra[[5]]))

}

but i want all the names to preserve and paste to realise that atra[[5]] is saying sep=''

Upvotes: 1

Views: 541

Answers (1)

David Robinson
David Robinson

Reputation: 78620

You want the do.call function, which can call a function using a list (including named arguments). For example:

do.call(paste, list("hello", "world", sep="/"))
# [1] "hello/world"

Upvotes: 2

Related Questions