[R] Is there a way to edit a specific line in a function (e.g: doing function->text->edit->function) ?

Henrique Dallazuanna wwwhsd at gmail.com
Tue Mar 16 22:34:53 CET 2010


Try this:

body(foo)[[grep('^line2', body(foo))]][[3]] <- 2

On Tue, Mar 16, 2010 at 6:31 PM, Tal Galili <tal.galili at gmail.com> wrote:
> Hello,
>
> 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?
>
> The two ways I know of are either to use
>
> fix(foo)
>
>  And change the function.
>
> Or to just write the function again.
>
> Is there another way?
>
> *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.
>
> The reason I am asking is that I just published a post online where I used a
> function to which I did a minor tweak (so to improve it's output for my
> particular case).
> This tweaking was just adding one line of code, to a function who's length
> is 187 lines of code. So instead of repasting all the function on my blog, I
> decided to just explain how to edit it.  But I would rather have a simple
> code that edited the function for the reader.
>
>
> Thanks,
> Tal
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili at gmail.com |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
> ----------------------------------------------------------------------------------------------
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list