pass a hash with list as element by reference in R -


i need pass reference in r r studio on win 7.

my code:

library(hash) myfunc<-function(myhash.arg) {   myhash <- myhash.arg   if (!has.key("first", myhash))     myhash["first"] <- list()   alist <- myhash["first"]   alist <- append(alist, 9)   eval.parent(substitute(myhash.arg<-myhash))   return(0) }  ahash<-hash() for(i in 1:5) {   myfunc(myhash.arg = ahash)   print(c("length of ahash ", length(ahash)))   print(c("length of ahash list ", length(ahash["first"]))) } 

but, list size 1, appended elements missed.

exapnding on comment above here generic, ugly, way pass things "by reference" in r. suppose have object named h in environment e , want pass function f reference. you'd this:

f = function(object) {      ...      # assign object or manipulate      ... }  f(h) # want see h has changed. 

doing in above way not change object, following code do:

f = function(env,obj.name) {     ...     # assignment done follows     env[[obj.name]] = ...     ... } f(e,"h") # h changed 

for example:

h = list(a=1,c=2) f = function(e,n) {     e[[n]]$c = 5     return(0) } f(.globalenv,"h") h 

that's it.

now can adapt case

i use method lot when have big datasets. still want organize work in functions operate on dataset (for example, make analysis reproducible, or able same analysis on different subsets, etc.), performance issues copying dataset problem. create environment called "working.data", , pass names of data.frames functions. hard code name of data environemtn (so don't need pass it), , deal naming/renaming of environment outside functions. not sure i'd recommend latter bit of solution.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -