Has anyone figured out how to create a destructor in R.oo? How I'd like to use it: I have an object which opens a connection thru RODBC (held as a private member) It would be nice if the connection closes automatically (inside the destructor) when an object gets gc()'ed. Thanks in advance. Regards, Ken BTW, a >BIG< thanks to Henrik Bengtsson for creating the R.oo package! Lucky for me as I am not smart enough to use S4...
I have a very long vector of character strings of the format "GO:0008104.ISS" and need to strip off the dot and anything that follows it. There are always 10 characters before the dot. The actual characters and the number of them after the dot is variable. So, I would like to return in the format "GO:0008104" . I could do this with substr and loop over the entire vector, but I thought there might be a more elegant (and faster) way to do this. I have tried gsub using regular expressions without success. The code gsub(pattern= "\.*?" , replacement="", x=character.vector) correctly locates the positions in the vector that contain the dot, but replaces all of the strings with "". Obviously not what I want. Is there a regular expression for replacement that would accomplish what I want? Or, does R have a better way to do this? Thanks, Mark Mark W. Kimpel MD (317) 490-5129 Work, & Mobile (317) 663-0513 Home (no voice mail please) 1-(317)-536-2730 FAX
Try this:> gsub("[.].*", "", "GO:0008104.ISS")[1] "GO:0008104" On 1/17/07, Kimpel, Mark William <mkimpel at iupui.edu> wrote:> I have a very long vector of character strings of the format > "GO:0008104.ISS" and need to strip off the dot and anything that follows > it. There are always 10 characters before the dot. The actual characters > and the number of them after the dot is variable. > > So, I would like to return in the format "GO:0008104" . I could do this > with substr and loop over the entire vector, but I thought there might > be a more elegant (and faster) way to do this. > > I have tried gsub using regular expressions without success. The code > > gsub(pattern= "\.*?" , replacement="", x=character.vector) > > correctly locates the positions in the vector that contain the dot, but > replaces all of the strings with "". Obviously not what I want. Is there > a regular expression for replacement that would accomplish what I want? > > Or, does R have a better way to do this? > > Thanks, > > Mark > > Mark W. Kimpel MD > > > > (317) 490-5129 Work, & Mobile > > > > (317) 663-0513 Home (no voice mail please) > > 1-(317)-536-2730 FAX > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Hi, I'm about the head out of the office next 48 hours, but the short answer is to override the finalize() method in your subclass of Object. This will be called when R garbage collects the object. From ?finalize.Object: setConstructorS3("MyClass", function() { extend(Object(), "MyClass") }) setMethodS3("finalize", "MyClass", function(this) { cat(as.character(this), "is about to be removed from the memory!\n") }) o <- MyClass() o <- MyClass() o <- MyClass() o <- MyClass() gc() ## Not run: MyClass: 0x31543776 is about to be removed from the memory! MyClass: 0x31772572 is about to be removed from the memory! MyClass: 0x32553244 is about to be removed from the memory! used (Mb) gc trigger (Mb) max used (Mb) Ncells 246049 6.6 407500 10.9 350000 9.4 Vcells 132538 1.1 786432 6.0 404358 3.1 rm(o) gc() MyClass: 0x31424196 is about to be removed from the memory! used (Mb) gc trigger (Mb) max used (Mb) Ncells 246782 6.6 467875 12.5 354145 9.5 Vcells 110362 0.9 786432 6.0 404358 3.1 Hope this helps Henrik On 1/18/07, Feng, Ken [CIB-EQTY] <ken.feng at citigroup.com> wrote:> > Has anyone figured out how to create a destructor in R.oo? > > How I'd like to use it: I have an object which opens a connection thru RODBC > (held as a private member) It would be nice if the connection closes automatically > (inside the destructor) when an object gets gc()'ed. > > Thanks in advance. > > Regards, > Ken > BTW, a >BIG< thanks to Henrik Bengtsson for creating the R.oo package! > Lucky for me as I am not smart enough to use S4... > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >