R: Get Environment Variables (original) (raw)

Sys.getenv {base} R Documentation

Description

Sys.getenv obtains the values of the environment variables.

Usage

Sys.getenv(x = NULL, unset = "", names = NA)

Arguments

x a character vector, or NULL.
unset a character string.
names logical: should the result be named? If NA (the default) single-element results are not named whereas multi-element results are.

Details

Both arguments will be coerced to character if necessary.

Setting unset = NA will enable unset variables and those set to the value "" to be distinguished, if the OS does. POSIX requires the OS to distinguish, and all known current R platforms do.

Value

A vector of the same length as x, with (if names == TRUE) the variable names as its names attribute. Each element holds the value of the environment variable named by the corresponding component of x (or the value of unset if no environment variable with that name was found).

On most platforms Sys.getenv() will return a named vector giving the values of all the environment variables, sorted in the current locale. It may be confused by names containing = which some platforms allow but POSIX does not. (Windows is such a platform: there names including = are truncated just before the first=.)

When x is missing and names is not false, the result is of class "Dlist" in order to get a nice[print](../../base/help/print.html) method.

See Also

[Sys.setenv](../../base/help/Sys.setenv.html),[Sys.getlocale](../../base/help/Sys.getlocale.html) for the locale in use,[getwd](../../base/help/getwd.html) for the working directory.

The help for ‘environment variables’ lists many of the environment variables used by R.

Examples


## whether HOST is set will be shell-dependent e.g. Solaris' csh did not.
Sys.getenv(c("R_HOME", "R_PAPERSIZE", "R_PRINTCMD", "HOST"))

s <- Sys.getenv() # *all* environment variables
op <- options(width=111) # (nice printing)
names(s)    # all settings (the values could be very long)
head(s, 12) # using the Dlist print() method

## Language and Locale settings -- but rather use Sys.getlocale()
s[grep("^L(C|ANG)", names(s))]
## typically R-related:
s[grep("^_?R_", names(s))]
options(op)# reset

[Package _base_ version 4.6.0 Index]