Title: | Temporarily Change Project Root |
---|---|
Description: | Lets you temporarily execute an expression or a local block with a different here() root in the 'here' package. This is useful for sourcing code in other projects which expect the root directory of here() to be the project directory of those projects. This may be the case with git submodules for example. |
Authors: | Torbjørn Lindahl [aut, cre, cph]
|
Maintainer: | Torbjørn Lindahl <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2025-02-24 19:40:23 UTC |
Source: | https://github.com/torbjorn/wither |
Temporarily change the here() root
with_here(new_here, expr, chdir = FALSE, verbose = FALSE) local_here( new_here, chdir = FALSE, verbose = FALSE, .local_envir = parent.frame() )
with_here(new_here, expr, chdir = FALSE, verbose = FALSE) local_here( new_here, chdir = FALSE, verbose = FALSE, .local_envir = parent.frame() )
new_here |
new temporary here root directory |
expr |
expression to evaluate |
chdir |
also temporarily change working directory |
verbose |
show here's messages on setting new root |
.local_envir |
the environment to use for scoping, see |
Changes here::here()
to temporarily point to a new
directory. Automatically changes back to the original value when
finished.
The with_* and local_* flavours of this functionality mimics that which is typically used in the withr package.
with_here()
returns the result of the
expression. local_here()
returns the original value of
here(), before the change.
Torbjørn Lindahl
library(here) library(withr) d <- local_tempdir() cat("here() is initially:", here(), "\n") # temporarily do things uner a different here() root: with_here(d, cat("but here() is now:", here(), "\n")) # check that everything is shifted back cat("here() is now again:", here(), "\n") local({ d <- local_tempdir() cat("here was initially: ", here(), "\n") local_here(d) cat("after local_here(), here() is: ",here(),"\n") stopifnot(normalizePath(d) == normalizePath(here())) # do something that requires here() be elsewhere }) cat("outside the block, here() is again:", here(), "\n")
library(here) library(withr) d <- local_tempdir() cat("here() is initially:", here(), "\n") # temporarily do things uner a different here() root: with_here(d, cat("but here() is now:", here(), "\n")) # check that everything is shifted back cat("here() is now again:", here(), "\n") local({ d <- local_tempdir() cat("here was initially: ", here(), "\n") local_here(d) cat("after local_here(), here() is: ",here(),"\n") stopifnot(normalizePath(d) == normalizePath(here())) # do something that requires here() be elsewhere }) cat("outside the block, here() is again:", here(), "\n")