[R] Returning to parent function

David Winsemius dwinsemius at comcast.net
Tue Mar 17 18:01:32 CET 2015


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 <dwinsemius at comcast.net> 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



More information about the R-help mailing list