Function Value Not Being Overwritten?

View: New views
2 Messages — Rating Filter:   Alert me  

Function Value Not Being Overwritten?

by JustADude :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Part of my problem is that I am in the middle of starting to use functions, so this was unexpected behavior for me.  Maybe there is a work-around because I like declaring some of the variables prior to using them.  

I have two separate files:

# File: Test.R
dog<-function(x)
{
    templeton<-NULL
    source("Cat.R")
    print(templeton)
    print(x)
   
    print(bobby)
}




# File Cat.R
templeton<-c(7)
bobby<-c("This", "is", "a", "test.")



Now running the function in the RGui...
> source("Test.R")
> dog(13)
NULL
[1] 13
[1] "This"  "is"    "a"     "test."


My question is why is "templeton" equal to NULL? 

I expected "templeton" to be equal to "7".  Notice that bobby came across fine.

Is there anyway for the value of "templeton" set in Cat.R to come across successfully?

Thanks for any feedback and insights.




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

Re: Function Value Not Being Overwritten?

by Charles C. Berry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, 6 Nov 2009, Jason Rupert wrote:

> Part of my problem is that I am in the middle of starting to use
> functions, so this was unexpected behavior for me. Maybe there is a
> work-around because I like declaring some of the variables prior to
> using them.  

See

  ?source

and note

local if local is FALSE, the statements scanned are evaluated in the
  user's workspace (the global environment), otherwise in the
  environment calling source.

Maybe you want source( <...>, local=TRUE)

Chuck


>
> I have two separate files:
>
> # File: Test.R
> dog<-function(x)
> {
>     templeton<-NULL
>     source("Cat.R")
>     print(templeton)
>     print(x)
>    
>     print(bobby)
> }
>
>
>
>
> # File Cat.R
> templeton<-c(7)
> bobby<-c("This", "is", "a", "test.")
>
>
>
> Now running the function in the RGui...
>> source("Test.R")
>> dog(13)
> NULL
> [1] 13
> [1] "This"  "is"    "a"     "test."
>
>
> My question is why is "templeton" equal to NULL? 
>
> I expected "templeton" to be equal to "7".  Notice that bobby came across fine.
>
> Is there anyway for the value of "templeton" set in Cat.R to come across successfully?
>
> Thanks for any feedback and insights.
>
>
>
>
> ______________________________________________
> 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.
>
Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry@...            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901


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