58

Let's say we have the following function:

foo <- function(x)
{
    line1 <- x
    line2 <- 0
    line3 <- line1 + line2
    return(line3)
}

And that we want to change the second line to be:

    line2 <- 2

How would you do that?

One way is to use

fix(foo)

And change the function.

Another way is to just write the function again.

Is there another way? (Remember, the task was to change just the second line)

What I would like is for some way to represent the function as a vector of strings (well, characters), then change one of it's values, and then turn it into a function again.

Tal Galili
  • 24,605
  • 44
  • 129
  • 187
  • 1
    And it's not something you can do by passing a parameter to a function?.. Note that you can also pass functions as parameters. – Leo Alekseyev Mar 16 '10 at 21:06
  • 2
    Hi Leo - the question is for when I want to change a function someone else did, but inside the code to not have to copy paste the entire function. – Tal Galili Mar 16 '10 at 21:22
  • if you need to edit an "internal" function, I found the following advice and snippets very useful: [nabble: how-to-override-replace-a-function-in-a-package-namespace](http://r.789695.n4.nabble.com/how-to-override-replace-a-function-in-a-package-namespace-td866337.html) – stats-hb Jan 24 '18 at 10:05
  • Nabble.com no longer exists. – IRTFM May 23 '23 at 08:11

5 Answers5

46

There is a body<- function that lets you assign new content of the function.

body(foo)[[3]] <- substitute(line2 <- 2)
foo
#-----------    
function (x) 
{
    line1 <- x
    line2 <- 2
    line3 <- line1 + line2
    return(line3)
}

(The "{" is body(foo)[[1]] and each line is a successive element of the list. Therefore the second line is the third element in the expression list. The inserted elements need to be unevaluated expressions rather than text.)

There is also a corresponding formals<- function that lets one perform similar surgery on the argument pairlist.

Note: fixInNamespace is probably a better choice than fix if the function will be calling accessory functional resources in a loaded package. When used from the console, both fix will assign results to the .GlobalEnv.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 12
    Recently I was forced to do similar thing and one thing can be improved: you can go deeper into the list, so for question case `body(foo)[[3]][[3]] <- 2` works and `substitute` is needless. – Marek Dec 05 '10 at 10:38
  • Thanks. I thought the substitute was needed to keep the mode of the expression as a language item. – IRTFM Dec 05 '10 at 14:06
  • 4
    If you replace whole line then yes, is needed. Generally: classes should match: my way work cause I replace number by number. If I want to replace, say, `line2` to `myVariable` then `as.symbol` conversion is needed. – Marek Dec 05 '10 at 16:11
  • 1
    I remembered this approach yesterday when addressing a question about how to build a moment-generating function. It only required a symbolic "append" of `*exp(x*t)` using substitute, where the argument was delivered as a `quote()`-ed call object. – IRTFM Mar 03 '15 at 19:08
29

Or take a look at the debugging function trace(). It is probably not exactly what you are looking for but it lets you play around with the changes and it has the nice feature that you can always go back to your original function with untrace(). trace() is part of the base package and comes with a nice and thorough help page.

Start by calling as.list (body(foo)) to see all the lines of your code.

as.list(body(foo))
[[1]]
`{`

[[2]]
line1 <- x

[[3]]
line2 <- 0

[[4]]
line3 <- line1 + line2

[[5]]
return(line3)

Then you simply define what to add to your function and where to place it by defining the arguments in trace().

trace (foo, quote(line2 <- 2), at=4)
foo (2)
[1] 4

I said in the beginning that trace() might not be exactly what you are looking for since you didn't really change your third line of code and instead simply reassigned the value to the object line2 in the following, inserted line of code. It gets clearer if you print out the code of your now traced function

body (foo)
{
    line1 <- x
    line2 <- 0
    {
        .doTrace(line2 <- 2, "step 4")
        line3 <- line1 + line2
    }
    return(line3)
}
mropa
  • 11,562
  • 10
  • 33
  • 29
24

fix is the best way that I know of doing this, although you can also use edit and re-assign it:

foo <- edit(foo)

This is what fix does internally. You might want to do this if you wanted to re-assign your changes to a different name.

Shane
  • 98,550
  • 35
  • 224
  • 217
17

fixInNamespace is like fix, for functions in a package (including those that haven't been exported).

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • I think this might be the best answer. When I am trying to hack functions from packages, I often need to follow the creation of a newly hacked function with `environment(new) <- environment(old)`. This prevent failure to find accessory function that aren't on the search tree accessible from the console-associated environment. – IRTFM Feb 07 '22 at 18:17
9

You can use the 'body' function. This function will return the body of function:

fnx = function(a, b) { return(a^2 + 7*a + 9)}
body(fnx)
# returns the body of the function

So a good way to 'edit' a function is to use 'body' on the left-hand side of an assignment statement:

body(fnx) = expression({a^2 + 11*a + 4})
doug
  • 69,080
  • 24
  • 165
  • 199