Skip to content

Minimize use of system() and system2() #175

Description

@HenrikBengtsson

Background

system() and system2() calls do not work in some environments, e.g. webR (R WebAssembly; https://webr.sh). Base R uses such calls in several places. Are they all truly needed?

A good project would be to go through the R source code and see there are some that can be replaced by plain R code. From a [quick search](https://github.com/search?q=repo%3Ar-devel%2Fr-svn%20language%3Ar%20path%3A%2Fsrc%2F**%2F*.R%20%22system(%22%20OR%20%22system2(%22%20&type=code), one candidate is:

parallel::detectCores() on Linux which effectively does:

cmd <- 'grep "^processor" /proc/cpuinfo 2>/dev/null | wc -l'
value <- system(cmd, intern = TRUE)

That can easily be replaced by:

value <- if (utils::file_test("-f", "/proc/cpuinfo")) {
  bfr <- readLines("/proc/cpuinfo", warn = FALSE)
  sum(grepl("^processor", bfr))
} else {
  0L
}

This would also remove the dependency on grep and wc, and the execution of those. Those tools on the file system (e.g. /usr/bin/grep, /usr/bin/wc).

Maybe there are others?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions