1

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

  • given x is a list, a data frame, or an integer you can use function `with()` to call the varibales directly. – Patrik_P Sep 05 '17 at 13:37

1 Answers1

1

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
James
  • 65,548
  • 14
  • 155
  • 193