Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if MacOS version is Greater Than or Equal to

Tags:

r

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)
like image 439
Tyler Rinker Avatar asked Nov 01 '25 14:11

Tyler Rinker


2 Answers

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.

like image 182
thelatemail Avatar answered Nov 03 '25 04:11

thelatemail


@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
like image 39
Ben Bolker Avatar answered Nov 03 '25 05:11

Ben Bolker