Function does not see variables outside the function

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

Function does not see variables outside the function

Zhong-Yuan Zhang
Dear experts:

     In MATLAB, functions cannot see variables outside the

functions.  However, in R, the functions can do that. Is there

any settings that can disable this ability of functions?

     Many thanks for your kind help.

     Best Regards Always.

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Function does not see variables outside the function

Rui Barradas
Hello,

I believe the answer is no. Functions will first look in their
environment, and then in the parent frame, i.e., outside the function.

Hope this helps,

Rui Barradas

Em 05-11-2013 10:42, Zhong-Yuan Zhang escreveu:

> Dear experts:
>
>       In MATLAB, functions cannot see variables outside the
>
> functions.  However, in R, the functions can do that. Is there
>
> any settings that can disable this ability of functions?
>
>       Many thanks for your kind help.
>
>       Best Regards Always.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Function does not see variables outside the function

Carl Witthoft
In reply to this post by Zhong-Yuan Zhang
Why would you want to impose this restriction?  Perhaps if you explain what you are trying to do, we can suggest approaches that will satisfy your specific needs.
(note- one can always redefine whatever variables are to be "excluded." E.g. to keep the body of a function from referring to 'foo' in the calling environment, just add the line 'foo<-NA' inside the function)

Zhong-Yuan Zhang wrote
     In MATLAB, functions cannot see variables outside the

functions.  However, in R, the functions can do that. Is there

any settings that can disable this ability of functions?

   
______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Function does not see variables outside the function

Prof Brian Ripley
In reply to this post by Rui Barradas
On 05/11/2013 12:25, Rui Barradas wrote:
> Hello,
>
> I believe the answer is no. Functions will first look in their
> environment, and then in the parent frame, i.e., outside the function.

That is not correct.  The scoping rule when evaluatiing a function is to
look first in the evaluation frame, then the function's environment (see
?environment).  The parent frame is not part of the scope (unless part
of the environment).

You can set a function's environment to emptyenv(): then there will be
no search outside the function.  As that includes no search for any of
the functions implementing R itself, only very simple functions will be
self-contained.

You can control the search by setting a function's environment.  Most
likely that will achieve what you want to do (but have not told us).

>
> Hope this helps,
>
> Rui Barradas
>
> Em 05-11-2013 10:42, Zhong-Yuan Zhang escreveu:
>> Dear experts:
>>
>>       In MATLAB, functions cannot see variables outside the
>>
>> functions.  However, in R, the functions can do that. Is there
>>
>> any settings that can disable this ability of functions?
>>
>>       Many thanks for your kind help.
>>
>>       Best Regards Always.

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Function does not see variables outside the function

Zhong-Yuan Zhang
In reply to this post by Carl Witthoft
Dear Experts:

    I am very appreciate your comments and help!

    Actually I am a new comer from MATLAB. If the function

can see global variables, then it may output wrong results without

any error messages. For example, there is a gloabl variable named

v, and I write one funciton with one local variable x. However, in some
line,

I misspelled x to v, which would results in unexpected errors without
warning.

    In summary, I want to disable the ability to make debugging easier.

    Best.


2013/11/5 Carl Witthoft <[hidden email]>

> Why would you want to impose this restriction?  Perhaps if you explain what
> you are trying to do, we can suggest approaches that will satisfy your
> specific needs.
> (note- one can always redefine whatever variables are to be "excluded."
> E.g.
> to keep the body of a function from referring to 'foo' in the calling
> environment, just add the line 'foo<-NA' inside the function)
>
>
> Zhong-Yuan Zhang wrote
> >      In MATLAB, functions cannot see variables outside the
> >
> > functions.  However, in R, the functions can do that. Is there
> >
> > any settings that can disable this ability of functions?
> >
> >
> > ______________________________________________
>
> > R-help@
>
> >  mailing list
> > 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.
>
>
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Function-does-not-see-variables-outside-the-function-tp4679762p4679768.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>



--
Zhong-Yuan Zhang (PhD.)
Associate Professor
School of Statistics
Central University of Finance and Economics
39 South College Road, Haidian District, Beijing, P.R.China 100081
Email: [hidden email]
Homepage: http://en.stat.cufe.edu.cn/zhongyuanzhang/

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Function does not see variables outside the function

Fox, John
Dear Zhong-Yuan Zhang,

R is lexically scoped. Pretending that you're using a different programming
language is probably a bad idea.

The findGlobals() function in the codetools package, which is part of the
standard R distribution, can help you locate references to global variables
(and functions) in a function. For example,

> f <- function() g(a)

> findGlobals(f)
[1] "a" "g"

> ff <- function() {a <- 10; g(a)}

> findGlobals(ff)
[1] "{"  "<-" "g"

> fff <- function(a) g(a)

> findGlobals(fff)
[1] "g"

