Jeremie Juste
2018-Sep-14 08:33 UTC
[R] modify the supposed return value of a function during evaluation?
Hello, I'm wondering it is possible and if yes would it be desirable to modify the return value of functions during evaluation. I supposed this would be very useful during debugging myfun <- function(x) {res <- x+3 ; browser() ; res} let say I run the following function myfun(3) and drop in the browser. I then happened to notice that I should multiply <x> by 3 instead of adding. I was thinking to do return(x*3) but the return value is still 6.> myfun(3)Called from: myfun(3) Browse[1]> debug at #1: res Browse[2]> return(3*3) [1] 6>This feature could be valuable if the following setting function(){ tmp_res <- time_consuming_function_with_browser(param) some_other_function(tmp_res) } Best regards, Jeremie
Rolf Turner
2018-Sep-14 08:56 UTC
[R] [FORGED] modify the supposed return value of a function during evaluation?
On 09/14/2018 08:33 PM, Jeremie Juste wrote:> > Hello, > > I'm wondering it is possible and if yes would it be desirable to modify > the return value of functions during evaluation. > > I supposed this would be very useful during debugging > > myfun <- function(x) {res <- x+3 ; browser() ; res} > > let say I run the following function myfun(3) and drop in the browser. I then happened to notice that I should > multiply <x> by 3 instead of adding. I was thinking to do return(x*3) > but the return value is still 6. > >> myfun(3) > Called from: myfun(3) > Browse[1]> > debug at #1: res > Browse[2]> return(3*3) > [1] 6Don't use return(). Change the value of "res" and then "continue". Browse[1]> res <- x*3 Browse[1]> c You will get the value 9 returned by "myfun(3)". (God how I hate this stupid egocentric sounding "mythis" and "mythat" syntax that Micro$oft has inflicted upon the world. But never mind.) I am however very sure that your proposed approach is Not A Good Idea. If you want an interactive structure for your function, make it interactive. In a well thought out and well organised manner. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276