memory efficient way of passing large objects in R

2019-05-15 06:24发布

I have a function that needs to access a variable in its parent environment (scope from which the function is called). The variable is large in terms of memory so I would prefer not to pass it to by value to the function being called. Is there a standard way of doing this other than declaring the variable in the global scope? For example:

g <- function (a, b) { #do stuff}

f <- function(x) {
    y <- 3 #but in my program y is very large
    g(x, y)
}

I would like to access y in g(). So something like this:

g <- function (a) { a+y }

f <- function(x) {
    y <- 3 #but in my program y is very large
    g(x)
}

Is this possible?

Thanks

1条回答
祖国的老花朵
2楼-- · 2019-05-15 06:53

There is no advantage to "declaring the variable in the global scope" and it may not even be possible in R depending on what you mean by that. You certainly could use the second form. The action that causes duplicate or even triplicate copies of an object is assignment. You will need to describe in more detail what you are trying to illustrate by the code: y <- 3. That would not normally be needed inside a function that merely accessed an object named "y" that was located in an enclosing frame.

Storing variables in a declared environment will sometimes improve efficiency of access, but my understanding is that the efficiency is in terms of improved speed because a hash table is used. One accesses items in an environment in the same manner as one accesses list elements:

> evn <- new.env()
> evn$a <- rnorm(100000)
> ls(evn)
[1] "a"
> length(evn$a)
[1] 100000

The BigMemory project may offer facilities for this: http://www.bigmemory.org/ . It and Lumley's biglm may help with the large dataset mentioned in the comments.

查看更多
登录 后发表回答