Get system files or directories, in R subdirectories, in package subdirectories, or elsewhere on the disk (including executables that are accessible on the search path).
system_file(..., exec = FALSE, package = NULL, lib.loc = NULL)
system_dir(..., exec = FALSE, package = NULL, lib.loc = NULL)
systemFile(..., exec = FALSE, package = NULL, lib.loc = NULL)
systemDir(..., exec = FALSE, package = NULL, lib.loc = NULL)
One or several executables if exec = TRUE
, or subpath to a file
or dir in a package directory if package != NULL
, or a list of paths and
subpaths for testing the existence of a file on disk, or a list of directory
components to retrieve in 'temp', 'sysTemp', 'user', 'home', 'bin', 'doc',
'etc' and/or 'share' to retrieve special system directories.
If TRUE
(default) search for executables on the search path.
It supersedes all other arguments.
The name of one package to look for files or subdirs in its
main directory (use exec = FALSE
to search inside package dirs).
A character vector with path names of R libraries or NULL
(search all currently known libraries in this case).
A string with the path to the directories or files, or ""
if they
are not found, or of the wrong type (a dir for system_file()
or or a file
for system_dir()
).
These function aggregate the features of several R functions in
package base: system.file()
, R.home()
, tempdir()
, Sys.which()
, and
aims to provide a unified and convenient single interface to all of them. We
make sure also to check that returned components are respectively directories
and files for system_dir()
and system_file()
.
system_file("INDEX", package = "base")
#> [1] "/opt/R/4.4.2/lib/R/library/base/INDEX"
system_file("help", "AnIndex", package = "splines")
#> [1] "/opt/R/4.4.2/lib/R/library/splines/help/AnIndex"
system_file(package = "base") # This is a dir, not a file!
#> [1] ""
system_file("zip", exec = TRUE)
#> [1] "/usr/bin/zip"
system_file("ftp", "ping", "zip", "nonexistingexe", exec = TRUE)
#> ftp ping zip nonexistingexe
#> "/usr/bin/ftp" "/usr/bin/ping" "/usr/bin/zip" ""
system_dir("temp") # The R temporary directory
#> [1] "/tmp/RtmpTkCdbT"
system_dir("sysTemp") # The system temporary directory
#> [1] "/tmp"
system_dir("user") # The user directory
#> [1] "/home/runner"
system_dir("home", "bin", "doc", "etc", "share") # Various R dirs
#> home bin
#> "/opt/R/4.4.2/lib/R" "/opt/R/4.4.2/lib/R/bin"
#> doc etc
#> "/opt/R/4.4.2/lib/R/doc" "/opt/R/4.4.2/lib/R/etc"
#> share
#> "/opt/R/4.4.2/lib/R/share"
system_dir("zip", exec = TRUE) # Look for the dir of an executable
#> [1] "/usr/bin"
system_dir("ftp", "ping", "zip", "nonexistingexe", exec = TRUE)
#> ftp ping zip nonexistingexe
#> "/usr/bin" "/usr/bin" "/usr/bin" ""
system_dir(package = "base") # The root of the 'base' package
#> [1] "/opt/R/4.4.2/lib/R/library/base"
system_dir(package = "stats") # The root of package 'stats'
#> [1] "/opt/R/4.4.2/lib/R/library/stats"
system_dir("INDEX", package = "stats") # This is a file, not a dir!
#> [1] ""
system_dir("help", package = "splines")
#> [1] "/opt/R/4.4.2/lib/R/library/splines/help"