[R] Function Value Not Being Overwritten?

Charles C. Berry cberry at tajo.ucsd.edu
Sat Nov 7 04:36:21 CET 2009


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

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list