Putting an object in to a function that calls the current function

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

Putting an object in to a function that calls the current function

Ales Ziberna
Hello!

I would like to put an object in to a function that calls the current
function.

I thought the answer will be clear to me after reading the help files:
?assign
?sys.parent

However it is not.
Here is an example I thought should work, however it dose not exactly:

f<-function(){s();print(a)}
s<-function()assign(x="a",value="ok",pos=sys.parent())
f() #I want to get "ok"
a #I do not want "a" in global enviorment, so here I should get
#Error: Object "a" not found
ff<-function()f() #here I also want to get "ok" - it should not matter if
the parent fuction has any parents

Thank you in advance for suggestions!

Ales Ziberna

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Putting an object in to a function that calls the current function

Gabor Grothendieck
Check out:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/64929.html

On 1/4/06, Ales Ziberna <[hidden email]> wrote:

> Hello!
>
> I would like to put an object in to a function that calls the current
> function.
>
> I thought the answer will be clear to me after reading the help files:
> ?assign
> ?sys.parent
>
> However it is not.
> Here is an example I thought should work, however it dose not exactly:
>
> f<-function(){s();print(a)}
> s<-function()assign(x="a",value="ok",pos=sys.parent())
> f() #I want to get "ok"
> a #I do not want "a" in global enviorment, so here I should get
> #Error: Object "a" not found
> ff<-function()f() #here I also want to get "ok" - it should not matter if
> the parent fuction has any parents
>
> Thank you in advance for suggestions!
>
> Ales Ziberna
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Putting an object in to a function that calls the current function

Duncan Murdoch
In reply to this post by Ales Ziberna
On 1/4/2006 9:14 AM, Ales Ziberna wrote:

> Hello!
>
> I would like to put an object in to a function that calls the current
> function.
>
> I thought the answer will be clear to me after reading the help files:
> ?assign
> ?sys.parent
>
> However it is not.
> Here is an example I thought should work, however it dose not exactly:
>
> f<-function(){s();print(a)}
> s<-function()assign(x="a",value="ok",pos=sys.parent())
> f() #I want to get "ok"
> a #I do not want "a" in global enviorment, so here I should get
> #Error: Object "a" not found
> ff<-function()f() #here I also want to get "ok" - it should not matter if
> the parent fuction has any parents
>
> Thank you in advance for suggestions!

That's not a good idea.  Why would you want to do something like that?

That out of the way, here's a function that does it:

f<-function(){s();print(a)}
s<-function()assign(x="a",value="ok",env=parent.frame())

The difference between pos=sys.parent() and env=parent.frame() is that
the pos is interpreted as a position in the search list (see ?assign),
while parent.frame() gives you the environment from the stack,
equivalent to sys.frame(sys.parent()).

In R you're almost certainly better off working directly with
environments, rather than going through integer indexing the way you
(used to?) have to do in S-PLUS.

Did I mention that messing with the environment of your caller is a bad
idea?  It's not yours, don't touch it.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Putting an object in to a function that calls the current function

Ales Ziberna
Thank you both (Duncan Murdoch and  Gabor Grotehendieck) for your answers.
Both work and my problem is solved.

I do aggree with Duncan Murdoch that usually messing with the environment of
your caller is a bad idea. The reason why I still want to do it in this case
is that I exactly know which functions are calling (the function is NEVER
called directly) it and it was in this case easier to use this than to
modify each of the fuctions that are calling it.

Thanks again!
Ales Ziberna

-----Original Message-----
From: Duncan Murdoch [mailto:[hidden email]]
Sent: Wednesday, January 04, 2006 3:26 PM
To: Ales Ziberna
Cc: R-help
Subject: Re: [R] Putting an object in to a function that calls the current
function

On 1/4/2006 9:14 AM, Ales Ziberna wrote:

> Hello!
>
> I would like to put an object in to a function that calls the current
> function.
>
> I thought the answer will be clear to me after reading the help files:
> ?assign
> ?sys.parent
>
> However it is not.
> Here is an example I thought should work, however it dose not exactly:
>
> f<-function(){s();print(a)}
> s<-function()assign(x="a",value="ok",pos=sys.parent())
> f() #I want to get "ok"
> a #I do not want "a" in global enviorment, so here I should get
> #Error: Object "a" not found
> ff<-function()f() #here I also want to get "ok" - it should not matter
> if the parent fuction has any parents
>
> Thank you in advance for suggestions!

