0

I am new to R and I would like to understand what the difference is between Option 1 and Option 2 given below:

x <- 10

Option 1:

get <- x

Option 2:

get <- function() x

When I print(get) based on Option 1, my result is 10 and when I print(get()) based on Option 2, my result is 10 again. But I don't understand how the two assignments differ from each other.

0

2 Answers 2

1

With a slight modification of your code:

x <- 10
get1 <- x
get2 <- (function() x)()
# or equivalently:
# fun <- function() x
# get2 <- fun()

... there is no difference, exactly the same value is used. That is, identical(get1, get2) equals TRUE. In get2 we used an anonymous function, x is neither defined within the function nor given as an argument, so it is taken from the parent environment. get1 gets the value of x in a more straightforward way but when the assignment is done, that doesn't matter any more.

On the other hand, of course, functions have environments, so if you have time and you're not afraid of some waste of effort, you can do things like:

`environment<-`(function() x, new.env())()
# Error in `environment<-`(function() x, new.env())() : 
#  object 'x' not found

# notice that a more conventional way to achieve the above is ...
# foo <- function() x
# environment(foo) <- new.env()
# foo()

x not found! Where did x go? Nowhere, we just didn't find it as it was not in the search path.

`environment<-`(function() x, new.env(parent=environment()))()
# 10 
# so x was found!
# the anon. function's environment is new.env, with .GlobalEnv as parent
# as x is not found int an empty new.env, it is searched for in .GlobalEnv

So what? This can actually make a practical difference. Or at least, this can help one understand how environments work. Consider the following example:

m1 <- mean(1:5)  
# m1 == 3
m2 <- function() mean(1:5)
m2()
# 3
environment(m2) <- new.env()
m2()
# still 3
environment(m2) <- emptyenv()
m2()
# Error in m2() : could not find function "mean"
m2 <- function() 3+2
m2()
# [1] 5
environment(m2) <- emptyenv()
m2()
# Error in m2() : could not find function "+"

So names like mean or even + are variables whose values (the functions) have to be found somehow. They are usually there in the parent environments of the function's environment but we can remove this using environmnent(fun) <- emptyenv().

So what's the point so far? There are many but the most important one is it is a good practice to avoid functions "grabbing" some values from the global environment. That is, instead of:

 fun <- function() x
 fun()
 # [1] 10

... it is a better idea to use arguments for any "input data" you want to give to a function:

 fun <- function(x) x
 fun(x)
 # [1] 10
 fun(2) # equivalent to fun(x=2)
 # [1] 2
 fun(x+1)
 # [1] 11
Sign up to request clarification or add additional context in comments.

Comments

1

In your first example, you assing get the value of the variable x. In option 2, you assign get the value of a function with return value x (no parameter, edited). The result in this case is the same, because the sole function of the function in option 2 is to return x. Normally this isn't used, but sometimes 'anonymous' functions are used in the *apply functions.

1 Comment

Function with return value x, there is no parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.