statguy
2012-Jan-18  18:30 UTC
[R] How to define a variable in a function that another function uses without using global variables
I would like to know if it is possible to make a function that defines a
variable in another function without setting that variable globally?
I would i.e. be able to define a variables value inside a function ("one
level above) which another function makes use of, without setting this
variable globally.
I have provided this very simple illustrating example.
test1=function(x)
	{
	x+y
	}
test2=function(y1)
	{
	y=y1
	test1(2,y1)
	}
Running the second function results in an error:> test2(1)
Error in test1(2) : object 'y' not found
I see 2 possible solutions to this, but neither of them is preferred in my
more complex situation:
1. Setting y<<-y_1 globally in test2-function
2. making test1 a function of both x and y.
Is there any other way to do this except from the 2 above? I hope someone
can help me with this.
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-define-a-variable-in-a-function-that-another-function-uses-without-using-global-variables-tp4307604p4307604.html
Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Jan-18  18:51 UTC
[R] How to define a variable in a function that another function uses without using global variables
Consider this example:
power <- function(n){
    function(x) x^n
} # Note that this function returns a function!
cube <- power(3)
cube(2) # gives 8
That make sense?
Michael
On Wed, Jan 18, 2012 at 1:30 PM, statguy <martinju at mail.com>
wrote:> I would like to know if it is possible to make a function that defines a
> variable in another function without setting that variable globally?
>
> I would i.e. be able to define a variables value inside a function
("one
> level above) which another function makes use of, without setting this
> variable globally.
>
> I have provided this very simple illustrating example.
>
> test1=function(x)
> ? ? ? ?{
> ? ? ? ?x+y
> ? ? ? ?}
> test2=function(y1)
> ? ? ? ?{
> ? ? ? ?y=y1
> ? ? ? ?test1(2,y1)
> ? ? ? ?}
>
> Running the second function results in an error:
>> test2(1)
> Error in test1(2) : object 'y' not found
>
> I see 2 possible solutions to this, but neither of them is preferred in my
> more complex situation:
>
> 1. Setting y<<-y_1 globally in test2-function
> 2. making test1 a function of both x and y.
>
> Is there any other way to do this except from the 2 above? I hope someone
> can help me with this.
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/How-to-define-a-variable-in-a-function-that-another-function-uses-without-using-global-variables-tp4307604p4307604.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.
William Dunlap
2012-Jan-18  18:57 UTC
[R] How to define a variable in a function that another function uses without using global variables
Make an environment that the functions share. One way to do this is with local():> test <- local({ y <- NA+ f1 <- function(x) x + y + f2 <- function(y1) { y <<- y1 ; f1(1) } + list(f1=f1, f2=f2) })> test$f2(2^(1:3))[1] 3 5 9 (The example you showed must have had a typo in it, as it didn't give the error message you showed. I assume you typed 'test1(2,y1)' where you meant 'test1(2)'.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of statguy > Sent: Wednesday, January 18, 2012 10:30 AM > To: r-help at r-project.org > Subject: [R] How to define a variable in a function that another function uses without using global > variables > > I would like to know if it is possible to make a function that defines a > variable in another function without setting that variable globally? > > I would i.e. be able to define a variables value inside a function ("one > level above) which another function makes use of, without setting this > variable globally. > > I have provided this very simple illustrating example. > > test1=function(x) > { > x+y > } > test2=function(y1) > { > y=y1 > test1(2,y1) > } > > Running the second function results in an error: > > test2(1) > Error in test1(2) : object 'y' not found > > I see 2 possible solutions to this, but neither of them is preferred in my > more complex situation: > > 1. Setting y<<-y_1 globally in test2-function > 2. making test1 a function of both x and y. > > Is there any other way to do this except from the 2 above? I hope someone > can help me with this. > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-define-a-variable-in-a-function- > that-another-function-uses-without-using-global-variables-tp4307604p4307604.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.
statguy
2012-Jan-18  19:02 UTC
[R] How to define a variable in a function that another function uses without using global variables
Great! That works perfectly sense. Thanks a lot! -- View this message in context: http://r.789695.n4.nabble.com/How-to-define-a-variable-in-a-function-that-another-function-uses-without-using-global-variables-tp4307604p4307715.html Sent from the R help mailing list archive at Nabble.com.