[R] [FORGED] modify the supposed return value of a function during evaluation?

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Fri Sep 14 10:56:40 CEST 2018

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] 6

Don'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.


Rolf Turner

Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

More information about the R-help mailing list