knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

util_os

The util_os function returns the operating system of the current R session.

library(jeksterslabRutils)
util_os()

Use case

I use the util_os function to programatically employ forking on operating systems (POSIX) that support it and employ sockets on Windows.

exe <- function(cores = 2) {
  os <- jeksterslabRutils::util_os()
  cat("Operating System:", os)
  if (os %in% c("linux", "osx")) {
    return(
      parallel::mclapply(
        X = 1:5,
        FUN = rnorm,
        mc.cores = cores
      )
    )
  }
  if (os == "windows") {
    cl <- parallel::makeCluster(cores)
    on.exit(
      parallel::stopCluster(cl)
    )
    return(
      parallel::parLapply(
        X = 1:5,
        fun = rnorm
      )
    )
  }
}
exe()


jeksterslabds/jeksterslabRutils documentation built on Jan. 18, 2021, 11:41 p.m.