Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"with" function behaviour

Tags:

r

Can anyone explain what causes the error in the last line of the code? Is it a bug?

> ll <- list(a=1, b=2)
> ee <- as.environment(ll)
> ee
<environment: 0x0000000004d35810>
> ls(ee)
[1] "a" "b"
> with(ee, a)
[1] 1
> with(ee, a - b)
Error in eval(expr, envir, enclos) : could not find function "-"
> 
like image 342
ggg Avatar asked Jun 22 '11 13:06

ggg


People also ask

What does function behavior mean?

In Applied Behavior Analysis (ABA), the reason a behavior continues is called the function of that behavior. These functions are reinforcers for the child. If the behavior no longer works for that purpose, the behavior will stop and a new behavior will take its place.

What are the 4 types of behavior?

A study on human behavior has revealed that 90% of the population can be classified into four basic personality types: Optimistic, Pessimistic, Trusting and Envious. However, the latter of the four types, Envious, is the most common, with 30% compared to 20% for each of the other groups.

What are examples of behavioral functions?

The four functions of behavior are sensory stimulation, escape, access to attention and access to tangibles.


2 Answers

This is due to R's scoping. It needs to find the function "-"(). You told R to evaluate your expression in the environment ee. There is no function "-"() there, so proceeded to the parent environment of ee, which is:

> parent.env(ee)
<environment: R_EmptyEnv>

where there is no function "-"() either. As there is no parent environment to the empty environment

> parent.env(parent.env(ee))
Error in parent.env(parent.env(ee)) : the empty environment has no parent

R gave up the search and threw an error.

We can solve the problem by attaching a parent environment to ee where R can find the function:

> parent.env(ee) <- .BaseNamespaceEnv
> with(ee, a - b)
[1] -1

But I think this would be more natural, to set the parent of ee to be the global environment:

> parent.env(ee) <- globalenv()
> with(ee, a - b)
[1] -1

a and b will always be found in ee as that is the first scoping environment encountered, but the functions can be looked up in the usual place, as if running this at the command line. If you are doing this in a function call, then you'll need to assign the correct environment.

like image 170
Gavin Simpson Avatar answered Sep 27 '22 23:09

Gavin Simpson


The - function isn't visible from the environment that you created.

If you assign it there,

ee$`-` <- `-`

then your example will work.

like image 28
Richie Cotton Avatar answered Sep 27 '22 22:09

Richie Cotton