Apostolos Polymeros
Apostolos Polymeros

Reputation: 843

move into an environment

I would to ask if there is a function that send us into an environment. For example:

# create two environments
Env1 <- new.env()
Env2 <- new.env()

# assign one variable into each environment
assign("v1", "1", envir = Env1)
assign("v2", "2", envir = Env2)

# In order to refer to the variable in Env2 I have to use Env2$v2, for example 
print(Env2$v2)

# The question is if there is some function that sents us into Env2
# so that when we refer to the variable in Env2 to use just v2, that is
print(v2)

Thank you all

Upvotes: 2

Views: 163

Answers (1)

Matthew Lundberg
Matthew Lundberg

Reputation: 42689

Depending on what you mean by "refer to the variable," attach does this:

attach(Env2)
print(v2)
## [1] "2"

detach()
print(v2)
## Error in print(v2) : object 'v2' not found

Attempting to modify the value is a different story, as it is attached at position 2.

Upvotes: 2

Related Questions