Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

with_envvar

Environment variables


Description

Temporarily change system environment variables.

Usage

with_envvar(new, code, action = "replace")

local_envvar(
  .new = list(),
  ...,
  action = "replace",
  .local_envir = parent.frame()
)

Arguments

new, .new

[named character]
New environment variables

code

[any]
Code to execute in the temporary environment

action

should new values "replace", "prefix" or "suffix" existing variables with the same name.

...

Named arguments with new environment variables.

.local_envir

[environment]
The environment to use for scoping.

Details

if NA is used those environment variables will be unset. If there are any duplicated variable names only the last one is used.

Value

[any]
The results of the evaluation of the code argument.

See Also

withr for examples

Examples

with_envvar(new = c("GITHUB_PAT" = "abcdef"), Sys.getenv("GITHUB_PAT"))

# with_envvar unsets variables after usage
Sys.getenv("TEMP_SECRET")
with_envvar(new = c("TEMP_SECRET" = "secret"), Sys.getenv("TEMP_SECRET"))
Sys.getenv("TEMP_SECRET")

withr

Run Code 'With' Temporarily Modified Global State

v2.4.2
MIT + file LICENSE
Authors
Jim Hester [aut, cre], Kirill Müller [aut], Kevin Ushey [aut], Hadley Wickham [aut], Winston Chang [aut], Jennifer Bryan [ctb], Richard Cotton [ctb], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.