withr: Run Code 'With' Temporarily Modified Global State

> library(withr)

バージョン: 1.0.0


関数名 概略
with_collate Collation Order
with_dir Working directory
with_envvar Environment variables
with_libpaths Library paths
with_locale Locale settings
with_makevars Makevars variables
with_options Options
with_par Graphics parameters
with_path PATH environment variable
with_temp_libpaths Library paths
withr Execute code in temporarily altered environment

with_dir

> dir.create("test")
> getwd()
[1] "/Users/uri/git/rpkg_showcase/development"
> with_dir("test2", getwd())
Error in setwd(dir = new): cannot change working directory
> getwd()
[1] "/Users/uri/git/rpkg_showcase/development"
> unlink("test")

with_envvar

一時的な環境変数の設定

> Sys.getenv("HADLEY")
[1] ""
> with_envvar(c("HADLEY" = "Hadley"), Sys.getenv("HADLEY"))
[1] "Hadley"
> Sys.getenv("HADLEY")
[1] ""