[R] understanding the FUNCTION function
Wet Bell Diver
wetbelldiver at gmail.com
Thu Apr 26 09:00:21 CEST 2012
In addition: the object MyNumberIs *is* created, but inside the
environment "attached" to the function as you evaluate it. And this
environment is gone when the function is done. (there is more to it, but
this is basically how it works). This is what Michael refers to in his
answer.
So, when you look for MyNumberIs in the global environment, it is not
there, since it was never created there, nor was it copied to it.
If you were adamant about finding it there, you could change the
function into
Number <- function(x) {MyNumberIs <<- x}
which would work in your example. Note, however, that this is almost
always ill-advised.
See ?"<<-"
--Peter
Op 25-4-2012 23:07, R. Michael Weylandt <michael.weylandt at gmail.com>
schreef:
> Functions are not subroutines; that is, the effects (including assignments) are not global.
>
> You need something like
>
> F<- function(x) x
> A<- F(3)
>
> But that seems unnecessary...
>
> A<- 3
>
> Michael
>
> On Apr 25, 2012, at 10:06 PM, michaelyb<cel81009759 at gmail.com> wrote:
>
>> Hello,
>>
>> I am trying to understand why the FUNCTION used in several codes, won't
>> create the object after it finishes running the code.
>> For instance, look at the following:
>>
>> Number<- function(x) {MyNumberIs<-x}
>>
>> When I run
>> Number(5)
>> Everything goes well, except that if I try to call the object MyNumberIs, I
>> won't find it.
>>
>> I understand that this function can assume many parameters, but why won't it
>> create the object?
>>
>> Besides, if I try "assing", it won't work either, no matter how I do it.
>>
>> Any advice/explanation?
>>
>> Thankk you very much!
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/understanding-the-FUNCTION-function-tp4588681p4588681.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org 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.
> ______________________________________________
> R-help at r-project.org 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.
More information about the R-help
mailing list