That's not a good idea.  Why would you want to do something like that?

That out of the way, here's a function that does it:

f<-function(){s();print(a)}
s<-function()assign(x="a",value="ok",env=parent.frame())

The difference between pos=sys.parent() and env=parent.frame() is that the
pos is interpreted as a position in the search list (see ?assign), while
parent.frame() gives you the environment from the stack, equivalent to
sys.frame(sys.parent()).

In R you're almost certainly better off working directly with environments,
rather than going through integer indexing the way you (used to?) have to do
in S-PLUS.

Did I mention that messing with the environment of your caller is a bad
idea?  It's not yours, don't touch it.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Putting an object in to a function that calls the current function

Duncan Murdoch
On 1/4/2006 10:32 AM, Ales Ziberna wrote:
> Thank you both (Duncan Murdoch and  Gabor Grotehendieck) for your answers.
> Both work and my problem is solved.
>
> I do aggree with Duncan Murdoch that usually messing with the environment of
> your caller is a bad idea. The reason why I still want to do it in this case
> is that I exactly know which functions are calling (the function is NEVER
> called directly) it and it was in this case easier to use this than to
> modify each of the fuctions that are calling it.

Using R's lexical scope may lead to a cleaner solution.  That is, you
define the functions within the one that calls them; then a <<- "ok"
would do what you want (provided "a" existed in the enclosure at the time).

For example,

f <- function() {
        a <- "init"
        s <- function() {
                a <<- "ok"
         }
        s()
        print(a)
}

Duncan Murdoch

>
> Thanks again!
> Ales Ziberna
>
> -----Original Message-----
> From: Duncan Murdoch [mailto:[hidden email]]
> Sent: Wednesday, January 04, 2006 3:26 PM
> To: Ales Ziberna
> Cc: R-help
> Subject: Re: [R] Putting an object in to a function that calls the current
> function
>
> On 1/4/2006 9:14 AM, Ales Ziberna wrote:
>> Hello!
>>
>> I would like to put an object in to a function that calls the current
>> function.
>>
>> I thought the answer will be clear to me after reading the help files:
>> ?assign
>> ?sys.parent
>>
>> However it is not.
>> Here is an example I thought should work, however it dose not exactly:
>>
>> f<-function(){s();print(a)}
>> s<-function()assign(x="a",value="ok",pos=sys.parent())
>> f() #I want to get "ok"
>> a #I do not want "a" in global enviorment, so here I should get
>> #Error: Object "a" not found
>> ff<-function()f() #here I also want to get "ok" - it should not matter
>> if the parent fuction has any parents
>>
>> Thank you in advance for suggestions!
>
> That's not a good idea.  Why would you want to do something like that?
>
> That out of the way, here's a function that does it:
>
> f<-function(){s();print(a)}
> s<-function()assign(x="a",value="ok",env=parent.frame())
>
> The difference between pos=sys.parent() and env=parent.frame() is that the
> pos is interpreted as a position in the search list (see ?assign), while
> parent.frame() gives you the environment from the stack, equivalent to
> sys.frame(sys.parent()).
>
> In R you're almost certainly better off working directly with environments,
> rather than going through integer indexing the way you (used to?) have to do
> in S-PLUS.
>
> Did I mention that messing with the environment of your caller is a bad
> idea?  It's not yours, don't touch it.
>
> Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Putting an object in to a function that calls the current function

Ales Ziberna-2
I do not belive this would work in my case, since as I said, the function is
called by several different functions.

Ales Ziberna

-----Original Message-----
From: Duncan Murdoch [mailto:[hidden email]]
Sent: Wednesday, January 04, 2006 5:05 PM
To: Ales Ziberna
Cc: 'R-help'
Subject: Re: [R] Putting an object in to a function that calls the current
function

On 1/4/2006 10:32 AM, Ales Ziberna wrote:
> Thank you both (Duncan Murdoch and  Gabor Grotehendieck) for your answers.
> Both work and my problem is solved.
>
> I do aggree with Duncan Murdoch that usually messing with the
> environment of your caller is a bad idea. The reason why I still want
> to do it in this case is that I exactly know which functions are
> calling (the function is NEVER called directly) it and it was in this
> case easier to use this than to modify each of the fuctions that are
calling it.

Using R's lexical scope may lead to a cleaner solution.  That is, you define
the functions within the one that calls them; then a <<- "ok"
would do what you want (provided "a" existed in the enclosure at the time).

