assigning phenoData via assign
2
0
Entering edit mode
@benilton-carvalho-1375
Last seen 4.1 years ago
Brazil/Campinas/UNICAMP
Hi everyone, I'm sorry if this was already discussed, as I did not succeed finding anything relevant in the archives. I'm trying to set the phenoData slot using get() or something similar, eg: library(Biobase) objName <- "sample.ExpressionSet" data(list=objName) pd <- phenoData(get(objName)) get(objName)@phenoData <- pd ## this will fail I could copy the object to a temporary one: tmp <- get(objName) tmp at phenoData <- pd assign(objName, tmp) rm(tmp) how can I avoid this copy? thanks a lot, b
• 747 views
ADD COMMENT
0
Entering edit mode
@martin-morgan-1513
Last seen 13 days ago
United States
Hi Benilton -- I know it looks like there's copying going on, but this is an _illusion_ that R very effectively maintains. Here's what happens with 'direct' access (things in <> are memory addresses of the objects; tracemem figures out when copies of the memory address is made): > tracemem(sample.ExpressionSet) [1] "<0x178df98>" > tracemem(sample.ExpressionSet at phenoData) [1] "<0x20f3828>" > phenoData(sample.ExpressionSet)=pd tracemem[0x178df98 -> 0x2163840]: tracemem[0x2163840 -> 0x216a858]: phenoData<- phenoData<- and in the indirect route: > tracemem(sample.ExpressionSet) [1] "<0x216a858>" > tracemem(sample.ExpressionSet at phenoData) [1] "<0x1d86ed8>" > tmp=get(objName) > phenoData(tmp)=pd tracemem[0x216a858 -> 0x1d86380]: tracemem[0x1d86380 -> 0x1d80af8]: phenoData<- phenoData<- > assign(objName, tmp) > rm(tmp) The surprising part is tmp=get(objName), which does NOT cause a memory copy -- instead, the sample.ExpressionSet object has a flag associated with it. Initially, the flag says 'there's only one reference to me'. With 'get' or other non-modifying assignments, the flag is incremented to say 'there's more than one reference to me'. The actual modification (assigning to phenoData) triggers the copy, just as it did in the direct way. Using tracemem requires that your R is configured with --enable-memory-profiling. There is, actually, an inefficiency here, but it's in the original version -- there's only one reference to sample.ExpressionSet, so modifying it with phenoData(sample.ExpressionSet) = pd does not need to make a copy of the whole instance, and assigning pd to the phenoData slot of the new instance also does not need to make a copy. This is hard to get around, though. Benilton Carvalho <bcarvalh at="" jhsph.edu=""> writes: > Hi everyone, > > I'm sorry if this was already discussed, as I did not succeed finding > anything relevant in the archives. > > I'm trying to set the phenoData slot using get() or something > similar, eg: > > library(Biobase) > objName <- "sample.ExpressionSet" > data(list=objName) > pd <- phenoData(get(objName)) > > get(objName)@phenoData <- pd ## this will fail As a matter of style, use phenoData(obj) <- pd rather than direct slot access; you'll get whatever benefits are provided by phenoData<-, and insulate your code from changes to underlying object structure. > I could copy the object to a temporary one: > > tmp <- get(objName) > tmp at phenoData <- pd > assign(objName, tmp) > rm(tmp) > > how can I avoid this copy? > > thanks a lot, > > b > > _______________________________________________ > 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 -- Martin Morgan Bioconductor / Computational Biology http://bioconductor.org
ADD COMMENT
0
Entering edit mode
Seth Falcon ★ 7.4k
@seth-falcon-992
Last seen 9.7 years ago
Hi Benilton, Benilton Carvalho <bcarvalh at="" jhsph.edu=""> writes: > I'm trying to set the phenoData slot using get() or something > similar, eg: First of all, you probably want to use phenoData(foo) <- updatedPheno, not direct slot access. > library(Biobase) > objName <- "sample.ExpressionSet" > data(list=objName) > pd <- phenoData(get(objName)) > > get(objName)@phenoData <- pd ## this will fail Perhaps it would help if you gave some context of what it is you are trying to achieve. Conceptually, get() returns a copy of the object requested and it doesn't make sense to modify an anonymous copy. Any modification of an object results in a copy (there are exceptions, but let's ignore them for now). But R is clever about when the copy is actually made (just assigning to a tmp var doesn't trigger a copy). > I could copy the object to a temporary one: > > tmp <- get(objName) > tmp at phenoData <- pd > assign(objName, tmp) > rm(tmp) > > how can I avoid this copy? I'm not sure you can, but maybe I'm not understanding what you really want to do. + seth -- Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center http://bioconductor.org
ADD COMMENT
0
Entering edit mode
Hi Martin and Morgan, I'm just writing some scripts to automate a few things here. My function takes an object name (say, 'foo') and a path. So, it loads data stored in path does lots of computations and stores the results in 'foo'. I have a helper function that builds the correct phenoData for 'foo'. So I wanted to modify 'foo' after the computations are done. Something like: myAutomator <- function(objName, path){ load(paste(path, "theData.rda", sep="/")) assign(objName, functionThatTakesLongTime(data)) ## here I'd like to modify phenoData save(list=objName, file=paste(objName, "rda", sep=".")) } But I just realized that for this particular case, the best is to pass the updated phenoData to functionThatTakesLongTime. b On May 7, 2007, at 1:12 PM, Seth Falcon wrote: > Hi Benilton, > > Benilton Carvalho <bcarvalh at="" jhsph.edu=""> writes: >> I'm trying to set the phenoData slot using get() or something >> similar, eg: > > First of all, you probably want to use phenoData(foo) <- updatedPheno, > not direct slot access. > >> library(Biobase) >> objName <- "sample.ExpressionSet" >> data(list=objName) >> pd <- phenoData(get(objName)) >> >> get(objName)@phenoData <- pd ## this will fail > > Perhaps it would help if you gave some context of what it is you are > trying to achieve. > > Conceptually, get() returns a copy of the object requested and it > doesn't make sense to modify an anonymous copy. > > Any modification of an object results in a copy (there are exceptions, > but let's ignore them for now). But R is clever about when the copy > is actually made (just assigning to a tmp var doesn't trigger a copy). > >> I could copy the object to a temporary one: >> >> tmp <- get(objName) >> tmp at phenoData <- pd >> assign(objName, tmp) >> rm(tmp) >> >> how can I avoid this copy? > > I'm not sure you can, but maybe I'm not understanding what you really > want to do. > > + seth > > -- > Seth Falcon | Computational Biology | Fred Hutchinson Cancer > Research Center > http://bioconductor.org > > _______________________________________________ > 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
ADD REPLY

Login before adding your answer.

Traffic: 575 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6