You need access to the environment associated with the fOuter
function--where the call to fInner
is. You can get this with parent.frame
, and you can get and set variables with get
and assign
:
fInner<-function()
{
assign("I", get("I", envir=parent.frame()) + 1, envir=parent.frame())
}
fOuter<-function()
{
I<-0
fInner()
print(I)
}
See ?environment
and ?parent.frame
for more info.
But this is just to satisfy your curiosity! You seem to agree that this is not a good idea. Manipulating environments and names can quickly get complicated, and unless you're doing something truly complex, there's probably a better way.