Function that storages variables from a list automatically, in another function

34 Views Asked by At

For example I'd like to storage my variables only in f() function with the function setObj(argument), whose argument is a list with a,b,c,d fields, for example.

   f<-function(x){
  setObj(argument=x)
  (a+b+c+d)/4
}

So I can call my variables with their names, without subsetting from the list.

Thank you

1

There are 1 best solutions below

2
On

I believe you are looking for with:

f<-function(x){
  with(x,
   (a+b+c+d)/4
  )
}


f(list(a=1,b=2,c=3,d=4))
[1] 2.5