[R] reporting multiple objects out of a function

andrewH ahoerner at rprogress.org
Wed Oct 5 23:14:02 CEST 2011


Thanks, Sina! This is very helpful and informative, but still not quite what
I want.

So, here is the thing: When a function returns an object, that object is
available in the calling environment.  If it is returned inside a function,
it is available in the function, but not outside of the function.  What I
want to do is simply to return more than one object in the usual sense in
which functions return objects.

Here is a test to see if a function fun does this, at least to the depth of
1.

obj1 <- 1
obj2 <- 2

cat("obj1 in global=", obj1)
cat("obj2 in global=", obj2)

wrapFun <- function(fun) {
   obj1 <- 3
   obj2 <- 4
   cat("obj1 in calling=", obj1)
   cat("obj2 in calling=", obj2)
   fun()
   cat("obj in calling=", obj)
   cat("obj1 in calling=", obj1)
   cat("obj2 in calling=", obj2)
}

cat("obj1 in global=", obj1)
cat("obj2 in global=", obj2)


Suppose the function "fun" assigns the values 5 and 6 to obj1 and obj2.  If
the function does what I want, this code should print:
obj1 in global=  1
obj2 in global=  2
obj1 in calling= 3
obj2 in calling= 4
obj1 in calling= 5
obj2 in calling= 6
obj1 in global=  1
obj2 in global=  2

I turned Paul’s and Sina’s code into functions as follows:
paulFun <- function() {
obj1 <<- 5; 
obj2 <<- 6; 
}

sinaFun <- function() {
attach(what = NULL, name = "my_env")
assign("obj1", 5, envir = as.environment("my_env"))
assign("obj1", 5, envir = as.environment("my_env"))
}

Running these two functions in the code above yields:

paulFun:
obj1 in global= 1
obj2 in global= 2
obj1 in calling= 3
obj2 in calling= 4
obj1 in calling= 3
obj2 in calling= 4
obj1 in global= 5
obj2 in global= 6

So paulFun puts the objects in the global environment but not in the calling
environment. Let’s try sinaFun:

sinaFun:
obj1 in global= 1
obj2 in global= 2 
obj1 in calling= 3
obj2 in calling= 4
obj1 in calling= 3
obj2 in calling= 4 
obj1 in global= 1
obj2 in global= 2

sinaFun puts the objects in the new environment it defines, but they are
available in neither the calling nor the global environment.  However, I was
immediately convinced that Sina had given me the tool I was missing: the
assign function. (Thanks, Sina!)  But I was wrong (or used it wrong), and
now I am even more deeply confused.  Here is a function that I thought would
do what I want:

andrewFun <- function() {
assign("obj1", 5, pos = sys.parent(n = 1))
assign("obj2", 6, pos = sys.parent(n = 1))
NULL
}

However, when I tried it, my results were the same as paulFun: assigned in
the global environment, but not in the calling environment.  Setting n = 0
seemed to limit the assignment to the interior of andrewFun: none of the
printed obj values were affected.

Help?

andrewH


--
View this message in context: http://r.789695.n4.nabble.com/reporting-multiple-objects-out-of-a-function-tp3873380p3876201.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list