Returning to parent function

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

Returning to parent function

Saptarshi
Hello,

I would like a function X to return to the place that called the
function XParent that called this function X.

Y calls XParent
Y = function(){
  XParent()
  print("hello")
}

XParent calls X

XParent = function(){
   X()
   print("H")
}

X returns to the point just after the call to XParent. Hence
print("H") is not called, but instead "hello" is printed.

An example of what i'm going for is this

continueIfTrue <- function(filterExp, grpname, subname,n=1){
    y <- substitute(filterExp)
    res <- isn(eval(y, envir=parent.frame()),FALSE)
    ## if res is FALSE, I would like to return from telemStats
}

telemStats <- function(a,b){
    b <- c(10,12)
    continueIfTrue( {    length(b) >=10 }, "progStats","00")
    print("Since the above condition failed, I would not like this
message to be printed")
}

I looked into callCC and signals but dont think i understood correctly.
Any hints would be appreciated

Kind Regards
Saptarshi

______________________________________________
[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: Returning to parent function

David Winsemius

On Mar 16, 2015, at 3:08 PM, Saptarshi Guha wrote:

> Hello,
>
> I would like a function X to return to the place that called the
> function XParent that called this function X.
>
> Y calls XParent
> Y = function(){
>  XParent()
>  print("hello")
> }
>
> XParent calls X
>
> XParent = function(){
>   X()
>   print("H")
> }
>
> X returns to the point just after the call to XParent. Hence
> print("H") is not called, but instead "hello" is printed.

?sys.call # my second reading of your question makes me think this wasn't what was requested.

?return  # this would do what was asked for

> XParent = function(){
+   return(sys.call())
+   print("H")
+ }
> Y()
[1] "hello"

# Success
# now to show that a value could be returned if desired

> Y = function(){
+  print(XParent())
+  print("hello")
+ }
> XParent = function(){
+   return(sys.call())
+   print("H")
+ }
> Y()
XParent()
[1] "hello"


>
> X returns to the point just after the call to XParent. Hence
> print("H") is not called, but instead "hello" is printed.
>
> An example of what i'm going for is this
>
> continueIfTrue <- function(filterExp, grpname, subname,n=1){
>    y <- substitute(filterExp)
>    res <- isn(eval(y, envir=parent.frame()),FALSE)
>    ## if res is FALSE, I would like to return from telemStats
> }
>
> telemStats <- function(a,b){
>    b <- c(10,12)
>    continueIfTrue( {    length(b) >=10 }, "progStats","00")
>    print("Since the above condition failed, I would not like this
> message to be printed")
> }

I'm afraid there were too many undefined objects to make much sense of that example.

>
> I looked into callCC and signals but dont think i understood correctly.
> Any hints would be appreciated
>
> Kind Regards
> Saptarshi
>
--

David Winsemius
Alameda, CA, USA

______________________________________________
[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: Returning to parent function

Saptarshi
Example was complicated, but here is a simpler form

continueIfTrue <- function(mm=return()){
    eval(mm)
}
telemStats <- function(){
    y <- substitute(return())
    continueIfTrue(y)
    print("I would not like this message to be printed")
}
telemStats()


Ideally, calling telemStats() should return to the prompt and the
print in telemStats should not appear


On Mon, Mar 16, 2015 at 4:02 PM, David Winsemius <[hidden email]> wrote:

>
> On Mar 16, 2015, at 3:08 PM, Saptarshi Guha wrote:
>
>> Hello,
>>
>> I would like a function X to return to the place that called the
>> function XParent that called this function X.
>>
>> Y calls XParent
>> Y = function(){
>>  XParent()
>>  print("hello")
>> }
>>
>> XParent calls X
>>
>> XParent = function(){
>>   X()
>>   print("H")
>> }
>>
>> X returns to the point just after the call to XParent. Hence
>> print("H") is not called, but instead "hello" is printed.
>
> ?sys.call # my second reading of your question makes me think this wasn't what was requested.
>
> ?return  # this would do what was asked for
>
>> XParent = function(){
> +   return(sys.call())
> +   print("H")
> + }
>> Y()
> [1] "hello"
>
> # Success
> # now to show that a value could be returned if desired
>
>> Y = function(){
> +  print(XParent())
> +  print("hello")
> + }
>> XParent = function(){
> +   return(sys.call())
> +   print("H")
> + }
>> Y()
> XParent()
> [1] "hello"
>
>
>>
>> X returns to the point just after the call to XParent. Hence
>> print("H") is not called, but instead "hello" is printed.
>>
>> An example of what i'm going for is this
>>
>> continueIfTrue <- function(filterExp, grpname, subname,n=1){
>>    y <- substitute(filterExp)
>>    res <- isn(eval(y, envir=parent.frame()),FALSE)
>>    ## if res is FALSE, I would like to return from telemStats
>> }
>>
>> telemStats <- function(a,b){
>>    b <- c(10,12)
>>    continueIfTrue( {    length(b) >=10 }, "progStats","00")
>>    print("Since the above condition failed, I would not like this
>> message to be printed")
>> }
>
> I'm afraid there were too many undefined objects to make much sense of that example.
>
>>
>> I looked into callCC and signals but dont think i understood correctly.
>> Any hints would be appreciated
>>
>> Kind Regards
>> Saptarshi
>>
> --
>
> David Winsemius
> Alameda, CA, USA
>

______________________________________________
[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: Returning to parent function

Berend Hasselman
In reply to this post by Saptarshi

> On 16-03-2015, at 23:08, Saptarshi Guha <[hidden email]> wrote:
>
> Hello,
>
> I would like a function X to return to the place that called the
> function XParent that called this function X.
>
> Y calls XParent
> Y = function(){
>  XParent()
>  print("hello")
> }
>
> XParent calls X
>
> XParent = function(){
>   X()
>   print("H")
> }
>
> X returns to the point just after the call to XParent. Hence
> print("H") is not called, but instead "hello" is printed.
>


I do not understand what you are saying or implying here.
Take this R code:

####<code>
Y <- function(){
 XParent()
 print("hello")
}

XParent <- function(){
  X()
  print("H")
}

X <- function() { print("In function X") }

Y()
####</code>

and this is the output:

[1] "In function X"
[1] "H"
[1] "hello"


Implying that everything you seem to want printed is acually printed.

Berend

______________________________________________
[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: Returning to parent function

David Winsemius
In reply to this post by Saptarshi

On Mar 16, 2015, at 5:05 PM, Saptarshi Guha wrote:

> Example was complicated, but here is a simpler form
>
> continueIfTrue <- function(mm=return()){
>    eval(mm)
> }

What are you trying to accomplish by passing `return()` to a formal parameter?


> telemStats <- function(){
>    y <- substitute(return())

That last bit of code doesn't make a lot of sense either. The `substitute` function is specifically designed to NOT evaluate the first argument but rather to return an unevaluated call. If you wanted to actually "return" from that function you would assuredly not use `return` within the substitute argument.

You need to explain what you want to accomplish  rather than posting failed code.

--
David.


>    continueIfTrue(y)
>    print("I would not like this message to be printed")
> }
> telemStats()
>
>
> Ideally, calling telemStats() should return to the prompt and the
> print in telemStats should not appear
>
>
> On Mon, Mar 16, 2015 at 4:02 PM, David Winsemius <[hidden email]> wrote:
>>
>> On Mar 16, 2015, at 3:08 PM, Saptarshi Guha wrote:
>>
>>> Hello,
>>>
>>> I would like a function X to return to the place that called the
>>> function XParent that called this function X.
>>>
>>> Y calls XParent
>>> Y = function(){
>>> XParent()
>>> print("hello")
>>> }
>>>
>>> XParent calls X
>>>
>>> XParent = function(){
>>>  X()
>>>  print("H")
>>> }
>>>
>>> X returns to the point just after the call to XParent. Hence
>>> print("H") is not called, but instead "hello" is printed.
>>
>> ?sys.call # my second reading of your question makes me think this wasn't what was requested.
>>
>> ?return  # this would do what was asked for
>>
>>> XParent = function(){
>> +   return(sys.call())
>> +   print("H")
>> + }
>>> Y()
>> [1] "hello"
>>
>> # Success
>> # now to show that a value could be returned if desired
>>
>>> Y = function(){
>> +  print(XParent())
>> +  print("hello")
>> + }
>>> XParent = function(){
>> +   return(sys.call())
>> +   print("H")
>> + }
>>> Y()
>> XParent()
>> [1] "hello"
>>
>>
>>>
>>> X returns to the point just after the call to XParent. Hence
>>> print("H") is not called, but instead "hello" is printed.
>>>
>>> An example of what i'm going for is this
>>>
>>> continueIfTrue <- function(filterExp, grpname, subname,n=1){
>>>   y <- substitute(filterExp)
>>>   res <- isn(eval(y, envir=parent.frame()),FALSE)
>>>   ## if res is FALSE, I would like to return from telemStats
>>> }
>>>
>>> telemStats <- function(a,b){
>>>   b <- c(10,12)
>>>   continueIfTrue( {    length(b) >=10 }, "progStats","00")
>>>   print("Since the above condition failed, I would not like this
>>> message to be printed")
>>> }
>>
>> I'm afraid there were too many undefined objects to make much sense of that example.
>>
>>>
>>> I looked into callCC and signals but dont think i understood correctly.
>>> Any hints would be appreciated
>>>
>>> Kind Regards
>>> Saptarshi
>>>
>> --
>>
>> David Winsemius
>> Alameda, CA, USA
>>

David Winsemius
Alameda, CA, USA

______________________________________________
[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: Returning to parent function

Spencer Graves-3


On 3/17/2015 10:01 AM, David Winsemius wrote:
> On Mar 16, 2015, at 5:05 PM, Saptarshi Guha wrote:
>
>> Example was complicated, but here is a simpler form
>>
>> continueIfTrue <- function(mm=return()){
>>     eval(mm)
>> }
> What are you trying to accomplish by passing `return()` to a formal parameter?


       Might returning a logical serve your purpose?  Then you could say
"if(!continueIfTrue(...))return(...)".  Will this do what you want?


       Spencer
>
>> telemStats <- function(){
>>     y <- substitute(return())
> That last bit of code doesn't make a lot of sense either. The `substitute` function is specifically designed to NOT evaluate the first argument but rather to return an unevaluated call. If you wanted to actually "return" from that function you would assuredly not use `return` within the substitute argument.
>
> You need to explain what you want to accomplish  rather than posting failed code.
>

______________________________________________
[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: Returning to parent function

Bert Gunter
With all due respect:

I don't think much respect is due. I doubt that the OP has made much of an
effort to learn R and is merely trying to slap on some prior CS class
concepts onto R. Ergo the confusion and nonsensical post.


http://www.jabberwocky.com/carroll/jabber/jabberwocky.html

seems relevant here.

Cheers,

Bert


On Tuesday, March 17, 2015, Spencer Graves <[hidden email]>
wrote:

>
>
> On 3/17/2015 10:01 AM, David Winsemius wrote:
>
>> On Mar 16, 2015, at 5:05 PM, Saptarshi Guha wrote:
>>
>>  Example was complicated, but here is a simpler form
>>>
>>> continueIfTrue <- function(mm=return()){
>>>     eval(mm)
>>> }
>>>
>> What are you trying to accomplish by passing `return()` to a formal
>> parameter?
>>
>
>
>       Might returning a logical serve your purpose?  Then you could say
> "if(!continueIfTrue(...))return(...)".  Will this do what you want?
>
>
>       Spencer
>
>>
>>  telemStats <- function(){
>>>     y <- substitute(return())
>>>
>> That last bit of code doesn't make a lot of sense either. The
>> `substitute` function is specifically designed to NOT evaluate the first
>> argument but rather to return an unevaluated call. If you wanted to
>> actually "return" from that function you would assuredly not use `return`
>> within the substitute argument.
>>
>> You need to explain what you want to accomplish  rather than posting
>> failed code.
>>
>>
> ______________________________________________
> [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.
>


--

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge is
certainly not wisdom."
Clifford Stoll

        [[alternative HTML version deleted]]

______________________________________________
[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: Returning to parent function

Jeff Newmiller
In reply to this post by Saptarshi
Perhaps read ?try
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On March 16, 2015 3:08:35 PM PDT, Saptarshi Guha <[hidden email]> wrote:

>Hello,
>
>I would like a function X to return to the place that called the
>function XParent that called this function X.
>
>Y calls XParent
>Y = function(){
>  XParent()
>  print("hello")
>}
>
>XParent calls X
>
>XParent = function(){
>   X()
>   print("H")
>}
>
>X returns to the point just after the call to XParent. Hence
>print("H") is not called, but instead "hello" is printed.
>
>An example of what i'm going for is this
>
>continueIfTrue <- function(filterExp, grpname, subname,n=1){
>    y <- substitute(filterExp)
>    res <- isn(eval(y, envir=parent.frame()),FALSE)
>    ## if res is FALSE, I would like to return from telemStats
>}
>
>telemStats <- function(a,b){
>    b <- c(10,12)
>    continueIfTrue( {    length(b) >=10 }, "progStats","00")
>    print("Since the above condition failed, I would not like this
>message to be printed")
>}
>
>I looked into callCC and signals but dont think i understood correctly.
>Any hints would be appreciated
>
>Kind Regards
>Saptarshi
>
>______________________________________________
>[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.

______________________________________________
[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: Returning to parent function

Rolf Turner
In reply to this post by Bert Gunter

On 18/03/15 13:22, Bert Gunter wrote:

> With all due respect:
>
> I don't think much respect is due.

<SNIP>


Fortune nomination!

cheers,

Rolf

--
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619

______________________________________________
[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: Returning to parent function

Martin Morgan-2
In reply to this post by Saptarshi
On 03/16/2015 05:05 PM, Saptarshi Guha wrote:

> Example was complicated, but here is a simpler form
>
> continueIfTrue <- function(mm=return()){
>      eval(mm)
> }
> telemStats <- function(){
>      y <- substitute(return())
>      continueIfTrue(y)
>      print("I would not like this message to be printed")
> }
> telemStats()
>
>
> Ideally, calling telemStats() should return to the prompt and the
> print in telemStats should not appear

here's one way to implement your original example -- signal and handle, via
tryCatch(), a custom condition created (modelled after simpleCondition()) as an
S3 class with linear inheritance.

X <- function() {
     print("I'm saying...")
     signalCondition(structure(list(), class=c("my", "condition")))
     print("X")
}

Y <- function(){
     tryCatch(XParent(), my=function(...) NULL)
     print("hello")
}

XParent <- function(){
     X()
     print("H")
}

leading to

   > Y()
   [1] "I'm saying..."
   [1] "hello"

callCC() is tricky for me to grasp, but I'll write Y to accept an argument X,
which will be a function. It'll call XParent with that function, and XParent
will use the function.

Y <- function(X){
     XParent(X)
     print("hello")
}

XParent <- function(X){
     X("fun")
     print("H")
}

then we've got

   > Y(X)
   Error in XParent(X) (from tmp.R!4361C1Y#2) : object 'X' not found
   > Y(function(x) print("X"))
   [1] "X"
   [1] "H"
   [1] "hello"

but more interestingly the long jump to the top (where callCC was invoked)

   > callCC(function(X) { Y(X) })
   [1] "fun"

or in a function

   y <- function() {
       value <- callCC(function(X) {
           Y(X)
       })
       print(value)
       print("done")
   }

Hope that helps and is not too misleading. Excellent question.

Martin

>
>
> On Mon, Mar 16, 2015 at 4:02 PM, David Winsemius <[hidden email]> wrote:
>>
>> On Mar 16, 2015, at 3:08 PM, Saptarshi Guha wrote:
>>
>>> Hello,
>>>
>>> I would like a function X to return to the place that called the
>>> function XParent that called this function X.
>>>
>>> Y calls XParent
>>> Y = function(){
>>>   XParent()
>>>   print("hello")
>>> }
>>>
>>> XParent calls X
>>>
>>> XParent = function(){
>>>    X()
>>>    print("H")
>>> }
>>>
>>> X returns to the point just after the call to XParent. Hence
>>> print("H") is not called, but instead "hello" is printed.
>>
>> ?sys.call # my second reading of your question makes me think this wasn't what was requested.
>>
>> ?return  # this would do what was asked for
>>
>>> XParent = function(){
>> +   return(sys.call())
>> +   print("H")
>> + }
>>> Y()
>> [1] "hello"
>>
>> # Success
>> # now to show that a value could be returned if desired
>>
>>> Y = function(){
>> +  print(XParent())
>> +  print("hello")
>> + }
>>> XParent = function(){
>> +   return(sys.call())
>> +   print("H")
>> + }
>>> Y()
>> XParent()
>> [1] "hello"
>>
>>
>>>
>>> X returns to the point just after the call to XParent. Hence
>>> print("H") is not called, but instead "hello" is printed.
>>>
>>> An example of what i'm going for is this
>>>
>>> continueIfTrue <- function(filterExp, grpname, subname,n=1){
>>>     y <- substitute(filterExp)
>>>     res <- isn(eval(y, envir=parent.frame()),FALSE)
>>>     ## if res is FALSE, I would like to return from telemStats
>>> }
>>>
>>> telemStats <- function(a,b){
>>>     b <- c(10,12)
>>>     continueIfTrue( {    length(b) >=10 }, "progStats","00")
>>>     print("Since the above condition failed, I would not like this
>>> message to be printed")
>>> }
>>
>> I'm afraid there were too many undefined objects to make much sense of that example.
>>
>>>
>>> I looked into callCC and signals but dont think i understood correctly.
>>> Any hints would be appreciated
>>>
>>> Kind Regards
>>> Saptarshi
>>>
>> --
>>
>> David Winsemius
>> Alameda, CA, USA
>>
>
> ______________________________________________
> [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.
>


--
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793

______________________________________________
[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.