I do get
> f1(a)
a= 1 2
b= 2 1
[1] TRUE
[1] "b"
however in this situations I use the envir paramenter of exists ( even if belong to some package you can still change it)
f2<-function(dataset){
print(exists(dataset, envir=envir=parent.frame()))
print(dataset) # do analysis on dataset b
}
If you really cannot modify f2 you have to "improve" f1...
a<-c(1,2)
f1<-function(dataset){
f2<-function(dataset){
print(exists(dataset, envir=parent.frame()))
print(dataset) # do analysis on dataset b
}
cat("a=",a,"\n")
b<-rev(a)
cat("b=",b,"\n")
f2("b")
}
> f1(a)
a= 1 2
b= 2 1
[1] TRUE
[1] "b"
the issue is, R looks for objects named "b" in the current environment (which the function b itself) then, if no object b is found, goes into the "upper" level...
this upper level is the parent environment inside which b is defined ( in your case the .GlobalEnv), and b is not there but inside f1.
Defining f2 inside f1 makes R going to look for b in f1 before going into the .GlabalEnv. My first solution made use of parent.frame, which explicitly asksR to look for the object into the environment where f2 was invoked, which is a different concept than parent environment.