Q:

R - modify an unknown function

I have a function f of some vector x
The function in R is written as :
f <- function(x){#insert some function of x here}
I would like to return (-f), which denotes the negative of the function. In the case the function itself is known beforehand, this is a simple exercise.
However, in this case, I don't know what this function is  
Could someone please help me with the R code to carry this out? (The output needs to be a function in the vector x.)   
An example would be - f(x) = x + 1, then -f(x) = -x - 1  
Thank you!

A:

The following function getNegFn() takes a function fn and returns a function which returns the negative return value of fn:  
getNegFn <- function(fn){
    fnOut <- function(){
        - do.call(what=fn, args=as.list(match.call())[-1])
    }
    formals(fnOut) <- formals(fn)
    fnOut
}

An example:
fn <- function(x) x + 1
nFn <- getNegFn(fn=fn)

fn(1)
[1] 2
nFn(1)
[1] -2

Also works if the input function has ... arguments:
fn2 <- function(x, ...) x + sum(unlist(list(...)))
nFn2 <- getNegFn(fn=fn2)

fn2(x=1, y=2)
[1] 3
nFn2(x=1, y=2)
[1] -3

