> packageVersion("stats")
[1] ‘3.5.3’
the returned entity is not a string. it's an S3 class of type package_version
> class(packageVersion("stats"))
[1] "package_version" "numeric_version"
This entity has no attributes, no names, nothing.
> attributes(packageVersion("stats"))
$class
[1] "package_version" "numeric_version"
> names(packageVersion("stats"))
NULL
But somehow this works
> packageVersion("stats")$major
[1] 3
There's also minor and patch. Question is, how do I get the full version as a string? Corollary question, why don't major, minor and patch show up when I ask for the names, and how can I see the full list of names supported by this entity?
To answer your first question: You can do as.character(packageVersion(x)).
The second question is trickier: The S3 class defines its own $ operator. I have no idea why, but here we are.
〉`$.package_version`
function (x, name)
{
name <- pmatch(name, c("major", "minor", "patchlevel"))
x <- unclass(x)
switch(name, major = vapply(x, "[", 0L, 1L), minor = vapply(x,
"[", 0L, 2L), patchlevel = vapply(x, "[", 0L, 3L))
}
<bytecode: 0x7fe3aa8fd000>
<environment: namespace:base>
There may be a good reason for this (maybe lots of package version numbers are generated and cached, and the extra space savings are relevant) but, honestly, it feels overly clever for no good reason. The code is also fairly convoluted — the same thing could be written a lot simpler.
In particular, the use of switch is unnecessary and misleading, and only happens to work because of a lucky coincidence, because name is not a name, it’s an integer (pmatch returns the index of a partially matching entry). And of course there’s absolutely no reason to repeat all that vapply code. So the following does the same, just clearer:
`$.package_version` = function (x, name) {
index = pmatch(name, c("major", "minor", "patchlevel"))
vapply(unclass(x), `[`, integer(1L), index)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With