Learn R Programming

xfun (version 0.48)

env_option: Retrieve a global option from both options() and environment variables

Description

If the option exists in options(), use its value. If not, query the environment variable with the name R_NAME where NAME is the capitalized option name with dots substituted by underscores. For example, for an option xfun.foo, first we try getOption('xfun.foo'); if it does not exist, we check the environment variable R_XFUN_FOO.

Usage

env_option(name, default = NULL)

Value

The option value.

Arguments

name

The option name.

default

The default value if the option is not found in options() or environment variables.

Details

This provides two possible ways, whichever is more convenient, for users to set an option. For example, global options can be set in the .Rprofile file, and environment variables can be set in the .Renviron file.

Examples

Run this code
xfun::env_option("xfun.test.option")  # NULL

Sys.setenv(R_XFUN_TEST_OPTION = "1234")
xfun::env_option("xfun.test.option")  # 1234

options(xfun.test.option = TRUE)
xfun::env_option("xfun.test.option")  # TRUE (from options())
options(xfun.test.option = NULL)  # reset the option
xfun::env_option("xfun.test.option")  # 1234 (from env var)

Sys.unsetenv("R_XFUN_TEST_OPTION")
xfun::env_option("xfun.test.option")  # NULL again

xfun::env_option("xfun.test.option", FALSE)  # use default

Run the code above in your browser using DataLab