Use the substitute function.
For example:
myfun <- function(x,y) { result <- list(x+y,x*y) return(result) }
Using body , consider myfun as a list to choose what you would like to change in the function:
> body(myfun)[[2]][[3]][[2]] x + y
When changing this parameter, you must use the substitute function to replace part of the function with a call or name object, if necessary. Replacing character strings does not work, because functions are not stored or used as character strings.
body(myfun)[[2]][[3]][[2]] <- substitute(2*x)
Now the selected fragment of the function has been replaced:
> myfun function (x, y) { result <- list(2 * x, x * y) return(result) }
Will beason
source share