[R] reporting multiple objects out of a function

Paul Hiemstra paul.hiemstra at knmi.nl
Thu Oct 6 09:55:26 CEST 2011


On 10/05/2011 09:14 PM, andrewH wrote:
> 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.

Hi,

As I understand it, you want to return multiple arguments without
returning them explicitly as an object. This can probably be done, by I
would advice against it because it makes your code harder to read. You
dump something in the calling environment, and a new user (maybe
yourself in a few months) has to do a lot of reasoning of what is
happening under the hood, which object is dumped in which environment. I
would just return a list. Alternatively, take a look at object oriented
programming like Gabor suggested. This, however, still involves
returning an object...

Again, I would recommend doing this the standard R way....

cheers,
Paul

> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Paul Hiemstra, Ph.D.
Global Climate Division
Royal Netherlands Meteorological Institute (KNMI)
Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
P.O. Box 201 | 3730 AE | De Bilt
tel: +31 30 2206 494

http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770



More information about the R-help mailing list