For example,

f <- function() {
        a <- "init"
        s <- function() {
                a <<- "ok"
         }
        s()
        print(a)
}

Duncan Murdoch

>
> Thanks again!
> Ales Ziberna
>
> -----Original Message-----
> From: Duncan Murdoch [mailto:[hidden email]]
> Sent: Wednesday, January 04, 2006 3:26 PM
> To: Ales Ziberna
> Cc: R-help
> Subject: Re: [R] Putting an object in to a function that calls the
> current function
>
> On 1/4/2006 9:14 AM, Ales Ziberna wrote:
>> Hello!
>>
>> I would like to put an object in to a function that calls the current
>> function.
>>
>> I thought the answer will be clear to me after reading the help files:
>> ?assign
>> ?sys.parent
>>
>> However it is not.
>> Here is an example I thought should work, however it dose not exactly:
>>
>> f<-function(){s();print(a)}
>> s<-function()assign(x="a",value="ok",pos=sys.parent())
>> f() #I want to get "ok"
>> a #I do not want "a" in global enviorment, so here I should get
>> #Error: Object "a" not found
>> ff<-function()f() #here I also want to get "ok" - it should not
>> matter if the parent fuction has any parents
>>
>> Thank you in advance for suggestions!
>
> That's not a good idea.  Why would you want to do something like that?
>
> That out of the way, here's a function that does it:
>
> f<-function(){s();print(a)}
> s<-function()assign(x="a",value="ok",env=parent.frame())
>
> The difference between pos=sys.parent() and env=parent.frame() is that
> the pos is interpreted as a position in the search list (see ?assign),
> while
> parent.frame() gives you the environment from the stack, equivalent to
> sys.frame(sys.parent()).
>
> In R you're almost certainly better off working directly with
> environments, rather than going through integer indexing the way you
> (used to?) have to do in S-PLUS.
>
> Did I mention that messing with the environment of your caller is a
> bad idea?  It's not yours, don't touch it.
>
> Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Putting an object in to a function that calls the current function

Gabor Grothendieck
In reply to this post by Duncan Murdoch
Often situations like this can be represented using object
oriented concepts.  The data that you want to access that is
outside your function is wrapped in an object and the function
becomes a method of that object.

Using Duncan's example and the proto package we
define object oo with variable a and method s and
then invoke s.

library(proto)
oo <- proto(a = "init", s = function(.)  .$a <- "ok")
oo$a
oo$s()
oo$a

This could also be done using the R.oo package.

On 1/4/06, Duncan Murdoch <[hidden email]> wrote:

> On 1/4/2006 10:32 AM, Ales Ziberna wrote:
> > Thank you both (Duncan Murdoch and  Gabor Grotehendieck) for your answers.
> > Both work and my problem is solved.
> >
> > I do aggree with Duncan Murdoch that usually messing with the environment of
> > your caller is a bad idea. The reason why I still want to do it in this case
> > is that I exactly know which functions are calling (the function is NEVER
> > called directly) it and it was in this case easier to use this than to
> > modify each of the fuctions that are calling it.
>
> Using R's lexical scope may lead to a cleaner solution.  That is, you
> define the functions within the one that calls them; then a <<- "ok"
> would do what you want (provided "a" existed in the enclosure at the time).
>
> For example,
>
> f <- function() {
>        a <- "init"
>        s <- function() {
>                a <<- "ok"
>         }
>        s()
>        print(a)
> }
>
> Duncan Murdoch
>
> >
> > Thanks again!
> > Ales Ziberna
> >
> > -----Original Message-----
> > From: Duncan Murdoch [mailto:[hidden email]]
> > Sent: Wednesday, January 04, 2006 3:26 PM
> > To: Ales Ziberna
> > Cc: R-help
> > Subject: Re: [R] Putting an object in to a function that calls the current
> > function
> >
> > On 1/4/2006 9:14 AM, Ales Ziberna wrote:
> >> Hello!
> >>
> >> I would like to put an object in to a function that calls the current
> >> function.
> >>
> >> I thought the answer will be clear to me after reading the help files:
> >> ?assign
> >> ?sys.parent
> >>
> >> However it is not.
> >> Here is an example I thought should work, however it dose not exactly:
> >>
> >> f<-function(){s();print(a)}
> >> s<-function()assign(x="a",value="ok",pos=sys.parent())
> >> f() #I want to get "ok"
> >> a #I do not want "a" in global enviorment, so here I should get
> >> #Error: Object "a" not found
> >> ff<-function()f() #here I also want to get "ok" - it should not matter
> >> if the parent fuction has any parents
> >>
> >> Thank you in advance for suggestions!
> >
> > That's not a good idea.  Why would you want to do something like that?
> >
> > That out of the way, here's a function that does it:
> >
> > f<-function(){s();print(a)}
> > s<-function()assign(x="a",value="ok",env=parent.frame())
> >
> > The difference between pos=sys.parent() and env=parent.frame() is that the
> > pos is interpreted as a position in the search list (see ?assign), while
> > parent.frame() gives you the environment from the stack, equivalent to
> > sys.frame(sys.parent()).
> >
> > In R you're almost certainly better off working directly with environments,
> > rather than going through integer indexing the way you (used to?) have to do
> > in S-PLUS.
> >
> > Did I mention that messing with the environment of your caller is a bad
> > idea?  It's not yours, don't touch it.
> >
> > Duncan Murdoch
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Putting an object in to a function that calls the current function

