I am trying to build a function that creates and outputs a new function (which I will call the output-function for convenience) from its inputs. I am able to generate the lines of code I want for the output-function as character strings and I have put this into a character vector that goes through the commands I want in the body of the function. However, I'm having trouble turning this into a function to give as the output.
I have observed that I can extract the body of a function using the body command and I can turn this into a character vector. For example, here is what you get when you apply this to the matrix function:
body(matrix)
{
if (is.object(data) || !is.atomic(data))
data <- as.vector(data)
.Internal(matrix(data, nrow, ncol, byrow, dimnames, missing(nrow),
missing(ncol)))
}
COMMANDS <- as.character(body(matrix))
COMMANDS
[1] "{"
[2] "if (is.object(data) || !is.atomic(data)) data <- as.vector(data)"
[3] ".Internal(matrix(data, nrow, ncol, byrow, dimnames, missing(nrow), missing(ncol)))"
However, I don't know how to reverse this process --- i.e., if I have constructed a character vector containing my commands, how do I create a function with this set of commands as the body?