After assessing that a user is using Darwin, how can I us R to check if they have >= version? For example I'm running Sonoma which is version 23.0.0. In the code below we see the system call says 23.0.0.
> system("uname -r")
23.0.0
How can I then compare this output to a value? I'd expect the following to be TRUE.
> system("uname -r") >= '23.0.0'
23.0.0
[1] FALSE
This also gave a FALSE:
> as.numeric_version(system("uname -r")) >= '23.0.0'
23.0.0
[1] FALSE
And tried:
> as.numeric(capture.output(system("uname -r"))) >= '23.0.0'
23.0.0
logical(0)
> as.numeric_version(capture.output(system("uname -r"))) >= '23.0.0'
23.0.0
logical(0)
According to the ?system help file, you need to explicitly request that R captures the output of the command as something usable to R:
system("uname -r", intern=TRUE)
intern: a logical (not 'NA') which indicates whether to capture the output of the command as an R character vector.
@thelatemail's comment about intern=TRUE is good and will work much of the time, but I think you need a little more to do valid package comparisons (e.g. when version number components have different numbers of digits).
On my system, the results of uname -r have some alphabetic stuff that I have to trim to make the results a valid R version type:
s <- system("uname -r", intern = TRUE) |>
gsub(pattern = "-[[:alpha:]]*", replacement = "")
# [1] "6.4.676060406"
(could also have used stringr::str_remove())
Comparing with "6.4.6761", which is numerically less than the last package component:
s < "6.4.6761" ## TRUE
as.package_version(s) < "6.4.6761" ## FALSE
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