[BioC] Assigning values to environments - merging them?

Wolfgang Huber huber at ebi.ac.uk
Tue Jun 10 19:27:28 CEST 2008


Dear Helen,

for your first question, try with setting "hash=TRUE" in your call to 
"new.env". See below (absolute timings are of course machine dependent).

e1=new.env()
e2=new.env(hash=TRUE)
nm=paste(1:25000)

print(
   system.time(
     for(v in nm)
       assign(v, pi, envir=e1)
   )
)

    user  system elapsed
   3.004   0.008   3.025


print(
   system.time(
     for(v in nm)
       assign(v, pi, envir=e2)
   )
)

   user  system elapsed
   0.152   0.000   0.151


----------------------------------

Re your second question: a) you could have a look at the function 
"mget", and at "multiassign" in Biobase. The first is written in C and 
promises to be fast, the second contains a loop written in R, but may 
still be fast enough.

b) you could set the parent environment of one of your environments to 
be the other; when you look up a name in the former, and it is not found 
there, the value found in the parent is returned.

  a=new.env()
  b=new.env()
  parent.env(b) = a
  assign("q", 1, env=a)
  get("q", a)
  [1] 1
  get("q", b)
  [1] 1

c) Note that your example code probably does not do what you expect:

  e1 <- e2 <- new.env(parent = emptyenv())
 > e1
<environment: 0x27813e0>
 > e2
<environment: 0x27813e0>

They point to the *same* workspace in memory.

If you want to learn more about environments (and many other useful 
things), I recommend this book
http://www.amazon.co.uk/Programming-Bioinformatics-Chapman-Computer-Analysis/dp/1420063677

  Best wishes
  Wolfgang

------------------------------------------------------------------
Wolfgang Huber  EBI/EMBL  Cambridge UK  http://www.ebi.ac.uk/huber

Helen Zhou a écrit 10/06/2008 17:36:
> Dear Sir/Madam
> 
> I have two questions.
> 
> 1) I am currently trying to add a lot of values to an
> environment. A simplistic example is:
> 
> names	<- as.character(1:100000)
> values	<- data.frame(a=rep("a", 4), b=(rep("b", 4)))
> environment	<- new.env(parent=emptyenv())
> for (i in 1:100000) {
> 	assign(names[i], values, envir=environment)	
> }
> 
> I find that as I gets bigger this runs increasingly
> slow. Is this caused by the increasing size of the
> enviroment? Or by something else? And is there a way
> for me to avoid this?
> 
> 2) Assume that I have two different environments that
> I would like to merge into one so all the values are
> shared, how can I accomplish this? For example with:
> 
> e1 <- new.env(parent = emptyenv())
> e2 <- new.env(parent = emptyenv())
> assign("a", 3, envir=e1)
> assign("b", 4, envir=e2)
> 
> how can I join e1 and e2 so I have for example e3
> containing all the values. This seems to happen if I
> have:
> 
> e1 <- e2 <- new.env(parent = emptyenv())
> assign("a", 3, envir=e1)
> assign("b", 4, envir=e2)
> 
> but is there a way to do it for enviroments that are
> already existing.
> 
> Thanks you for any help you can give me. I am sorry if
> my questions are very simple, but I am a bit confused
> by the concept of environemnts and how to work with
> them.
> 
> Yours truly.
> Mrs Helen Zhou
> 
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor



More information about the Bioconductor mailing list