[R] How to modify the body of a function?

William Dunlap wdunlap at tibco.com
Sun Jul 27 19:34:14 CEST 2014


This is a real hack, but you can redefine return in your function:
> f <- function() {
+     return("early return")
+     "last value in function"
+ }
> f()
[1] "early return"
> f <- function() {
+     return <- function(x)x
+     return("early return")
+     "last value in function"
+ }
> f()
[1] "last value in function"

IMO, well written functions do not have return statements in them.  They
are the equivalent of goto statements.
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sun, Jul 27, 2014 at 6:41 AM, super <desolator88 at 163.com> wrote:
> Suppose that I had a function as below:
> f<-function() {
> return(1)
> }
> i want to change the body of f  to the form like this:
> f<-function(){
> 1
> function() {}
> }
> How can i do the task using body(f) or something else solutions?
>         [[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.



More information about the R-help mailing list