Servet Ahmet Çizmeli
2016-Jun-08 12:24 UTC
[R] UUIDgenerate() withn a s4 class produces the same uuid at each instance
Hello When I create a new instance of an S4 class in R, I would like the newly created object to have a unique id field. I try to achieve it through UUIDgenerate() from the uuid package. The problem is that I obtain the same UUID at every new object instance : library(uuid) setClass("C", representation=representation( id = "character" ), prototype = prototype( id = UUIDgenerate(use.time = TRUE)) ) new("C") An object of class "C" Slot "id": [1] "1e07d7c2-2d71-11e6-b5e1-e1f59d8ccf09" new("C") An object of class "C" Slot "id": [1] "1e07d7c2-2d71-11e6-b5e1-e1f59d8ccf09" new("C") An object of class "C" Slot "id": [1] "1e07d7c2-2d71-11e6-b5e1-e1f59d8ccf09" Calling UUIDgenerate() successively at the R command line produces different UUIDS each time. Where do I go wrong? Thanks Servet [[alternative HTML version deleted]]
Ben Tupper
2016-Jun-08 14:22 UTC
[R] UUIDgenerate() withn a s4 class produces the same uuid at each instance
Hi, I think your issue is that in the absence of an explicit 'initialize' method the value of id is assigned when you define the class prototype. All subsequent new instances will use that very same prototype value defined when you first defined the class. That's in the help for ?new, "The function new begins by copying the prototype object from the class definition." Read on in the details section to learn more about 'initialize'. To get a new id for each new object instance you can define an 'initialize' method. setMethod("initialize", "C", function(.Object){ .Object at id = UUIDgenerate(use.time = TRUE) .Object })> new("C")An object of class "C" Slot "id": [1] "f5e87504-2d83-11e6-9dcb-685b35bcefe9"> new("C")An object of class "C" Slot "id": [1] "f73cf010-2d83-11e6-9dcb-685b35bcefe9"> new("C")An object of class "C" Slot "id": [1] "f8321f22-2d83-11e6-9dcb-685b35bcefe9"> new("C")An object of class "C" Slot "id": [1] "f96e3c90-2d83-11e6-9dcb-685b35bcefe9" There may be other ways to achieve the same outcome. Caveat... I handy with Reference Classes (see ?setRefClass) but have only recently exploring S4 classes. Cheers, Ben> On Jun 8, 2016, at 8:24 AM, Servet Ahmet ?izmeli <Servet.Ahmet.Cizmeli at USherbrooke.ca> wrote: > > > > Hello > > When I create a new instance of an S4 class in R, I would like the newly > created object to have a unique id field. I try to achieve it through > UUIDgenerate() from the uuid package. The problem is that I obtain the > same UUID at every new object instance : > > library(uuid) > setClass("C", > representation=representation( > id = "character" > ), > prototype = prototype( > id = UUIDgenerate(use.time = TRUE)) > ) > > new("C") > > An object of class "C" Slot "id": [1] > "1e07d7c2-2d71-11e6-b5e1-e1f59d8ccf09" > > new("C") > > An object of class "C" Slot "id": [1] > "1e07d7c2-2d71-11e6-b5e1-e1f59d8ccf09" > > new("C") > > An object of class "C" Slot "id": [1] > "1e07d7c2-2d71-11e6-b5e1-e1f59d8ccf09" > > Calling UUIDgenerate() successively at the R command line produces > different UUIDS each time. > > Where do I go wrong? > > Thanks > > Servet > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org Report Gulf of Maine jellyfish sightings to jellyfish at bigelow.org or tweet them to #MaineJellies -- include date, time, and location, as well as any descriptive information such as size or type. Learn more at https://www.bigelow.org/research/srs/nick-record/nick-record-laboratory/mainejellies/