What ways are there to edit a function in R?

Answer

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)}

All r Questions

Ask your interview questions on r

Write Your comment or Questions if you want the answers on r from r Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---