Duncan Murdoch
In reply to this post by Ales Ziberna-2
On 1/4/2006 11:22 AM, Ales Ziberna wrote:
> I do not belive this would work in my case, since as I said, the function is
> called by several different functions.

This may not be welcome advice, but I don't think that is a great idea
either.  Conventionally using the same name for the same purpose in
different functions is generally a good idea, but writing functions that
depend on things being named identically is risky.  You may find that in
2 years when you are editing this code that you change the name, but
miss one of the locations:  then you may get a very obscure bug.  Or you
may be reading through your function and forget that s() changes the
value of a variable, so you won't understand what is going on in your code.

High level source code is meant equally for humans to read as it is for
the interpreter to act on, so you should write it clearly.

I don't know the context of your problem, but I'd think a better
approach would be to have your s() function return the variable a as
part of its return value (so the several different functions could store
it where they liked), or to define a in an environment where it is
accessible from all the functions that need to use it including s(), and
do something like the lexical scoping solution below.

Duncan Murdoch


>
> Ales Ziberna
>
> -----Original Message-----
> From: Duncan Murdoch [mailto:[hidden email]]
> Sent: Wednesday, January 04, 2006 5:05 PM
> To: Ales Ziberna
> Cc: 'R-help'
> Subject: Re: [R] Putting an object in to a function that calls the current
> function
>
> On 1/4/2006 10:32 AM, Ales Ziberna wrote:
>> Thank you both (Duncan Murdoch and  Gabor Grotehendieck) for your answers.
>> Both work and my problem is solved.
>>
>> I do aggree with Duncan Murdoch that usually messing with the
>> environment of your caller is a bad idea. The reason why I still want
>> to do it in this case is that I exactly know which functions are
>> calling (the function is NEVER called directly) it and it was in this
>> case easier to use this than to modify each of the fuctions that are
> calling it.
>
> Using R's lexical scope may lead to a cleaner solution.  That is, you define
> the functions within the one that calls them; then a <<- "ok"
> would do what you want (provided "a" existed in the enclosure at the time).
>
> For example,
>
> f <- function() {
> a <- "init"
> s <- function() {
> a <<- "ok"
>          }
> s()
> print(a)
> }
>
> Duncan Murdoch
>
>>
>> Thanks again!
>> Ales Ziberna
>>
>> -----Original Message-----
>> From: Duncan Murdoch [mailto:[hidden email]]
>> Sent: Wednesday, January 04, 2006 3:26 PM
>> To: Ales Ziberna
>> Cc: R-help
>> Subject: Re: [R] Putting an object in to a function that calls the
>> current function
>>
>> On 1/4/2006 9:14 AM, Ales Ziberna wrote:
>>> Hello!
>>>
>>> I would like to put an object in to a function that calls the current
>>> function.
>>>
>>> I thought the answer will be clear to me after reading the help files:
>>> ?assign
>>> ?sys.parent
>>>
>>> However it is not.
>>> Here is an example I thought should work, however it dose not exactly:
>>>
>>> f<-function(){s();print(a)}
>>> s<-function()assign(x="a",value="ok",pos=sys.parent())
>>> f() #I want to get "ok"
>>> a #I do not want "a" in global enviorment, so here I should get
>>> #Error: Object "a" not found
>>> ff<-function()f() #here I also want to get "ok" - it should not
>>> matter if the parent fuction has any parents
>>>
>>> Thank you in advance for suggestions!
>>
>> That's not a good idea.  Why would you want to do something like that?
>>
>> That out of the way, here's a function that does it:
>>
>> f<-function(){s();print(a)}
>> s<-function()assign(x="a",value="ok",env=parent.frame())
>>
>> The difference between pos=sys.parent() and env=parent.frame() is that
>> the pos is interpreted as a position in the search list (see ?assign),
>> while
>> parent.frame() gives you the environment from the stack, equivalent to
>> sys.frame(sys.parent()).
>>
>> In R you're almost certainly better off working directly with
>> environments, rather than going through integer indexing the way you
>> (used to?) have to do in S-PLUS.
>>
>> Did I mention that messing with the environment of your caller is a
>> bad idea?  It's not yours, don't touch it.
>>
>> Duncan Murdoch
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Putting an object in to a function that calls the current function

