modify the supposed return value of a function during evaluation?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

modify the supposed return value of a function during evaluation?

Jeremie Juste

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

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

Rolf Turner
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.

cheers,

Rolf Turner

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.