Tal Galili
2010-Mar-16 21:31 UTC
[R] Is there a way to edit a specific line in a function (e.g: doing function->text->edit->function) ?
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@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]]
Henrique Dallazuanna
2010-Mar-16 21:34 UTC
[R] Is there a way to edit a specific line in a function (e.g: doing function->text->edit->function) ?
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
Duncan Murdoch
2010-Mar-16 21:47 UTC
[R] Is there a way to edit a specific line in a function (e.g: doing function->text->edit->function) ?
On 16/03/2010 5:31 PM, Tal Galili 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.That's the best way.> > Is there another way?You can use findLineNum to find where that line of source appears in your workspace. For example, if I source your input from the above, the line that interests you is line 9 of my foo.R input file. Then > source("c:/temp/foo.R") > findLineNum("foo.R#9") c:\temp\foo.R#9: foo step 3 in <environment: R_GlobalEnv> > body(foo)[[3]] line2 <- 0 Now I can replace step 3 using body(foo)[[3]] <- quote(line2 <- 2) If the line you want is nested deep within a block structure, you might need to use a vector index to get to it, e.g. after wrapping that line in an if() { }, I see > findLineNum("foo.R#10") c:\temp\foo.R#10: foo step 3,3,2 in <environment: R_GlobalEnv> > body(foo)[[c(3,3,2)]] <- quote(line2 <- 2) > foo function (x) { line1 <- x if (x > 0) { line2 <- 2 } line3 <- line1 + line2 return(line3) } The findLineNum() function depends on having source references in the function, so it won't work on functions in packages unless you build them with the option to keep package source refs. (And then you need special contortions to tell it to look in the namespace for the function, and to edit a function in the namespace. Better not to do that.) Duncan Murdoch> > *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.