Debian Codegraph @

R/os.R

raw
1 #' Test for types of operating systems
2 #'
3 #' Functions based on `.Platform$OS.type` and `Sys.info()` to test if
4 #' the current operating system is Windows, macOS, Unix, or Linux.
5 #' @rdname os
6 #' @export
7 #' @examples
8 #' library(xfun)
9 #' # only one of the following statements should be true
10 #' is_windows()
11 #' is_unix() && is_macos()
12 #' is_linux()
13 #' # In newer Macs, CPU can be either Intel or Apple
14 #' is_arm64() # TRUE on Apple silicone machines
15 is_windows = function() .Platform$OS.type == 'windows'
16
17 #' @rdname os
18 #' @export
19 is_unix = function() .Platform$OS.type == 'unix'
20
21 #' @rdname os
22 #' @export
23 is_macos = function() unname(Sys.info()['sysname'] == 'Darwin')
24
25 #' @rdname os
26 #' @export
27 is_linux = function() unname(Sys.info()['sysname'] == 'Linux')
28
29 #' @rdname os
30 #' @export
31 is_arm64 = function() Sys.info()[['machine']] %in% c('arm64', 'aarch64')
32
33 # open a file (optionally in an editor) or directory
34 open_path = function(x, edit = !dir_exists(x), ...) {
35 if (edit) {
36 tryCatch(rstudioapi::navigateToFile(x, ...), error = function(e) file.edit(x))
37 } else {
38 if (is_windows()) shell.exec(x) else {
39 system2(if (is_macos()) 'open' else 'xdg-open', shQuote(x))
40 }
41 }
42 }