Friday 15 June 2012

getting the arguments of a parent function in R, with names -


I am trying to write a function that captures the function's argument which is called by it For,

  get_args & lt; - Function () as.list (sys.pall ()) [- 1] Caller & lt; - Function (x, y, z) {Get_args ()} Collar (1,2,3) [[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 < / Code>  

sys.call () Unfortunately, the matching parameter name does not match the argument values, and I would like to type a version of get_args which

  caller2 & lt ; - Returns the output similar to the function (X, Y, Z) {as.list (match.call ()) [- 1]} Caller 2 (1,2,3) $ x [1] 1 $ y [1] 2 $ z [1] 3   

"match_args ()" to move with "match.call ()" is not the direct solution which I see, because in reality get_args its The original function will do some other things before returning the arguments.

I have tried using match.call () with sys.parent () in many ways, but I can not find the function to return the caller's argument; It just gives the argument of get_args ().

Is there any way to create get_args () return output similar to caller2 for the above test case? I know that it is possible to name the arguments manually using the formals (), but is it guaranteed to be consolidated?

If any clarification is necessary, please comment below. Thank you.

1:

The purpose of get_args () was to call the user-friendly method of obtaining those arguments that was a function. Typing as.list (match.call ()) [- 1] gets obsolete, but because match.call grabs the closest function call, it currently receives arguments for get_args ().

get_args () will also get the default argument from the original function, but it is easy to implement.

Solution:

Thanks Hong Oh, the key to using Macchalk, providing both the call and the definition of that function Is doing what you want to know about it.

  get_args & lt; - Function () is a slightly modified, anonymously similar version for {as.list (match.call (def = sys.function (-1)). Call = sys.call (-1))) [- 1]}   

This version enhances the function above the phone stack, grabs its definition and call, and matches the parameters for its arguments.

  get_args & lt; - function () {cl & lt; - sys.call (- 1) F & LT; - (As Checher (CL [[1]], Mode = "Function", sys.frame (-2)) CL & lt; - match.call (Definition = F, call = CL) As.list (cl) [- 1]}   

Here the key is defined by the definition logic Match.call to be done Calling function of get_arg should be (hopefully!) Work for normal case, where get_args is anywhere Can be called.

No comments:

Post a Comment