rfunctionargumentssubstitution

How do you specify a variable name for a function call using a character variable in R?


I am working in R trying to create a wrapper function that calls a specified internal function (which is some existing function) and does some things with it. My wrapper function will include an argument func for the internal function I want to call and this function will always have a particular standard structure with a particular argument of interest, but the argument might be called by a different name in different cases, so I also want an argument in my wrapper function arg.name that specifies the name of one of the arguments in the internal function. I also want to be able to specify additional arguments with the ellipses ... to allow other general inputs to the internal function. Here is an outline of what I want to do (but this code doesn't work):

#Example of the internal function
#This would be an existing function - not one that I program myself
#The variable n might be named something different in some cases
EXISTING.FUNCTION <- function(a, n) { a + n^2 }

#Example of the wrapper function
#It includes arg.name to specify the name of the variable
MY.WRAPPER.FUNCTION <- function(value, func, arg.name, ...) {
  
  #Evaluate the internal function using inputs wrapper inputs
  OUTPUT <- func(arg.name = value, ...)
  
  #Return the output
  OUTPUT }

I want to be able to call the wrapper function like this:

MY.WRAPPER.FUNCTION(value = 4, func = EXISTING.FUNCTION, arg.name = 'n', a = 3)

#What I want to get
[1] 19

#What I actually get
Error in func(arg.name = value, ...) : unused argument (arg.name = value)

I am not sure how to get R to recognise the character string arg.name as the name of the variable in the internal use of the specified internal function within the wrapper function. I think this uses one or more of the evaluation functions (e.g., substitute, quote, as.name, etc.) but I have not yet had any success in modifying the above code to have arg.name recognised as identifying the argument name in the internal function.


Additional information: The above is written to strip the problem back to essentials, but if it matters, the existing functions I want to work with are quantile functions for probability distributions. Usually these will have a probability argument called p but I want to allow for the possibility that someone programs a quantile function with a different name for the probability argument. I'm not sure if this context matters for the solution, but that is the motivation for the problem.


Solution

  • Your question contains a lot of design questions.

    I am not too sure what you want. Quick-fix (dirty)

    .my_function = \(a, n) a + n^2L # . to indicate internal use
    
    wrapper = \(value, func, arg.name, ...) {
      args = list(...)
      args[[arg.name]] = value
      do.call(func, args)
    }
    
    > wrapper(value=4, func=.my_function, arg.name='n', a=3)
    [1] 19
    

    I do not see much sense behind this.