Reputation: 829
I'd like to call reinforcements to help with this error as follows:
First I created my environment:
envizito <- new.env(parent = emptyenv())
attr(envizito, "name") <- "envizito"
and assigned a new variable called aaa
:
envizito$aaa <- 12
I created a function:
FUNn <- function() {print(envizito$aaa)}
environment(FUNn)
# <environment: R_GlobalEnv>
Then, I changed its environment to be in envizito
:
environment(FUNn) <- envizito
environment(FUNn)
# <environment: 0xc20b0a8>
#attr(,"name")
#[1] "envizito"
But after call FUNn()
it showed the error below:
FUNn()
# Error in { : could not find function "{"
PS: Although environment(FUNn)
points to the same place in memory it didn't appear when I had called ls()
ls(envir = envizito, all.names = TRUE)
# [1] "aaa"
Any advice and suggestions will be greatly appreciated! thank you for your attention.
Upvotes: 2
Views: 63
Reputation: 37879
{
like everything else in R which is not an object, is a function. When you created envizito
, you appointed the empty environment as its parent. The empty environment as its name hints, contains no objects i.e. it is empty.
When you changed FUNn
's environment to envisito
you made its parent environment the empty environment. When you call FUNn
(the function definition is in the global environment) it searches in its environment (i.e. envisito
) for function {
(the first function it finds in FUNn
). It cannot find {
within envisito
, so it looks one environment up. That environment, however, is the empty environment
which is empty. Therefore, it fails with an error that it cannot find function {
.
Upvotes: 5