Gabor Grothendieck
In reply to this post by Ales Ziberna-2
Try

   demo(scoping)

which shows a situation similar to the one Duncan describes
but uses several functions.

On 1/4/06, Ales Ziberna <[hidden email]> wrote:

> I do not belive this would work in my case, since as I said, the function is
> called by several different functions.
>
> Ales Ziberna
>
> -----Original Message-----
> From: Duncan Murdoch [mailto:[hidden email]]
> Sent: Wednesday, January 04, 2006 5:05 PM
> To: Ales Ziberna
> Cc: 'R-help'
> Subject: Re: [R] Putting an object in to a function that calls the current
> function
>
> On 1/4/2006 10:32 AM, Ales Ziberna wrote:
> > Thank you both (Duncan Murdoch and  Gabor Grotehendieck) for your answers.
> > Both work and my problem is solved.
> >
> > I do aggree with Duncan Murdoch that usually messing with the
> > environment of your caller is a bad idea. The reason why I still want
> > to do it in this case is that I exactly know which functions are
> > calling (the function is NEVER called directly) it and it was in this
> > case easier to use this than to modify each of the fuctions that are
> calling it.
>
> Using R's lexical scope may lead to a cleaner solution.  That is, you define
> the functions within the one that calls them; then a <<- "ok"
> would do what you want (provided "a" existed in the enclosure at the time).
>
> For example,
>
> f <- function() {
>        a <- "init"
>        s <- function() {
>                a <<- "ok"
>         }
>        s()
>        print(a)
> }
>
> Duncan Murdoch
>
> >
> > Thanks again!
> > Ales Ziberna
> >
> > -----Original Message-----
> > From: Duncan Murdoch [mailto:[hidden email]]
> > Sent: Wednesday, January 04, 2006 3:26 PM
> > To: Ales Ziberna
> > Cc: R-help
> > Subject: Re: [R] Putting an object in to a function that calls the
> > current function
> >
> > On 1/4/2006 9:14 AM, Ales Ziberna wrote:
> >> Hello!
> >>
> >> I would like to put an object in to a function that calls the current
> >> function.
> >>
> >> I thought the answer will be clear to me after reading the help files:
> >> ?assign
> >> ?sys.parent
> >>
> >> However it is not.
> >> Here is an example I thought should work, however it dose not exactly:
> >>
> >> f<-function(){s();print(a)}
> >> s<-function()assign(x="a",value="ok",pos=sys.parent())
> >> f() #I want to get "ok"
> >> a #I do not want "a" in global enviorment, so here I should get
> >> #Error: Object "a" not found
> >> ff<-function()f() #here I also want to get "ok" - it should not
> >> matter if the parent fuction has any parents
> >>
> >> Thank you in advance for suggestions!
> >
> > That's not a good idea.  Why would you want to do something like that?
> >
> > That out of the way, here's a function that does it:
> >
> > f<-function(){s();print(a)}
> > s<-function()assign(x="a",value="ok",env=parent.frame())
> >
> > The difference between pos=sys.parent() and env=parent.frame() is that
> > the pos is interpreted as a position in the search list (see ?assign),
> > while
> > parent.frame() gives you the environment from the stack, equivalent to
> > sys.frame(sys.parent()).
> >
> > In R you're almost certainly better off working directly with
> > environments, rather than going through integer indexing the way you
> > (used to?) have to do in S-PLUS.
> >
> > Did I mention that messing with the environment of your caller is a
> > bad idea?  It's not yours, don't touch it.
> >
> > Duncan Murdoch
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html