How can I generate GUIDs and UUIDs in R?
I would like to be able to generate GUIDs based on the hardware etc. of the machine running the rsession.
As a fallback, however, I would be happy to create UUIDs that comply with rfc4122.
Is there a package that can create GUIDs? Otherwise, does someone have some RFC4122 compatible UUID code lying about?
The optimal choice for this now is the uuid
package. It consists of one function (UUIDgenerate
) that doesn't rely on R's internal random number generators and so doesn't suffer any consequences from using set.seed
in a session as @thelatemail's answer does. You can choose to have the UUID be generated either by the package's internal random number generator or based on time.
I know nothing about the intricacies of UUID's, but would something like this do?
baseuuid <- paste(sample(c(letters[1:6],0:9),30,replace=TRUE),collapse="")
paste(
substr(baseuuid,1,8),
"-",
substr(baseuuid,9,12),
"-",
"4",
substr(baseuuid,13,15),
"-",
sample(c("8","9","a","b"),1),
substr(baseuuid,16,18),
"-",
substr(baseuuid,19,30),
sep="",
collapse=""
)
# result like: "f7bd11ed-fca9-42e5-8c3e-4464cd02e0fa"
This should be in line with http://en.wikipedia.org/wiki/Uuid#Version_4_.28random.29
If you are using R in Unix environment, you can get UUID in R using system()
command.
On Linux (Ubuntu 12.04 LTS):
my_uuid <- system("uuid",intern=T)
my_uuid
[1] 0f62f1de-418d-11e3-8a19-cb0ceccb58ec
On Mac OS X 10.8:
my_uuid <- system("uuidgen", intern=T)
my_uuid
[1] 9A9D64DF-EB01-47E7-B16E-DC0343951883
Far as I know, both uuid
and uuidgen
follows UUID Version 4 format.
Bioconductor had an Ruuid package that doesn't seem to be there anymore, but a google search on "Ruuid" will point to places that you can download it.
来源:https://stackoverflow.com/questions/10492817/how-can-i-generate-a-guid-in-r