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")
Run the code above in your browser using DataLab