I hope this helps,
 John

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Zhong-Yuan Zhang
> Sent: Wednesday, November 06, 2013 7:32 AM
> To: [hidden email]
> Subject: Re: [R] Function does not see variables outside the function
>
> Dear Experts:
>
>     I am very appreciate your comments and help!
>
>     Actually I am a new comer from MATLAB. If the function
>
> can see global variables, then it may output wrong results without
>
> any error messages. For example, there is a gloabl variable named
>
> v, and I write one funciton with one local variable x. However, in some
> line,
>
> I misspelled x to v, which would results in unexpected errors without
> warning.
>
>     In summary, I want to disable the ability to make debugging easier.
>
>     Best.
>
>
> 2013/11/5 Carl Witthoft <[hidden email]>
>
> > Why would you want to impose this restriction?  Perhaps if you
> explain what
> > you are trying to do, we can suggest approaches that will satisfy
> your
> > specific needs.
> > (note- one can always redefine whatever variables are to be
> "excluded."
> > E.g.
> > to keep the body of a function from referring to 'foo' in the calling
> > environment, just add the line 'foo<-NA' inside the function)
> >
> >
> > Zhong-Yuan Zhang wrote
> > >      In MATLAB, functions cannot see variables outside the
> > >
> > > functions.  However, in R, the functions can do that. Is there
> > >
> > > any settings that can disable this ability of functions?
> > >
> > >
> > > ______________________________________________
> >
> > > R-help@
> >
> > >  mailing list
> > > 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.
> >
> >
> >
> >
> >
> > --
> > View this message in context:
> > http://r.789695.n4.nabble.com/Function-does-not-see-variables-
> outside-the-function-tp4679762p4679768.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [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
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
> Zhong-Yuan Zhang (PhD.)
> Associate Professor
> School of Statistics
> Central University of Finance and Economics
> 39 South College Road, Haidian District, Beijing, P.R.China 100081
> Email: [hidden email]
> Homepage: http://en.stat.cufe.edu.cn/zhongyuanzhang/
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Function does not see variables outside the function

Zhong-Yuan Zhang
Dear John Fox:


I highly appreciate

your help!!!  Problems solved.

     Best Wishes Always.


2013/11/6 John Fox <[hidden email]>

> Dear Zhong-Yuan Zhang,
>
> R is lexically scoped. Pretending that you're using a different programming
> language is probably a bad idea.
>
> The findGlobals() function in the codetools package, which is part of the
> standard R distribution, can help you locate references to global variables
> (and functions) in a function. For example,
>
> > f <- function() g(a)
>
> > findGlobals(f)
> [1] "a" "g"
>
> > ff <- function() {a <- 10; g(a)}
>
> > findGlobals(ff)
> [1] "{"  "<-" "g"
>
> > fff <- function(a) g(a)
>
> > findGlobals(fff)
> [1] "g"
>
> I hope this helps,
>  John
>
> > -----Original Message-----
> > From: [hidden email] [mailto:r-help-bounces@r-
> > project.org] On Behalf Of Zhong-Yuan Zhang
> > Sent: Wednesday, November 06, 2013 7:32 AM
> > To: [hidden email]
> > Subject: Re: [R] Function does not see variables outside the function
> >
> > Dear Experts:
> >
> >     I am very appreciate your comments and help!
> >
> >     Actually I am a new comer from MATLAB. If the function
> >
> > can see global variables, then it may output wrong results without
> >
> > any error messages. For example, there is a gloabl variable named
> >
> > v, and I write one funciton with one local variable x. However, in some
> > line,
> >
> > I misspelled x to v, which would results in unexpected errors without
> > warning.
> >
> >     In summary, I want to disable the ability to make debugging easier.
> >
> >     Best.
> >
> >
> > 2013/11/5 Carl Witthoft <[hidden email]>
> >
> > > Why would you want to impose this restriction?  Perhaps if you
> > explain what
> > > you are trying to do, we can suggest approaches that will satisfy
> > your
> > > specific needs.
> > > (note- one can always redefine whatever variables are to be
> > "excluded."
> > > E.g.
> > > to keep the body of a function from referring to 'foo' in the calling
> > > environment, just add the line 'foo<-NA' inside the function)
> > >
> > >
> > > Zhong-Yuan Zhang wrote
> > > >      In MATLAB, functions cannot see variables outside the
> > > >
> > > > functions.  However, in R, the functions can do that. Is there
> > > >
> > > > any settings that can disable this ability of functions?
> > > >
> > > >
> > > > ______________________________________________
> > >
> > > > R-help@
> > >
> > > >  mailing list
> > > > 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.
> > >
> > >
> > >
> > >
> > >
> > > --
> > > View this message in context:
> > > http://r.789695.n4.nabble.com/Function-does-not-see-variables-
> > outside-the-function-tp4679762p4679768.html
> > > Sent from the R help mailing list archive at Nabble.com.
> > >
> > > ______________________________________________
> > > [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
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >
> >
> > --
> > Zhong-Yuan Zhang (PhD.)
> > Associate Professor
> > School of Statistics
> > Central University of Finance and Economics
> > 39 South College Road, Haidian District, Beijing, P.R.China 100081
> > Email: [hidden email]
> > Homepage: http://en.stat.cufe.edu.cn/zhongyuanzhang/
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [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
> > and provide commented, minimal, self-contained, reproducible code.
>
>


--
Zhong-Yuan Zhang (PhD.)
Associate Professor
School of Statistics
Central University of Finance and Economics
39 South College Road, Haidian District, Beijing, P.R.China 100081
Email: [hidden email]
Homepage: http://en.stat.cufe.edu.cn/zhongyuanzhang/

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.